Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Izzet Yusufov authored and Izzet Yusufov committed Aug 1, 2023
1 parent 42cfbd8 commit 4a00861
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 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://IzzetYusufov.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
12 changes: 11 additions & 1 deletion src/App.scss
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
// styles go here
.red--item {
color: red;
}

.blue--item {
color: blue;
}

.green--item {
color: green;
}
81 changes: 62 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,70 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { Good } from './types/Good';
import { getAll, get5First, getRedGoods } from './api/goods';

// import { getAll, get5First, getRed } from './api/goods';
// or
// import * as goodsAPI from './api/goods';
enum ServerRequest {
AllGoods = 'ALL',
FiveFirst = 'FiveFirst',
OnlyRed = 'OnlyRed',
}

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC = () => {
const [goods, setGoods] = useState<Good[]>([]);
const [
serverReqeust,
setServerReqeust,
] = useState<ServerRequest | null>(null);

<button type="button" data-cy="all-button">
Load all goods
</button>
useEffect(() => {
if (serverReqeust === ServerRequest.AllGoods) {
getAll().then((goodsRequest: Good[]) => {
setGoods(goodsRequest);
});
}

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
if (serverReqeust === ServerRequest.FiveFirst) {
get5First().then((goodsRequest: Good[]) => {
setGoods(goodsRequest);
});
}

<button type="button" data-cy="red-button">
Load red goods
</button>
if (serverReqeust === ServerRequest.OnlyRed) {
getRedGoods().then((goodsRequest: Good[]) => {
setGoods(goodsRequest);
});
}
}, [serverReqeust]);

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

<button
type="button"
data-cy="all-button"
onClick={() => setServerReqeust(ServerRequest.AllGoods)}
>
Load all goods
</button>

<button
type="button"
data-cy="first-five-button"
onClick={() => setServerReqeust(ServerRequest.FiveFirst)}
>
Load 5 first goods
</button>

<button
type="button"
data-cy="red-button"
onClick={() => setServerReqeust(ServerRequest.OnlyRed)}
>
Load red goods
</button>
{serverReqeust !== null && <GoodsList goods={goods} />}
</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"
className={`${good.color}--item`}
>
{good.name}
</li>
))}
Expand Down
5 changes: 3 additions & 2 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ 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)).slice(0, 5)); // sort and get the first 5
};

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

0 comments on commit 4a00861

Please sign in to comment.