Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroHoncharuk committed Dec 12, 2024
1 parent f746309 commit 83df2d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,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 `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://DmytroHoncharuk.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
55 changes: 37 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,45 @@ import React from 'react';
import './App.scss';
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 = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC = () => {
const [goodsFromServer, setGoodsFromServer] = React.useState<Good[]>([]);

<button type="button" data-cy="all-button">
Load all goods
</button>
const get5Goods = () => {
get5First().then(setGoodsFromServer);
};

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
const getAllGoods = () => {
getAll().then(setGoodsFromServer);
};

<button type="button" data-cy="red-button">
Load red goods
</button>
const getRedGood = () => {
getRedGoods().then(setGoodsFromServer);
};

<GoodsList goods={[]} />
</div>
);
return (
<div className="App">
<h1>Dynamic list of Goods</h1>

<button type="button" data-cy="all-button" onClick={() => getAllGoods()}>
Load all goods
</button>

<button
type="button"
data-cy="first-five-button"
onClick={() => get5Goods()}
>
Load 5 first goods
</button>

<button type="button" data-cy="red-button" onClick={() => getRedGood()}>
Load red goods
</button>

<GoodsList goods={goodsFromServer} />
</div>
);
};
2 changes: 1 addition & 1 deletion src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Props = {
export const GoodsList: React.FC<Props> = ({ goods }) => (
<ul>
{goods.map(good => (
<li key={good.id} data-cy="good">
<li key={good.id} data-cy="good" style={{ color: good.color }}>
{good.name}
</li>
))}
Expand Down
14 changes: 10 additions & 4 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ export function getAll(): Promise<Good[]> {
return fetch(API_URL).then(response => response.json());
}

export const get5First = () => {
return getAll().then(goods => goods); // sort and get the first 5
export const get5First = (): Promise<Good[]> => {
return getAll().then(goods => {
goods.sort((a, b) => a.name.localeCompare(b.name));

return goods.slice(0, 5);
}); // sort and get the first 5
};

export const getRedGoods = () => {
return getAll().then(goods => goods); // get only red
export const getRedGoods = (): Promise<Good[]> => {
return getAll().then(goods => {
return goods.filter(good => good.color === 'red');
}); // get only red
};

0 comments on commit 83df2d5

Please sign in to comment.