diff --git a/src/App.tsx b/src/App.tsx index 2cf5239da..e01a87dcf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,44 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.scss'; -import { GoodsList } from './GoodsList'; +import GoodsList from './GoodsList'; -// import { getAll, get5First, getRed } from './api/goods'; -// or -// import * as goodsAPI from './api/goods'; +import { getAll, get5First, getRedGoods } from './api/goods'; +import { Good } from './types/Good'; -export const App: React.FC = () => ( -
-

Dynamic list of Goods

+export const App: React.FC = () => { + const [goods, setGoods] = useState([]); - + return ( +
+

Dynamic list of Goods

- + - + - -
-); + + + +
+ ); +}; diff --git a/src/GoodsList.tsx b/src/GoodsList.tsx index ca92d7573..9b88641e2 100644 --- a/src/GoodsList.tsx +++ b/src/GoodsList.tsx @@ -1,16 +1,17 @@ -import React from 'react'; import { Good } from './types/Good'; type Props = { goods: Good[]; }; -export const GoodsList: React.FC = ({ goods }) => ( - -); +export default function GoodsList({ goods }: Props) { + return ( + + ); +} diff --git a/src/api/goods.ts b/src/api/goods.ts index 551d0c275..b088eabcf 100644 --- a/src/api/goods.ts +++ b/src/api/goods.ts @@ -8,9 +8,13 @@ export function getAll(): Promise { } export const get5First = () => { - return getAll().then(goods => goods); // sort and get the first 5 + return getAll().then(goods => + goods + .sort((good1, good2) => good1.name.localeCompare(good2.name)) + .slice(0, 5), + ); // sort and get the first 5 }; export const getRedGoods = () => { - return getAll().then(goods => goods); // get only red + return getAll().then(goods => goods.filter(good => good.color === 'red')); // get only red };