Skip to content

Commit

Permalink
#104 added errors saga to storing the last error.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed May 30, 2023
1 parent 02de63b commit 0bfa6cc
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/redux/modules/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
fail,
} from '@/redux/utils';

import errorsSlice from './errors';

const { actions: { lastError } } = errorsSlice;

const initialState = {
selected: null,
items: [],
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion src/redux/modules/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
fail,
} from '@/redux/utils';

import errorsSlice from './errors';

const { actions: { lastError } } = errorsSlice;

const initialState = {
items: [],
};
Expand Down Expand Up @@ -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());
Expand Down
13 changes: 11 additions & 2 deletions src/redux/modules/emojis.js
Original file line number Diff line number Diff line change
@@ -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: {},
};
Expand All @@ -17,6 +21,7 @@ export default createSlice({
state.items = payload;
},

stopFetching,
fail,
},

Expand All @@ -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());
}
}
},
},
Expand Down
33 changes: 33 additions & 0 deletions src/redux/modules/errors.js
Original file line number Diff line number Diff line change
@@ -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));
},
},
}),
});
2 changes: 2 additions & 0 deletions src/redux/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,6 +17,7 @@ const rootSlices = [
branches,
commits,
emojis,
errors,
profiles,
ui,
progress,
Expand Down
32 changes: 29 additions & 3 deletions src/redux/modules/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
},
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/redux/modules/repositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 0bfa6cc

Please sign in to comment.