Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiYelieva committed Aug 1, 2023
1 parent 42cfbd8 commit 5625918
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 67 deletions.
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://AndriiYelieva.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
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
92 changes: 73 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,81 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import './App.scss';

import { GoodsList } from './GoodsList';

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

export const App: React.FC = () => {
const [goods, setGoods] = useState<Good[]>([]);
const [filter, setFilter] = useState(Status.ALL);

useEffect(() => {
getAll().then(setGoods);
}, []);

const handleButtonClick = (status: Status): void => {
if (status === filter) {
return;
}

setFilter(status);

switch (status) {
case Status.ALL:
getAll().then(setGoods);
break;

case Status.FIRST5:
get5First().then(setGoods);
break;

case Status.RED:
getRedGoods().then(setGoods);
break;

default:
getAll().then(setGoods);
}
};

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
return (
<div className="App">
<h1>Dynamic list of Goods</h1>

<button type="button" data-cy="all-button">
Load all goods
</button>
<button
type="button"
data-cy="all-button"
onClick={() => {
handleButtonClick(Status.ALL);
}}
>
Load all goods
</button>

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
<button
type="button"
data-cy="first-five-button"
onClick={() => handleButtonClick(Status.FIRST5)}
>
Load 5 first goods
</button>

<button type="button" data-cy="red-button">
Load red goods
</button>
<button
type="button"
data-cy="red-button"
onClick={() => handleButtonClick(Status.RED)}
>
Load red goods
</button>

<GoodsList goods={[]} />
</div>
);
<GoodsList goods={goods} />
</div>
);
};
8 changes: 4 additions & 4 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export function getAll(): Promise<Good[]> {
.then(response => response.json());
}

export const get5First = () => {
export const get5First = (): Promise<Good[]> => {
return getAll()
.then(goods => goods); // sort and get the first 5
.then(goods => goods.slice(0, 5)); // sort and get the first 5
};

export const getRedGoods = () => {
export const getRedGoods = (): Promise<Good[]> => {
return getAll()
.then(goods => goods); // get only red
.then(goods => goods.filter(item => item.color === 'red')); // get only red
};
5 changes: 5 additions & 0 deletions src/service/Status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Status {
ALL = 'All',
RED = 'Red',
FIRST5 = 'First5',
}

0 comments on commit 5625918

Please sign in to comment.