Skip to content

Commit

Permalink
task done
Browse files Browse the repository at this point in the history
  • Loading branch information
Manankin committed Jan 29, 2024
1 parent 2a46adf commit 3421145
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ You have 3 button that should load [the goods](https://mate-academy.github.io/re

1. `Load All goods` should load and show all the `goods`;
1. `Load 5 first goods` should do the next:
- load all the goods;
- sort them by name;
- and show the first 5;
DONE - load all the goods;
DONE - sort them by name;
DONE - and show the first 5;
1. `Load red goods` should load all the goods show only `red` ones;
1. Server has only 1 endpoint returning all the goods, so you should do all the preparations in corresponding methods in `/api/goods`.
1. `GoodsList` is almost finished, you just need to use corresponding colors for `li`s;
1. DONE - Server has only 1 endpoint returning all the goods, so you should do all the preparations in corresponding methods in `/api/goods`.
1. DONE - `GoodsList` is almost finished, you just need to use corresponding colors for `li`s;

## Instructions

- 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.
DONE - Replace `<your_account>` with your Github username in the [DEMO LINK](https://Manankin.github.io/react_dynamic-list-of-goods/) and add it to the PR description.
63 changes: 46 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
import React from 'react';
import React, { useState } from 'react';
import './App.scss';
import { GoodsList } from './GoodsList';
import { Good } from './types/Good';

// import { getAll, get5First, getRed } from './api/goods';
import { getAll, get5First, getRedGoods } 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, setVisibleGoods] = useState<Good[]>([]);

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

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

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

<GoodsList goods={visibleGoods} />
</div>
);
};
8 changes: 7 additions & 1 deletion src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ 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"
style={{
color: good.color,
}}
>
{good.name}
</li>
))}
Expand Down
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((a, b) => a.name.localeCompare(b.name))
.slice(0, 5));
};

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

0 comments on commit 3421145

Please sign in to comment.