This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make example more detailed
- Loading branch information
Шараев Айнур Раильевич
committed
Jun 19, 2021
1 parent
df1a363
commit 58a458e
Showing
4 changed files
with
64 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,34 @@ | ||
import React from 'react'; | ||
import { Button, ListContainer, ListItem, Loader, Text } from 'ui'; | ||
import { ListContainer, ListItem } from 'ui'; | ||
import { StateCallback } from 'lib/playground'; | ||
|
||
export const loadUsers = async ({ value, change }: StateCallback) => { | ||
change({ ...value, loading: true }); | ||
|
||
const response = await fetch('https://jsonplaceholder.typicode.com/users').then( | ||
(response) => response.json() as Promise<User[]>, | ||
); | ||
setTimeout(() => { | ||
change({ | ||
data: response, | ||
loading: false, | ||
}); | ||
}, 1500); | ||
}; | ||
|
||
interface User { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
interface ExampleState { | ||
loading: boolean; | ||
data: User[] | null; | ||
interface UsersListProps { | ||
data: User[]; | ||
} | ||
|
||
const initialState: ExampleState = { | ||
loading: false, | ||
data: null, | ||
}; | ||
|
||
export const ExampleDataFetching = () => { | ||
const [{ loading, data }, setExampleState] = React.useState<ExampleState>(initialState); | ||
|
||
const handleClick = () => { | ||
const loadUsers = async () => { | ||
setExampleState((current) => ({ ...current, loading: true })); | ||
|
||
const response = await fetch('https://jsonplaceholder.typicode.com/users').then( | ||
(response) => response.json() as Promise<User[]>, | ||
); | ||
setTimeout(() => { | ||
setExampleState({ | ||
data: response, | ||
loading: false, | ||
}); | ||
}, 1500); | ||
}; | ||
|
||
loadUsers(); | ||
}; | ||
|
||
const reset = () => { | ||
setExampleState(initialState); | ||
}; | ||
|
||
if (loading) { | ||
return ( | ||
<div style={{ width: 300 }}> | ||
<Loader | ||
variant="primary" | ||
description={<Text weight="medium">Fetching users, please wait</Text>} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
if (!data) { | ||
return <Button variant="primary" text="Fetch users" onClick={handleClick} />; | ||
} | ||
|
||
return ( | ||
<> | ||
<ListContainer variant="primary" style={{ width: '100%' }}> | ||
{data.map(({ id, name }) => ( | ||
<ListItem key={id} as="li" text={name} /> | ||
))} | ||
</ListContainer> | ||
<Button text="Reset" onClick={reset} /> | ||
</> | ||
); | ||
}; | ||
export const UsersList = ({ data }: UsersListProps) => ( | ||
<ListContainer variant="primary" style={{ width: '100%' }}> | ||
{data.map(({ id, name }) => ( | ||
<ListItem key={id} as="li" text={name} /> | ||
))} | ||
</ListContainer> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters