项目作者: remylenoir

项目描述 :
Compare the air quality from cities in the United Kingdom using a feed from OpenAQ.
高级语言: JavaScript
项目地址: git://github.com/remylenoir/compare-your-air.git
创建时间: 2019-06-20T00:51:29Z
项目社区:https://github.com/remylenoir/compare-your-air

开源协议:

下载


:dash: ​Compare your Air

Introduction

The application allows you to compare the air quality from cities in the United Kingdom using a feed from OpenAQ).

Install and run the application

  1. npm i ; npm start

By default, the app will start on the 3000 PORT.


Technologies & methodologies

  • Node JS
  • React
  • SASS
  • BEM, DRY methodologies

Modules

Code structure

The application is using React hooks:

  • useState()
  • useEffect()
  • useContext()

More information: hooks reference

Application states

The application states are set in the ./Store.js , using the React Context API. Thus you can access to the states from anywhere in your application without the hassle to pass down the props/lift up the states in the components.

  1. // Store.js
  2. export const DataContext = createContext();
  3. const Store = ({ children }) => {
  4. const [data, setData] = useState({
  5. locations: []
  6. });
  7. });
  8. return (
  9. <DataContext.Provider value={[data, setData]}>{children}</DataContext.Provider>
  10. );
  1. // index.js
  2. import Store from './Store';
  3. <Store>
  4. <App ></App>
  5. </Store>

More information: Context API reference

Data fetching

The data is fetched using an Axios GET request.

More information: npm package

Feed logic

In the current version, the feed is fetching the latest records. The output is filtered using these parameters:

  • Country selection (ISO code): country=GB
  • Limit: limit=500

More information: available feeds and parameters


Documentation

Feed

Get and store the data

The getData() function is located at ./src/services/airquality.js.

  1. // airquality.js
  2. const getData = async (countryISO, limit) => {
  3. try {
  4. const response = await axios.get(
  5. `https://api.openaq.org/v1/latest?country=${countryISO}&limit=${limit}`
  6. );
  7. return response.data.results;
  8. } catch (error) {
  9. return console.error(error);
  10. }
  11. };

In the main component ./src/App.js, the arguments are passed to the getData() function.

  1. // App.js
  2. useEffect(() => {
  3. getData('GB', 500)
  4. .then(locations => setData({ locations }))
  5. .catch(error => console.error(error));
  6. }, [setData]);

Add a parameter

You can add a new parameter via the Axios GET request located at ./src/services/airquality.js.
Then you can pass the value in the getData() function located at ./src/App.js (see above) .

More information: available feeds and parameters


Styling

The application is styled via SASS, and using BEM methodology as a naming convention.
Combining BEM with SASS make the code flow fast and powerful.

  1. // _card.scss
  2. .card {
  3. background-color: #fff;
  4. border-radius: $radius;
  5. margin: 20px;
  6. padding: 15px;
  7. width: $search-width;
  8. max-width: $search-width;
  9. box-shadow: $box-shadow;
  10. &__time {
  11. font-weight: 600;
  12. font-size: 0.8rem;
  13. }
  14. // ...
  15. }

More information: SASS guidelinesBEM methodology

Structure

The application is using the 7-1 pattern.

Add a SCSS file

You can add a file in the corresponding folders, in ./src/stylesheets/.
Please ensure to @import the file in the ./src/stylesheets/main.scss.

  1. // main.scss
  2. // Abstracts: global variables, helpers, mixins, etc.
  3. @import './abstracts/mixins';
  4. @import './abstracts/helpers';
  5. @import './abstracts/variables';
  6. // Base: global styles (resets, typography, colors, etc)
  7. @import './base/reset';
  8. @import './base/typography';
  9. // Components: styling for each components
  10. @import './components/card';
  11. @import './components/search';
  12. // Pages: page-specific styling
  13. @import './pages/home';