Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
denzzlle171 committed Oct 30, 2023
1 parent bfed414 commit 6ee7a4b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 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://denzzlle171.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
52 changes: 35 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
import React from 'react';
import React, { useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { getAll, get5First, 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 = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC = () => {
const [visibleGoods, GetVisibleGoods] = useState<Good[]>([]);

<button type="button" data-cy="all-button">
Load all goods
</button>
const getOllGoods = () => {
getAll().then((goods) => GetVisibleGoods(goods));
};

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
const getFirst5 = () => {
get5First().then((goods) => GetVisibleGoods(goods));
};

<button type="button" data-cy="red-button">
Load red goods
</button>
const getOnlyRed = () => {
getRedGoods().then((goods) => GetVisibleGoods(goods));
};

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

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

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

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

<GoodsList goods={visibleGoods} />
</div>
);
};
4 changes: 2 additions & 2 deletions src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type Props = {

export const GoodsList: React.FC<Props> = ({ goods }) => (
<ul>
{goods.map(good => (
<li key={good.id} data-cy="good">
{goods.map((good) => (
<li key={good.id} data-cy="good" style={{ color: good.color }}>
{good.name}
</li>
))}
Expand Down
21 changes: 15 additions & 6 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import { Good } from '../types/Good';
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());
return fetch(API_URL).then((response) => {
if (!response.ok) {
throw new Error(
`Server error: ${response.status} ${response.statusText}`,
);
}

return response.json();
});
}

export const get5First = () => {
return getAll()
.then(goods => goods); // sort and get the first 5
return getAll().then((goods) => goods
.sort((a, b) => {
return 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
return getAll().then((goods) => goods.filter((good) => good.color === 'red')); // get only red
};

0 comments on commit 6ee7a4b

Please sign in to comment.