Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
home2ego committed Jan 13, 2025
1 parent f746309 commit 97b25e0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
57 changes: 37 additions & 20 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC = () => {
const [goods, setGoods] = useState<Good[]>([]);

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

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
<button
type="button"
data-cy="all-button"
onClick={() =>
getAll().then(goodsFromServer => setGoods(goodsFromServer))
}
>
Load all goods
</button>

<button type="button" data-cy="red-button">
Load red goods
</button>
<button
type="button"
data-cy="first-five-button"
onClick={() => get5First().then(setGoods)}
>
Load 5 first goods
</button>

<GoodsList goods={[]} />
</div>
);
<button
type="button"
data-cy="red-button"
onClick={() => getRedGoods().then(setGoods)}
>
Load red goods
</button>

<GoodsList goods={goods} />
</div>
);
};
21 changes: 11 additions & 10 deletions src/GoodsList.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react';
import { Good } from './types/Good';

type Props = {
goods: Good[];
};

export const GoodsList: React.FC<Props> = ({ goods }) => (
<ul>
{goods.map(good => (
<li key={good.id} data-cy="good">
{good.name}
</li>
))}
</ul>
);
export default function GoodsList({ goods }: Props) {
return (
<ul>
{goods.map(good => (
<li key={good.id} data-cy="good" style={{ color: good.color }}>
{good.name}
</li>
))}
</ul>
);
}
8 changes: 6 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ export function getAll(): Promise<Good[]> {
}

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

0 comments on commit 97b25e0

Please sign in to comment.