Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create dynamic list of goods #2648

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
import React from 'react';
import './App.scss';
import { useState } from 'react';
import { GoodsList } from './GoodsList';
import { Good } from './types/Good';
import * as goodsAPI from './api/goods';

// import { getAll, get5First, getRed } from './api/goods';
// or
// import * as goodsAPI from './api/goods';
type Props = {
goods: Good[];
};

export const App: React.FC = () => (
<div className="App">
<h1>Dynamic list of Goods</h1>
export const App: React.FC<Props> = () => {
const [goods, setGoods] = 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={() => {
goodsAPI.getAll().then(setGoods);
}}
>
Load all goods
</button>

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

<GoodsList goods={[]} />
</div>
);
<button
type="button"
data-cy="red-button"
onClick={() => {
goodsAPI.getRedGoods().then(setGoods);
}}
>
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
@@ -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>
))}
15 changes: 11 additions & 4 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Good } from '../types/Good';

// eslint-disable-next-line
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(goo => goo.json());
}

export const get5First = () => {
return getAll().then(goods => goods); // sort and get the first 5
return getAll().then(goods =>
goods
.sort((element1: Good, element2: Good) =>
element1.name.localeCompare(element2.name),
)
.splice(0, 5),
);
};

export const getRedGoods = () => {
return getAll().then(goods => goods); // get only red
return getAll().then(goods =>
goods.filter(element => element.color === 'red'),
);
};
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createRoot } from 'react-dom/client';
import { App } from './App';

createRoot(document.getElementById('root') as HTMLElement).render(<App />);
createRoot(document.getElementById('root') as HTMLElement).render(
<App goods={[]} />,
);