Skip to content

Commit 9b59148

Browse files
committed
add slice
commented code
1 parent 5be785f commit 9b59148

File tree

9 files changed

+61
-141
lines changed

9 files changed

+61
-141
lines changed

.eslintcache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/app/components/Link/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/app/components/Link/__tests__/index.test.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/app/containers/Countries/actions.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/app/containers/Countries/index.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import React, { useEffect } from 'react'
22
import { useSelector, useDispatch } from 'react-redux'
3+
import { Link } from 'react-router-dom'
34
import { useInjectReducer, useInjectSaga } from 'utils/redux-injectors'
45
import { saga } from './saga'
5-
import { key, countriesReducer } from './reducer'
6-
import { actions } from './actions'
6+
import { key, reducer, actions } from './slice'
77
import { selectCountries, selectLoading, selectError } from './selectors'
88
import { LoadingIndicator } from 'app/components/LoadingIndicator'
9-
import { Link } from 'app/components/Link'
10-
import { PageWrapper } from 'app/components/PageWrapper'
119

1210
export const Countries = () => {
13-
useInjectReducer({ key: key, reducer: countriesReducer })
11+
useInjectReducer({ key: key, reducer: reducer })
1412
useInjectSaga({ key: key, saga })
1513

1614
const countries = useSelector(selectCountries)
@@ -23,7 +21,14 @@ export const Countries = () => {
2321
}, [dispatch])
2422

2523
return (
26-
<PageWrapper>
24+
<div
25+
style={{
26+
boxSizing: 'content-box',
27+
margin: '0 auto',
28+
padding: '0 1.5rem',
29+
width: '960px',
30+
}}
31+
>
2732
<h1>Countries</h1>
2833
{isLoading && <LoadingIndicator small />}
2934
{countries?.length > 0 && (
@@ -36,6 +41,6 @@ export const Countries = () => {
3641
</ul>
3742
)}
3843
{error && <div>{error}</div>}
39-
</PageWrapper>
44+
</div>
4045
)
4146
}

src/app/containers/Countries/reducer.tsx

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { call, put, takeLatest } from 'redux-saga/effects';
2-
import { request } from 'utils/request';
3-
import { actions } from './actions';
1+
import { call, put, takeLatest } from 'redux-saga/effects'
2+
import { request } from 'utils/request'
3+
import { actions } from './slice'
44

55
export function* fetchCountries() {
6-
const requestURL = `https://api.carerev.com/api/v1/countries`;
6+
const requestURL = `https://api.carerev.com/api/v1/countries`
77

88
try {
9-
const { countries } = yield call(request, requestURL);
9+
const { countries } = yield call(request, requestURL)
1010

1111
if (countries?.length > 0) {
12-
yield put(actions.fetchCountriesSuccess(countries));
12+
yield put(actions.fetchCountriesSuccess(countries))
1313
} else {
14-
yield put(actions.fetchCountriesError('No countries found.'));
14+
yield put(actions.fetchCountriesError('No countries found.'))
1515
}
16-
} catch (err) {
17-
yield put(actions.fetchCountriesError(err.toString()));
16+
} catch (err: any) {
17+
yield put(actions.fetchCountriesError(err.toString()))
1818
}
1919
}
2020

2121
/**
2222
* Root saga manages watcher lifecycle
2323
*/
2424
export function* saga() {
25-
yield takeLatest(actions.fetchCountries.type, fetchCountries);
25+
yield takeLatest(actions.fetchCountries.type, fetchCountries)
2626
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import { createSelector } from '@reduxjs/toolkit';
1+
import { createSelector } from '@reduxjs/toolkit'
22

3-
import { RootState } from 'types';
4-
import { initialState } from './reducer';
3+
import { RootState } from 'types'
4+
import { initialState } from './slice'
55

6-
// First select the relevant part from the state
7-
const selectDomain = (state: RootState) => state.countries || initialState;
6+
const selectDomain = (state: RootState) => state.countries || initialState
87

98
export const selectLoading = createSelector(
109
[selectDomain],
1110
countriesState => countriesState.isLoading,
12-
);
11+
)
1312

1413
export const selectError = createSelector(
1514
[selectDomain],
1615
countriesState => countriesState.error,
17-
);
16+
)
1817

1918
export const selectCountries = createSelector(
2019
[selectDomain],
2120
countriesState => countriesState.countries,
22-
);
21+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createSlice } from '@reduxjs/toolkit'
2+
import { CountriesState } from './types'
3+
4+
const initialState: CountriesState = {
5+
countries: [],
6+
error: undefined,
7+
isLoading: false,
8+
}
9+
10+
const key = 'countries'
11+
const slice = createSlice({
12+
name: key,
13+
initialState,
14+
reducers: {
15+
fetchCountries: state => {
16+
state.isLoading = true
17+
},
18+
fetchCountriesSuccess: (state, { payload }) => {
19+
state.isLoading = false
20+
state.countries = payload
21+
},
22+
fetchCountriesError: (state, { payload: error }) => {
23+
state.isLoading = false
24+
state.error = error
25+
},
26+
},
27+
})
28+
29+
const { actions, reducer } = slice
30+
31+
export { actions, reducer, initialState, key }

0 commit comments

Comments
 (0)