Created
May 17, 2022 15:22
-
-
Save jth0024/bba85ae2d810008ba484a9387ca9aea2 to your computer and use it in GitHub Desktop.
A component that accepts dependencies as a prop to pass to its "component hook"
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
import React from 'react'; | |
import { charactersApi } from '../apis'; | |
import { useCharacterList, UseCharacterListDeps } from './useCharacterList'; | |
import { | |
Button, | |
Wrapper, | |
List, | |
ListItem, | |
ListItemInfo, | |
NameWrapper, | |
IsJediWrapper, | |
HeaderTypography, | |
Spinner, | |
} from './styles'; | |
interface CharacterListProps { | |
deps?: UseCharacterListDeps | |
} | |
const defaultDeps = { | |
charactersApi, | |
} | |
export const CharacterList = ({ deps = defaultDeps }) => { | |
const [data, handlers] = useCharacterList(deps); | |
const { characters, loading } = data; | |
const { onRefresh } = handlers; | |
return ( | |
<Wrapper> | |
Star Wars Characters | |
<Button onClick={onRefresh}>Refresh List</Button> | |
{loading ? <Spinner /> : ( | |
<List> | |
<ListItem> | |
<NameWrapper> | |
<HeaderTypography> | |
Name | |
</HeaderTypography> | |
</NameWrapper> | |
<ListItemInfo> | |
<HeaderTypography> | |
Age | |
</HeaderTypography> | |
</ListItemInfo> | |
<IsJediWrapper> | |
<HeaderTypography> | |
isJedi | |
</HeaderTypography> | |
</IsJediWrapper> | |
</ListItem> | |
{characters.map((c) => ( | |
<ListItem key={c.name}> | |
<NameWrapper> | |
{c.name} | |
</NameWrapper> | |
<ListItemInfo> | |
{c.age} | |
</ListItemInfo> | |
<IsJediWrapper> | |
{c.isJedi ? 'True' : 'False'} | |
</IsJediWrapper> | |
</ListItem> | |
))} | |
</List> | |
)} | |
</Wrapper> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment