diff --git a/README.md b/README.md index aa688e6f2..b0233dcb4 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ You have 3 button that should load [the goods](https://mate-academy.github.io/re - Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline). - Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript). - Open one more terminal and run tests with `npm test` to ensure your solution is correct. -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_dynamic-list-of-goods/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://MaxKalalashnyk02.github.io/react_dynamic-list-of-goods/) and add it to the PR description. diff --git a/package.json b/package.json index b3f865f66..d8c723cd7 100755 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "@mate-academy/cypress-tools": "^1.0.5", "@mate-academy/eslint-config-react": "^0.0.11", "@mate-academy/eslint-config-react-typescript": "^1.0.12", - "@mate-academy/scripts": "^1.2.12", + "@mate-academy/scripts": "^1.3.2", "@mate-academy/students-ts-config": "*", "@mate-academy/stylelint-config": "^0.0.11", "@types/node": "^17.0.23", diff --git a/src/App.tsx b/src/App.tsx index 2cf5239da..532ce31f1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,46 @@ -import React from 'react'; +// App.tsx +import React, { useState } from 'react'; import './App.scss'; import { GoodsList } from './GoodsList'; +import { Good } from './types/Good'; +import * as goodsAPI from './api/goods'; -// import { getAll, get5First, getRed } from './api/goods'; -// or -// import * as goodsAPI from './api/goods'; +export const App: React.FC = () => { + const [goods, setGoods] = useState([]); -export const App: React.FC = () => ( -
-

Dynamic list of Goods

+ const loadAllGoods = () => { + goodsAPI.getAll().then(setGoods); + }; - + const load5FirstGoods = () => { + goodsAPI.get5First().then(setGoods); + }; - + const loadRedGoods = () => { + goodsAPI.getRedGoods().then(setGoods); + }; - + return ( +
+

Dynamic list of Goods

- -
-); + + + + + + + +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index b56a4331e..aedb2b831 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -2,13 +2,13 @@ import React from 'react'; import { Good } from './types/Good'; type Props = { - goods: Good[] + goods: Good[]; }; export const GoodsList: React.FC = ({ goods }) => (
    {goods.map(good => ( -
  • +
  • {good.name}
  • ))} diff --git a/src/api/goods.ts b/src/api/goods.ts index f0d1659f8..a1bc22e50 100644 --- a/src/api/goods.ts +++ b/src/api/goods.ts @@ -1,19 +1,20 @@ import { Good } from '../types/Good'; -// eslint-disable-next-line -const API_URL = `https://mate-academy.github.io/react_dynamic-list-of-goods/goods.json`; +// eslint-disable-next-line max-len +const API_URL = 'https://mate-academy.github.io/react_dynamic-list-of-goods/goods.json'; export function getAll(): Promise { return fetch(API_URL) .then(response => response.json()); } -export const get5First = () => { +export const get5First = (): Promise => { return getAll() - .then(goods => goods); // sort and get the first 5 + // eslint-disable-next-line max-len + .then(goods => goods.sort((a, b) => a.name.localeCompare(b.name)).slice(0, 5)); }; -export const getRedGoods = () => { +export const getRedGoods = (): Promise => { return getAll() - .then(goods => goods); // get only red + .then(goods => goods.filter(good => good.color.toLowerCase() === 'red')); };