Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #1693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@mate-academy/cypress-tools": "^1.0.4",
"@mate-academy/eslint-config-react": "0.0.7",
"@mate-academy/eslint-config-react-typescript": "^1.0.1",
"@mate-academy/scripts": "^1.2.3",
"@mate-academy/scripts": "^1.2.8",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^17.0.23",
Expand Down
76 changes: 57 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { getAll, get5First, getRedGoods } from './api/goods';
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 [allGoods, setAllGoods] = useState<Good[]>([]);

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
useEffect(() => {
getAll().then(goods => {
setAllGoods(goods);
});
}, []);

<button type="button" data-cy="all-button">
Load all goods
</button>
const handleLoadAllGoods = () => {
getAll().then(goods => {
setAllGoods(goods);
});
};

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
const handleLoad5FirstGoods = () => {
get5First().then(goods => {
setAllGoods(goods);
});
};

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

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

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

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

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

<GoodsList goods={allGoods} />
</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
10 changes: 8 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ 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 => {
return goods.filter(good => good.color === 'red');
});
};
Loading