Skip to content

Commit ddcf77c

Browse files
committed
add slice
1 parent 5be785f commit ddcf77c

File tree

9 files changed

+63
-140
lines changed

9 files changed

+63
-140
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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React, { useEffect } from 'react'
22
import { useSelector, useDispatch } from 'react-redux'
33
import { useInjectReducer, useInjectSaga } from 'utils/redux-injectors'
44
import { saga } from './saga'
5-
import { key, countriesReducer } from './reducer'
6-
import { actions } from './actions'
5+
// import { key, countriesReducer } from './reducer'
6+
// import { actions } from './actions'
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'
9+
import { Link } from 'react-router-dom'
10+
import { key, reducer, actions } from './slice'
1111

1212
export const Countries = () => {
13-
useInjectReducer({ key: key, reducer: countriesReducer })
13+
useInjectReducer({ key: key, reducer: reducer })
1414
useInjectSaga({ key: key, saga })
1515

1616
const countries = useSelector(selectCountries)
@@ -23,7 +23,14 @@ export const Countries = () => {
2323
}, [dispatch])
2424

2525
return (
26-
<PageWrapper>
26+
<div
27+
style={{
28+
boxSizing: 'content-box',
29+
margin: '0 auto',
30+
padding: '0 1.5rem',
31+
width: '960px',
32+
}}
33+
>
2734
<h1>Countries</h1>
2835
{isLoading && <LoadingIndicator small />}
2936
{countries?.length > 0 && (
@@ -36,6 +43,6 @@ export const Countries = () => {
3643
</ul>
3744
)}
3845
{error && <div>{error}</div>}
39-
</PageWrapper>
46+
</div>
4047
)
4148
}

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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

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

99
export const selectLoading = createSelector(
1010
[selectDomain],
1111
countriesState => countriesState.isLoading,
12-
);
12+
)
1313

1414
export const selectError = createSelector(
1515
[selectDomain],
1616
countriesState => countriesState.error,
17-
);
17+
)
1818

1919
export const selectCountries = createSelector(
2020
[selectDomain],
2121
countriesState => countriesState.countries,
22-
);
22+
)
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)