-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#68 Added emojis and repositories sagas.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}, | ||
}, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}, | ||
}, | ||
}), | ||
}); |