Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeSavostikov committed Jan 20, 2024
1 parent 2a46adf commit eb4773e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
71 changes: 52 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
import React from 'react';
import React, { 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 { getAll, get5First, getRed } from './api/goods';
import { Good } from './types/Good';

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

<button type="button" data-cy="all-button">
Load all goods
</button>
function onselect(value: string) {
switch (value) {
case 'firstFive': {
get5First().then(setGoods);
break;
}

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
case 'onlyRed': {
getRed().then(setGoods);
break;
}

<button type="button" data-cy="red-button">
Load red goods
</button>
default: {
getAll().then(setGoods);
}
}
}

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

<button
type="button"
data-cy="all-button"
onClick={() => onselect('all')}
>
Load all goods
</button>

<button
type="button"
data-cy="first-five-button"
onClick={() => onselect('firstFive')}
>
Load 5 first goods
</button>

<button
type="button"
data-cy="red-button"
onClick={() => onselect('onlyRed')}
>
Load red goods
</button>

{goods !== null && <GoodsList goods={goods} />}
</div>
);
};
8 changes: 6 additions & 2 deletions src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ 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
11 changes: 6 additions & 5 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ 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 => response.json());
}

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 = () => {
export const getRed = () => {
return getAll()
.then(goods => goods); // get only red
.then(goods => goods.filter(good => good.color === 'red'));
};

0 comments on commit eb4773e

Please sign in to comment.