From 6513bd583636215656d6cde38a64c79d266c535c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNikita?= Date: Tue, 30 Jan 2024 01:37:14 +0200 Subject: [PATCH] add task solution --- src/App.tsx | 71 ++++++++++++++++++++++++++++++++++------------- src/GoodsList.tsx | 2 +- src/api/goods.ts | 6 ++-- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 2cf5239da..ab2eda796 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,60 @@ -import React from 'react'; +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([]); + const handleClick = (query:string) => { + switch (query) { + case 'all': + goodsAPI.getAll() + .then(setGoods); + break; + case 'first5': + goodsAPI.get5First() + .then(setGoods); + break; + case 'red': + goodsAPI.getRedGoods() + .then(setGoods); + break; + default: + goodsAPI.getAll() + .then(setGoods); + } + }; -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..304707bf9 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -8,7 +8,7 @@ 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..3e5dc6324 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((a, b) => a.name.localeCompare(b.name)) + .slice(0, 5)); }; export const getRedGoods = () => { return getAll() - .then(goods => goods); // get only red + .then(goods => goods.filter(good => good.color === 'red')); };