Skip to content

Commit

Permalink
#68 Added emojis and repositories sagas.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Feb 3, 2022
1 parent b24f73d commit dff877e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/redux/modules/emojis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getEmojis } from "@/redux/api/github/getEmojis";
import { createSlice, startFetching, stopFetching } from "@/redux/utils";
import { call, put } from 'redux-saga/effects';

const initialState = {
items: {},
};

export default createSlice({
name: 'emojis',
initialState,
reducers: {
fetch: startFetching,
fetchSuccess: (state, { payload }) => {
stopFetching(state);
state.items = payload;
},

fail: (state, { payload: { message } }) => {
stopFetching(state);
state.error = message;
},
},

sagas: (actions) => ({
[actions.fetch]: {
* saga() {
try {
const { data } = yield call(getEmojis);
yield put(actions.fetchSuccess(data));
} catch (error) {
yield put(actions.fail(error));
}
},
},
}),
});
4 changes: 4 additions & 0 deletions src/redux/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { all } from 'redux-saga/effects';
import emojis from "./emojis";
import profiles from "./profiles";
import progress from "./progress";
import repositories from "./repositories";
import ui from './ui';

// Put modules that have their reducers nested in other (root) reducers here
const nestedSlices = [];

// Put modules whose reducers you want in the root tree in this array.
const rootSlices = [
emojis,
profiles,
ui,
progress,
repositories,
];

const sagas = [...rootSlices, ...nestedSlices]
Expand Down
87 changes: 87 additions & 0 deletions src/redux/modules/repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { getRepositories } from "@/redux/api/github";
import { createSlice, startFetching, stopFetching } from "@/redux/utils";
import { put, call, cancelled, delay } from "redux-saga/effects";
import slice from './progress';

const initialState = {
isFetching: false,
selected: null,
items: [],
error: null,
};

export default createSlice({
name: 'repositories',
initialState,
reducers: {
setRepository: (state, { payload }) => {
state.selected = payload;
},

fetchRepositories: startFetching,
fetchRepositoriesSuccess: (state, { payload: { data, append } }) => {
const fixed = Array.isArray(data) ? data : [];
state.items = append ? [
...state.items,
...fixed,
] : fixed;
},

stopFetching,

fail: (state, { payload: { message } }) => {
stopFetching(state);
state.error = message;
},
},

sagas: (actions) => ({
[actions.fetchRepositories]: {
* saga({ payload: { login, amount } }) {
try {
if (!amount) {
yield put(actions.stopFetching({ message: null }));
return;
}

yield put(slice.actions.change({
max: amount,
value: 0,
valueBuffer: 0,
show: true,
}));

let next = true;
let page = 0;

while (next) {
const { data, pageInfo } = yield call(getRepositories, {
owner: login,
perPage: 100,
page: page,
});

yield put(actions.fetchRepositoriesSuccess({ data, append: page > 0 }));

page = pageInfo.nextPage;
next = pageInfo.hasNextPage;

yield put(slice.actions.change({ max: pageInfo.total || amount }));
yield put(slice.actions.incValue(data.length));
yield put(slice.actions.incValueBuffer(data.length + 100));
}

yield put(actions.stopFetching());
} catch (error) {
yield put(actions.fail(error));
if (yield cancelled()) {
yield put(actions.stopFetching);
}
} finally {
yield delay(500);
yield put(slice.actions.toggle(false));
}
},
},
}),
});

0 comments on commit dff877e

Please sign in to comment.