From 10a72f6709026a68809c8e53a43382265b00eb7d Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 23 Jan 2024 20:45:55 +0200 Subject: [PATCH] add task solution --- README.md | 2 +- src/App.tsx | 67 +++++++++++++++++++++++++++++++++++------------ src/GoodsList.tsx | 6 ++++- src/api/goods.ts | 6 +++-- 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index aa688e6f2..9b98443f1 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://IShamkii.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..7914dea15 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,60 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import './App.scss'; import { GoodsList } from './GoodsList'; -// import { getAll, get5First, getRed } from './api/goods'; +import { getAll, get5First, getRedGoods } from './api/goods'; +import { Good } from './types/Good'; // or // import * as goodsAPI from './api/goods'; -export const App: React.FC = () => ( -
-

Dynamic list of Goods

+export const App: React.FC = () => { + const [visibleGoods, setVisibleGoods] = useState([]); - + useEffect(() => { + getAll().then(setVisibleGoods); + }, []); - + const allGoods = () => { + getAll().then(setVisibleGoods); + }; - + const firstFive = () => { + get5First().then(setVisibleGoods); + }; - -
-); + const redGoods = () => { + getRedGoods().then(setVisibleGoods); + }; + + return ( +
+

Dynamic list of Goods

+ + + + + + + + +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index b56a4331e..dd8501191 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..0217b1e5c 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((g1, g2) => g1.name.localeCompare(g2.name)) + .slice(0, 5)); }; export const getRedGoods = () => { return getAll() - .then(goods => goods); // get only red + .then(goods => goods.filter(good => good.color === 'red')); };