Skip to content

Commit

Permalink
add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii committed Jan 29, 2024
1 parent 2a46adf commit 65e3be5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
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';
/* eslint-disable no-nested-ternary */
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 * as goodsAPI 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[]>([]);
const [mode, setMode] = useState('all');

<button type="button" data-cy="all-button">
Load all goods
</button>
useEffect(() => {
goodsAPI.getAll().then(result => setGoods(
mode === 'all' ? result
: mode === 'five'
// eslint-disable-next-line max-len
? result.sort((one, two) => one.name.localeCompare(two.name)).slice(0, 5)
: result.filter(item => item.color === 'red'),
));
}, [mode]);

<button type="button" data-cy="first-five-button">
Load 5 first goods
</button>
return (
<div className="App">
<h1>Dynamic list of Goods</h1>

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

<GoodsList goods={[]} />
</div>
);
<button
type="button"
data-cy="first-five-button"
onClick={() => setMode('five')}
>
Load 5 first goods
</button>

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

<GoodsList goods={goods} />
</div>
);
};
2 changes: 1 addition & 1 deletion src/GoodsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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
10 changes: 0 additions & 10 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,3 @@ export function getAll(): Promise<Good[]> {
return fetch(API_URL)
.then(response => response.json());
}

export const get5First = () => {
return getAll()
.then(goods => goods); // sort and get the first 5
};

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

0 comments on commit 65e3be5

Please sign in to comment.