From 3421145e4cb6731d9a95167483673aa1d72730da Mon Sep 17 00:00:00 2001 From: Dmytro Manankin Date: Mon, 29 Jan 2024 20:59:56 +0200 Subject: [PATCH] task done --- README.md | 12 ++++----- src/App.tsx | 63 ++++++++++++++++++++++++++++++++++------------- src/GoodsList.tsx | 8 +++++- src/api/goods.ts | 6 +++-- 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index aa688e6f2..c22f1e94d 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,16 @@ You have 3 button that should load [the goods](https://mate-academy.github.io/re 1. `Load All goods` should load and show all the `goods`; 1. `Load 5 first goods` should do the next: - - load all the goods; - - sort them by name; - - and show the first 5; + DONE - load all the goods; + DONE - sort them by name; + DONE - and show the first 5; 1. `Load red goods` should load all the goods show only `red` ones; -1. Server has only 1 endpoint returning all the goods, so you should do all the preparations in corresponding methods in `/api/goods`. -1. `GoodsList` is almost finished, you just need to use corresponding colors for `li`s; +1. DONE - Server has only 1 endpoint returning all the goods, so you should do all the preparations in corresponding methods in `/api/goods`. +1. DONE - `GoodsList` is almost finished, you just need to use corresponding colors for `li`s; ## Instructions - 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. +DONE - Replace `` with your Github username in the [DEMO LINK](https://Manankin.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..f25aacf91 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,56 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.scss'; import { GoodsList } from './GoodsList'; +import { Good } from './types/Good'; -// import { getAll, get5First, getRed } from './api/goods'; +import { getAll, get5First, getRedGoods } from './api/goods'; // 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([]); - + return ( +
+

Dynamic list of Goods

- + - + - -
-); + + + +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index b56a4331e..c6abceef0 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -8,7 +8,13 @@ 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')); };