|
1 | | -import React, { useEffect } from 'react'; |
2 | | -import styled from 'styled-components/macro'; |
3 | | -import { useSelector, useDispatch } from 'react-redux'; |
4 | | -import { useInjectReducer, useInjectSaga } from 'utils/redux-injectors'; |
5 | | -import { saga } from './saga'; |
6 | | -import { key, countriesReducer } from './reducer'; |
7 | | -import { actions } from './actions'; |
8 | | -import { selectCountries, selectLoading, selectError } from './selectors'; |
9 | | -import { LoadingIndicator } from 'app/components/LoadingIndicator'; |
10 | | -import { Link } from 'app/components/Link'; |
11 | | -import { PageWrapper } from 'app/components/PageWrapper'; |
| 1 | +import React, { useEffect } from 'react' |
| 2 | +import { useSelector, useDispatch } from 'react-redux' |
| 3 | +import { useInjectReducer, useInjectSaga } from 'utils/redux-injectors' |
| 4 | +import { saga } from './saga' |
| 5 | +import { key, countriesReducer } from './reducer' |
| 6 | +import { actions } from './actions' |
| 7 | +import { selectCountries, selectLoading, selectError } from './selectors' |
| 8 | +import { LoadingIndicator } from 'app/components/LoadingIndicator' |
| 9 | +import { Link } from 'app/components/Link' |
| 10 | +import { PageWrapper } from 'app/components/PageWrapper' |
12 | 11 |
|
13 | | -export function Countries() { |
14 | | - useInjectReducer({ key: key, reducer: countriesReducer }); |
15 | | - useInjectSaga({ key: key, saga }); |
| 12 | +export const Countries = () => { |
| 13 | + useInjectReducer({ key: key, reducer: countriesReducer }) |
| 14 | + useInjectSaga({ key: key, saga }) |
16 | 15 |
|
17 | | - const countries = useSelector(selectCountries); |
18 | | - const isLoading = useSelector(selectLoading); |
19 | | - const error = useSelector(selectError); |
| 16 | + const countries = useSelector(selectCountries) |
| 17 | + const isLoading = useSelector(selectLoading) |
| 18 | + const error = useSelector(selectError) |
20 | 19 |
|
21 | | - const dispatch = useDispatch(); |
| 20 | + const dispatch = useDispatch() |
22 | 21 | useEffect(() => { |
23 | | - dispatch(actions.fetchCountries()); |
24 | | - }, [dispatch]); |
| 22 | + dispatch(actions.fetchCountries()) |
| 23 | + }, [dispatch]) |
25 | 24 |
|
26 | 25 | return ( |
27 | 26 | <PageWrapper> |
28 | 27 | <h1>Countries</h1> |
29 | 28 | {isLoading && <LoadingIndicator small />} |
30 | | - {countries?.length > 0 ? ( |
31 | | - <List> |
| 29 | + {countries?.length > 0 && ( |
| 30 | + <ul> |
32 | 31 | {countries.map(country => ( |
33 | | - <Country key={country.id}> |
| 32 | + <li key={country.id}> |
34 | 33 | <Link to={`/country/${country.id}`}>{country.name}</Link> |
35 | | - </Country> |
| 34 | + </li> |
36 | 35 | ))} |
37 | | - </List> |
38 | | - ) : error ? ( |
39 | | - <ErrorText>{error}</ErrorText> |
40 | | - ) : null} |
| 36 | + </ul> |
| 37 | + )} |
| 38 | + {error && <div>{error}</div>} |
41 | 39 | </PageWrapper> |
42 | | - ); |
| 40 | + ) |
43 | 41 | } |
44 | | - |
45 | | -const Country = styled.li` |
46 | | - color: blue; |
47 | | -`; |
48 | | - |
49 | | -const ErrorText = styled.span` |
50 | | - color: ${p => p.theme.text}; |
51 | | -`; |
52 | | - |
53 | | -const List = styled.div``; |
0 commit comments