-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
add task solution #1686
base: master
Are you sure you want to change the base?
add task solution #1686
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,65 @@ | ||
import React from 'react'; | ||
import React, { useEffect, 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 = React.memo(() => { | ||
const [goods, setGoods] = useState<Good[]>([]); | ||
const [selected5First, setSelected5First] = useState(false); | ||
const [selectedRed, setSelectedRed] = useState(false); | ||
|
||
export const App: React.FC = () => ( | ||
<div className="App"> | ||
<h1>Dynamic list of Goods</h1> | ||
useEffect(() => { | ||
if (selected5First) { | ||
get5First() | ||
.then(setGoods); | ||
} else if (selectedRed) { | ||
getRedGoods() | ||
.then(setGoods); | ||
} else { | ||
getAll() | ||
.then(setGoods); | ||
} | ||
}, [goods]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant here. Look, you can creat functions for each button and wrapped it on useCallback as Anastasia note in previous review. Or alternative You can create memoize value to prevent make request each time when you click on button, for example: |
||
|
||
<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={() => { | ||
setSelected5First(false); | ||
setSelectedRed(false); | ||
}} | ||
> | ||
Load all goods | ||
</button> | ||
|
||
<button type="button" data-cy="red-button"> | ||
Load red goods | ||
</button> | ||
<button | ||
type="button" | ||
data-cy="first-five-button" | ||
onClick={() => { | ||
setSelected5First(true); | ||
setSelectedRed(false); | ||
}} | ||
> | ||
Load 5 first goods | ||
</button> | ||
|
||
<GoodsList goods={[]} /> | ||
</div> | ||
); | ||
<button | ||
type="button" | ||
data-cy="red-button" | ||
onClick={() => { | ||
setSelected5First(false); | ||
setSelectedRed(true); | ||
}} | ||
> | ||
Load red goods | ||
</button> | ||
|
||
<GoodsList goods={goods} /> | ||
</div> | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
import { Color } from '../types/Color'; | ||
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()); | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Failed to receive data'); | ||
} | ||
|
||
return response.json(); | ||
}); | ||
} | ||
|
||
export const get5First = () => { | ||
return getAll() | ||
.then(goods => goods); // sort and get the first 5 | ||
.then(goods => goods | ||
.sort((good1, good2) => good1.name.localeCompare(good2.name)) | ||
.splice(0, 5)); | ||
}; | ||
|
||
export const getRedGoods = () => { | ||
return getAll() | ||
.then(goods => goods); // get only red | ||
.then(goods => goods.filter(good => good.color === Color.Red)); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum Color { | ||
Red = 'red', | ||
Green = 'green', | ||
Blue = 'blue', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Color } from './Color'; | ||
|
||
export interface Good { | ||
id: number; | ||
name: string; | ||
color: string; | ||
color: Color; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant states