From 7e358d3209c103c4d4a04f336e336933339e97bf Mon Sep 17 00:00:00 2001 From: Kriv Date: Wed, 2 Aug 2023 13:18:57 +0300 Subject: [PATCH 1/2] solution --- README.md | 2 +- src/App.tsx | 58 +++++++++++++++++++++++++++++++---------------- src/GoodsList.tsx | 6 ++++- src/api/goods.ts | 6 +++-- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index aa688e6f2..dbcf35c21 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://Krivets.github.io/react_dynamic-list-of-goods/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 2cf5239da..9a2f16442 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,47 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.scss'; import { GoodsList } from './GoodsList'; +import { get5First, getAll, getRedGoods } from './api/goods'; +import { Good } from './types/Good'; -// import { getAll, get5First, getRed } from './api/goods'; -// or -// import * as goodsAPI from './api/goods'; +export const App: React.FC = () => { + const [goodsList, setGoodsList] = useState([]); -export const App: React.FC = () => ( -
-

Dynamic list of Goods

+ return ( +
+

Dynamic list of Goods

- + - + - + - -
-); + +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index b56a4331e..45e78feef 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -8,7 +8,11 @@ type Props = { 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..2b2964f59 100644 --- a/src/api/goods.ts +++ b/src/api/goods.ts @@ -10,10 +10,12 @@ export function getAll(): Promise { export const get5First = () => { return getAll() - .then(goods => goods); // sort and get the first 5 + .then(goods => goods + .sort((word1, word2) => word1.name.localeCompare(word2.name)) + .slice(0, 5)); }; export const getRedGoods = () => { return getAll() - .then(goods => goods); // get only red + .then(goods => goods.filter((good: Good) => good.color === 'red')); // get only red }; From fee05512aa1790e2daf2557ba1f00d99ca88ac2d Mon Sep 17 00:00:00 2001 From: Kriv Date: Wed, 2 Aug 2023 16:15:34 +0300 Subject: [PATCH 2/2] solution --- src/App.tsx | 10 +++++++--- src/GoodsList.tsx | 22 +++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 9a2f16442..020963d67 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,6 +7,10 @@ import { Good } from './types/Good'; export const App: React.FC = () => { const [goodsList, setGoodsList] = useState([]); + const responseHandler = (getMethod: () => Promise) => { + getMethod().then(setGoodsList); + }; + return (

    Dynamic list of Goods

    @@ -15,7 +19,7 @@ export const App: React.FC = () => { type="button" data-cy="all-button" onClick={() => { - getAll().then(setGoodsList); + responseHandler(getAll); }} > Load all goods @@ -25,7 +29,7 @@ export const App: React.FC = () => { type="button" data-cy="first-five-button" onClick={() => { - get5First().then(setGoodsList); + responseHandler(get5First); }} > Load 5 first goods @@ -35,7 +39,7 @@ export const App: React.FC = () => { type="button" data-cy="red-button" onClick={() => { - getRedGoods().then(setGoodsList); + responseHandler(getRedGoods); }} > Load red goods diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index 45e78feef..1f8da6139 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -7,14 +7,18 @@ type Props = { export const GoodsList: React.FC = ({ goods }) => (
      - {goods.map(good => ( -
    • - {good.name} -
    • - ))} + {goods.map(good => { + const { id, color, name } = good; + + return ( +
    • + {name} +
    • + ); + })}
    );