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 #1674

Open
wants to merge 3 commits 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
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
3 changes: 3 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
// styles go here
iframe {
display: none;
}
74 changes: 57 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
import React from 'react';
/* eslint-disable max-len */
/* eslint-disable implicit-arrow-linebreak */
import React, { useMemo, useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';

// import { getAll, get5First, getRed } from './api/goods';
import { getAll, get5First, getRedGoods } from './api/goods';
import { Good } from './types/Good';
// or
// import * as goodsAPI from './api/goods';

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC = () => {
const [preparedGoods, setPreparedGoods] = useState<Good[]>([]);
const [errorMessage, setErrorMessage] = useState('');

<button type="button" data-cy="all-button">
Load all goods
</button>
const onLoad = useMemo(() => () => getAll()
.then(setPreparedGoods)
.catch(err => setErrorMessage(err)),
[]);

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
const onLoadFirstFive = useMemo(() => () => get5First()
.then(setPreparedGoods)
.catch(err => setErrorMessage(err)),
[]);

<button type="button" data-cy="red-button">
Load red goods
</button>
const onLoadRedOnes = useMemo(() => () => getRedGoods()
.then(setPreparedGoods)
.catch(err => setErrorMessage(err)),
[]);

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

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

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

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

{errorMessage
? (
<p>{errorMessage}</p>
) : (
<GoodsList goods={preparedGoods} />
)}
</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
14 changes: 12 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ export function getAll(): Promise<Good[]> {

export const get5First = () => {
return getAll()
.then(goods => goods); // sort and get the first 5
.then(goods => {
const normalizedGoods = goods
.sort((good1, good2) => good1.name.localeCompare(good2.name))
.splice(0, 5);

return normalizedGoods;
});
};

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

return normalizedGoods;
});
};
Loading