From b24f73d30a2bc98b4ac37c672c5bb5bd16b38eb5 Mon Sep 17 00:00:00 2001 From: Artem Zubkov Date: Tue, 29 Dec 2020 15:57:25 +0700 Subject: [PATCH] #68 Renamed field to common format. Added cancel catching. --- .../Header/components/UserStep/Body.jsx | 6 ++--- .../Header/components/UserStep/Header.jsx | 22 ++++++++++-------- src/redux/modules/profiles.js | 23 ++++++++++++++----- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/components/Header/components/UserStep/Body.jsx b/src/components/Header/components/UserStep/Body.jsx index a79a2f4..29cf83a 100644 --- a/src/components/Header/components/UserStep/Body.jsx +++ b/src/components/Header/components/UserStep/Body.jsx @@ -67,7 +67,7 @@ const Body = () => { const inputRef = useRef(); const [search, setSearch] = useState(''); const [neverChange, setNeverChange] = useState(true); - const { isFetching, searched, top } = useSelector(slice.selectors.getState); + const { isFetching, items, top } = useSelector(slice.selectors.getState); const [bodyOpen, setBodyOpen] = useUIProperty('bodyOpen'); const onChange = useCallback( @@ -126,8 +126,8 @@ const Body = () => { dense subheader={SearchHeader} > - {!searched.length && } - {(searched || []).map((user) => ( + {!items.length && } + {(items || []).map((user) => ( { - const { profile } = useSelector(slice.selectors.getState); + const { selected } = useSelector(slice.selectors.getState); + const { + avatar_url, name, login, + html_url, public_repos, blog, + } = selected || {}; return ( - + - {!profile &&
Find a user
} - {profile && ( + {!selected &&
Find a user
} + {selected && ( - {profile.name || profile.login} + {name || login} - {profile.login} + {login} - {profile.public_repos} + {public_repos} - {profile.blog && ( + {blog && ( - blog + blog )} diff --git a/src/redux/modules/profiles.js b/src/redux/modules/profiles.js index c3210db..1e9ab07 100644 --- a/src/redux/modules/profiles.js +++ b/src/redux/modules/profiles.js @@ -1,17 +1,17 @@ import { getProfile, searchAccount } from '@/redux/api/github'; import { createSlice, startFetching, stopFetching } from "@/redux/utils"; -import { call, put } from 'redux-saga/effects'; +import { call, cancelled, put } from 'redux-saga/effects'; const initialState = { isFetching: false, - profile: null, - searched: [], + selected: null, + items: [], top: [], error: null, }; -const setProfile = (state, profile) => { - state.profile = profile; +const setProfile = (state, selected) => { + state.selected = selected; }; export default createSlice({ @@ -31,7 +31,7 @@ export default createSlice({ search: startFetching, searchSuccess: (state, { payload }) => { stopFetching(state); - state.searched = Array.isArray(payload) ? payload : []; + state.items = Array.isArray(payload) ? payload : []; }, fetchTop: startFetching, fetchTopSuccess: (state, { payload }) => { @@ -39,6 +39,8 @@ export default createSlice({ state.top = Array.isArray(payload) ? payload : []; }, + stopFetching, + fail: (state, { payload: { message } }) => { stopFetching(state); state.error = message; @@ -53,6 +55,9 @@ export default createSlice({ yield put(actions.fetchProfileSuccess(data)); } catch (error) { yield put(actions.fail(error)); + if (yield cancelled()) { + yield put(actions.stopFetching()); + } } }, }, @@ -64,6 +69,9 @@ export default createSlice({ yield put(actions.searchSuccess(data)); } catch (error) { yield put(actions.fail(error)); + if (yield cancelled()) { + yield put(actions.stopFetching()); + } } }, }, @@ -75,6 +83,9 @@ export default createSlice({ yield put(actions.fetchTopSuccess(data)); } catch (error) { yield put(actions.fail(error)); + if (yield cancelled()) { + yield put(actions.stopFetching()); + } } }, },