From 4a00861d146e9c8554db8e981622d11784e3f449 Mon Sep 17 00:00:00 2001 From: Izzet Yusufov <17izzet@gmail.com12345Hello> Date: Tue, 1 Aug 2023 13:17:25 +0200 Subject: [PATCH] solution --- README.md | 2 +- src/App.scss | 12 ++++++- src/App.tsx | 81 ++++++++++++++++++++++++++++++++++++----------- src/GoodsList.tsx | 6 +++- src/api/goods.ts | 5 +-- 5 files changed, 82 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index aa688e6f2..447355f8c 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://IzzetYusufov.github.io/react_dynamic-list-of-goods/) and add it to the PR description. diff --git a/src/App.scss b/src/App.scss index dc81a3449..5fc79d49f 100644 --- a/src/App.scss +++ b/src/App.scss @@ -1 +1,11 @@ -// styles go here +.red--item { + color: red; +} + +.blue--item { + color: blue; +} + +.green--item { + color: green; +} diff --git a/src/App.tsx b/src/App.tsx index 2cf5239da..1d6da46c6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,70 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import './App.scss'; import { GoodsList } from './GoodsList'; +import { Good } from './types/Good'; +import { getAll, get5First, getRedGoods } from './api/goods'; -// import { getAll, get5First, getRed } from './api/goods'; -// or -// import * as goodsAPI from './api/goods'; +enum ServerRequest { + AllGoods = 'ALL', + FiveFirst = 'FiveFirst', + OnlyRed = 'OnlyRed', +} -export const App: React.FC = () => ( -
-

Dynamic list of Goods

+export const App: React.FC = () => { + const [goods, setGoods] = useState([]); + const [ + serverReqeust, + setServerReqeust, + ] = useState(null); - + useEffect(() => { + if (serverReqeust === ServerRequest.AllGoods) { + getAll().then((goodsRequest: Good[]) => { + setGoods(goodsRequest); + }); + } - + if (serverReqeust === ServerRequest.FiveFirst) { + get5First().then((goodsRequest: Good[]) => { + setGoods(goodsRequest); + }); + } - + if (serverReqeust === ServerRequest.OnlyRed) { + getRedGoods().then((goodsRequest: Good[]) => { + setGoods(goodsRequest); + }); + } + }, [serverReqeust]); - -
-); + return ( +
+

Dynamic list of Goods

+ + + + + + + {serverReqeust !== null && } +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index b56a4331e..226b99356 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..041c60c8d 100644 --- a/src/api/goods.ts +++ b/src/api/goods.ts @@ -10,10 +10,11 @@ export function getAll(): Promise { export const get5First = () => { return getAll() - .then(goods => goods); // sort and get the first 5 + .then(goods => goods + .sort((a, b) => a.name.localeCompare(b.name)).slice(0, 5)); // sort and get the first 5 }; export const getRedGoods = () => { return getAll() - .then(goods => goods); // get only red + .then(goods => goods.filter(good => good.color === 'red')); // get only red };