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

new solution #2074

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
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://MaxKalalashnyk02.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@mate-academy/cypress-tools": "^1.0.5",
"@mate-academy/eslint-config-react": "^0.0.11",
"@mate-academy/eslint-config-react-typescript": "^1.0.12",
"@mate-academy/scripts": "^1.2.12",
"@mate-academy/scripts": "^1.3.2",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "^0.0.11",
"@types/node": "^17.0.23",
Expand Down
57 changes: 38 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
import React from 'react';
// App.tsx
import React, { useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { Good } from './types/Good';
import * as goodsAPI from './api/goods';

// 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 loadAllGoods = () => {
goodsAPI.getAll().then(setGoods);
};

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

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

<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={loadAllGoods}>
Load all goods
</button>

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

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

<GoodsList goods={goods} />
</div>
);
};
4 changes: 2 additions & 2 deletions src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import { Good } from './types/Good';

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

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
13 changes: 7 additions & 6 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Good } from '../types/Good';

// eslint-disable-next-line
const API_URL = `https://mate-academy.github.io/react_dynamic-list-of-goods/goods.json`;
// eslint-disable-next-line max-len
const API_URL = 'https://mate-academy.github.io/react_dynamic-list-of-goods/goods.json';

export function getAll(): Promise<Good[]> {
return fetch(API_URL)
.then(response => response.json());
}

export const get5First = () => {
export const get5First = (): Promise<Good[]> => {
return getAll()
.then(goods => goods); // sort and get the first 5
// eslint-disable-next-line max-len
.then(goods => goods.sort((a, b) => a.name.localeCompare(b.name)).slice(0, 5));
};

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