From 0bfa6cc05707d9ed8245985e23bd2e645fbc0dce Mon Sep 17 00:00:00 2001 From: Artem Zubkov Date: Tue, 30 May 2023 18:59:42 +0700 Subject: [PATCH] #104 added errors saga to storing the last error. --- src/redux/modules/branches.js | 6 +++++- src/redux/modules/commits.js | 6 +++++- src/redux/modules/emojis.js | 13 ++++++++++-- src/redux/modules/errors.js | 33 +++++++++++++++++++++++++++++++ src/redux/modules/index.js | 2 ++ src/redux/modules/profiles.js | 32 +++++++++++++++++++++++++++--- src/redux/modules/repositories.js | 5 ++++- 7 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 src/redux/modules/errors.js diff --git a/src/redux/modules/branches.js b/src/redux/modules/branches.js index a91c8f0..4f89d2e 100644 --- a/src/redux/modules/branches.js +++ b/src/redux/modules/branches.js @@ -10,6 +10,10 @@ import { fail, } from '@/redux/utils'; +import errorsSlice from './errors'; + +const { actions: { lastError } } = errorsSlice; + const initialState = { selected: null, items: [], @@ -72,7 +76,7 @@ export default createSlice({ yield put(actions.stopFetching()); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching()); diff --git a/src/redux/modules/commits.js b/src/redux/modules/commits.js index a582469..974a8f6 100644 --- a/src/redux/modules/commits.js +++ b/src/redux/modules/commits.js @@ -10,6 +10,10 @@ import { fail, } from '@/redux/utils'; +import errorsSlice from './errors'; + +const { actions: { lastError } } = errorsSlice; + const initialState = { items: [], }; @@ -76,7 +80,7 @@ export default createSlice({ yield put(actions.stopFetching()); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching()); diff --git a/src/redux/modules/emojis.js b/src/redux/modules/emojis.js index 3ac2cdc..cabf9d5 100644 --- a/src/redux/modules/emojis.js +++ b/src/redux/modules/emojis.js @@ -1,8 +1,12 @@ -import { call, put } from 'redux-saga/effects'; +import { call, put, cancelled } from 'redux-saga/effects'; import { getEmojis } from '@/redux/api/github/getEmojis'; import { createSlice, startFetching, stopFetching, fail } from '@/redux/utils'; +import errorsSlice from './errors'; + +const { actions: { lastError } } = errorsSlice; + const initialState = { items: {}, }; @@ -17,6 +21,7 @@ export default createSlice({ state.items = payload; }, + stopFetching, fail, }, @@ -27,7 +32,11 @@ export default createSlice({ const { data } = yield call(getEmojis); yield put(actions.fetchSuccess(data)); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); + } finally { + if (yield cancelled()) { + yield put(actions.stopFetching()); + } } }, }, diff --git a/src/redux/modules/errors.js b/src/redux/modules/errors.js new file mode 100644 index 0000000..d9bb38c --- /dev/null +++ b/src/redux/modules/errors.js @@ -0,0 +1,33 @@ +import { put, takeLatest } from 'redux-saga/effects'; + +import { createSlice, fail } from '@/redux/utils'; + +const initialState = { + error: null, + errorStatus: null, +}; + +export default createSlice({ + name: 'errors', + initialState, + reducers: { + lastError: () => {}, + + fail, + }, + + sagas: (actions) => ({ + [actions.lastError]: { + taker: takeLatest, + * saga({ payload: effect }) { + yield put(actions.clear()); + + const { payload } = effect; + + yield put(effect); + + yield put(actions.fail(payload)); + }, + }, + }), +}); diff --git a/src/redux/modules/index.js b/src/redux/modules/index.js index 7bb229f..c69f831 100644 --- a/src/redux/modules/index.js +++ b/src/redux/modules/index.js @@ -3,6 +3,7 @@ import { all } from 'redux-saga/effects'; import branches from './branches'; import commits from './commits'; import emojis from './emojis'; +import errors from './errors'; import profiles from './profiles'; import progress from './progress'; import repositories from './repositories'; @@ -16,6 +17,7 @@ const rootSlices = [ branches, commits, emojis, + errors, profiles, ui, progress, diff --git a/src/redux/modules/profiles.js b/src/redux/modules/profiles.js index d7fde5b..8523647 100644 --- a/src/redux/modules/profiles.js +++ b/src/redux/modules/profiles.js @@ -3,9 +3,14 @@ import { call, cancelled, put } from 'redux-saga/effects'; import { getProfile, searchAccount } from '@/redux/api/github'; import { createSlice, startFetching, stopFetching, fail } from '@/redux/utils'; +import errorsSlice from './errors'; + +const { actions: { lastError } } = errorsSlice; + const initialState = { isFetching: false, selected: null, + authenticated: null, items: [], top: [], error: null, @@ -25,6 +30,12 @@ export default createSlice({ setSelected(state, payload); }, + fetchAuthenticated: startFetching, + fetchAuthenticatedSuccess: (state, { payload }) => { + stopFetching(state); + state.authenticated = payload; + }, + setSelected: (state, { payload }) => { setSelected(state, payload); }, @@ -51,7 +62,22 @@ export default createSlice({ const { data } = yield call(getProfile, payload); yield put(actions.fetchProfileSuccess(data)); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); + } finally { + if (yield cancelled()) { + yield put(actions.stopFetching()); + } + } + }, + }, + + [actions.fetchAuthenticated]: { + * saga() { + try { + const { data } = yield call(getProfile); + yield put(actions.fetchAuthenticatedSuccess(data)); + } catch (error) { + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching()); @@ -66,7 +92,7 @@ export default createSlice({ const { data } = yield call(searchAccount, payload); yield put(actions.searchSuccess(data)); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching()); @@ -81,7 +107,7 @@ export default createSlice({ const { data } = yield call(searchAccount, 'followers:>1000'); yield put(actions.fetchTopSuccess(data)); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching()); diff --git a/src/redux/modules/repositories.js b/src/redux/modules/repositories.js index 7a753ca..2c1661a 100644 --- a/src/redux/modules/repositories.js +++ b/src/redux/modules/repositories.js @@ -9,8 +9,11 @@ import { fail, } from '@/redux/utils'; +import errorsSlice from './errors'; import slice from './progress'; +const { actions: { lastError } } = errorsSlice; + const initialState = { isFetching: false, selected: null, @@ -73,7 +76,7 @@ export default createSlice({ yield put(actions.stopFetching()); } catch (error) { - yield put(actions.fail(error)); + yield put(lastError(actions.fail(error))); } finally { if (yield cancelled()) { yield put(actions.stopFetching());