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

Open
wants to merge 2 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,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://Krivets.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
62 changes: 43 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import React from 'react';
import React, { useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { get5First, getAll, 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 [goodsList, setGoodsList] = useState<Good[]>([]);

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
const responseHandler = (getMethod: () => Promise<Good[]>) => {
getMethod().then(setGoodsList);
};

<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={() => {
responseHandler(getAll);
}}
>
Load all goods
</button>

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

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

<GoodsList goods={goodsList} />
</div>
);
};
18 changes: 13 additions & 5 deletions src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ type Props = {

export const GoodsList: React.FC<Props> = ({ goods }) => (
<ul>
{goods.map(good => (
<li key={good.id} data-cy="good">
{good.name}
</li>
))}
{goods.map(good => {
const { id, color, name } = good;

return (
<li
key={id}
data-cy="good"
style={{ color: `${color}` }}
>
{name}
</li>
);
})}
</ul>
);
6 changes: 4 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export function getAll(): Promise<Good[]> {

export const get5First = () => {
return getAll()
.then(goods => goods); // sort and get the first 5
.then(goods => goods
.sort((word1, word2) => word1.name.localeCompare(word2.name))
.slice(0, 5));
};

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