Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavYarynych committed Oct 30, 2023
1 parent bfed414 commit 9aa6da3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
64 changes: 45 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
import React from 'react';
import React, { useState } from 'react';
import { get5First, getAll, getRedGoods } from './api/goods';
import './App.scss';
import { GoodsList } from './GoodsList';
import { Good } from './types/Good';

// import { getAll, get5First, getRed } from './api/goods';
// or
// import * as goodsAPI from './api/goods';
export const App: React.FC = () => {
const [goods, setGoods] = useState<Good[]>([]);

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
const handleLoadAll = () => {
getAll().then(goodsFromServer => setGoods(goodsFromServer));
};

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

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
const handleLoadOnlyRed = () => {
getRedGoods().then(onlyRedGoods => setGoods(onlyRedGoods));
};

<button type="button" data-cy="red-button">
Load red goods
</button>
return (
<div className="App">
<h1>Dynamic list of Goods</h1>

<GoodsList goods={[]} />
</div>
);
<button
type="button"
data-cy="all-button"
onClick={handleLoadAll}
>
Load all goods
</button>

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

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

<GoodsList goods={goods} />
</div>
);
};
6 changes: 5 additions & 1 deletion src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ 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
12 changes: 10 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ export function getAll(): Promise<Good[]> {

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));

return goods.slice(0, 5);
});
};

export const getRedGoods = () => {
return getAll()
.then(goods => goods); // get only red
.then(goods => {
const filteredGoods = goods.filter(good => good.color === 'red');

return filteredGoods;
});
};

0 comments on commit 9aa6da3

Please sign in to comment.