const setUsers = (res) => (dispatch, getState) => { let normalized = normalize(res.users, arrayOf(userSchema)); dispatch(setEntities(normalized)); dispatch(setPages(Resources.USER, res.page, normalized.result)); }
export function getTopicReplies(id, offset, limit) { return (dispatch) => { return fetch(address.topicReplies(id, offset, limit)) .then(res => res.json()) .then( replyPayload => { if (replyPayload && replyPayload.replies && replyPayload.replies.length > 0) { const normalized = normalize(replyPayload.replies, arrayOf(replySchema)); dispatch(receiveReplies(normalized.entities, normalized.result)); } }); }; }
export function* fetchCharacterSaga(action) { try { const request = yield call(getApiReq, `characters/${action.payload}`); if (request.status === 200) { const normalisedData = normalize(request.body.data.results, [charactersSchema]); yield put(doFetchCharactersSuccess({ byId: normalisedData.entities.characters, ids: normalisedData.result })); } } catch(e) { // TODO: dispatch a fail action with error message console.log(e); } }
export function fetchNotifications(offset, limit, type) { return (dispatch) => { return fetch(address.notifications(offset, limit, type)) .then(res => res.json()) .then(data => { const notifications = data.notifications; const normalized = normalize(notifications, arrayOf(notificationSchema)); dispatch(receiveNotifications(normalized.entities, normalized.result)); }) .catch((error) => { return {error: error.message} }) }; }
const handleFetchUserFeed = userId => async (dispatch, getState) => { const { userFeed } = getState(); if (userFeed.isFetching) { return; } dispatch(fetchUserFeedRequest()); try { const response = await api.getUser(userId); const normalizedUser = { [response.data._id]: response.data }; dispatch(addUsers(normalizedUser)); const { data } = await api.getUserFeed(userId); const normalizedData = normalize(data, [article]); dispatch(addArticles(normalizedData.entities.articles)); dispatch(fetchUserFeedSuccess(userId, normalizedData.result)); } catch (error) { console.log(error); dispatch(fetchUserFeedFailure(error)); } }
const handleFetchFeed = () => async (dispatch, getState) => { const { feed } = getState(); if (feed.isFetching) { return; } dispatch(fetchFeedRequest()); try { const { data } = await api.getFeed(); const normalizedData = normalize(data, [article]); dispatch(addArticles(normalizedData.entities.articles)); dispatch(addUsers(normalizedData.entities.users)); dispatch(fetchFeedSuccess(normalizedData.result)); } catch (error) { console.log(error); dispatch(fetchFeedFailure(error)); } }
export function getUserReplies(username, offset, limit) { return (dispatch) => { return fetch(address.userReplies(username, offset, limit)) .then(res => res.json()) .then(data => { if (data && data.error) { return { error: data.error }; } if (data && data.replies && Array.isArray(data.replies)) { data.login = username; const normalized = normalize([data], arrayOf(userSchema)); return dispatch(receiveUsers(normalized.entities, normalized.result)); } return { error: "fetch user replies error" }; }) .catch( e => { return { error: e.message }}); } }
export function getTopicDetail(id) { return (dispatch) => { return fetch(address.topic(id)) .then(res => res.json()) .then( topicPayload => { if (topicPayload && topicPayload.topic) { topicPayload.topic.meta = topicPayload.meta; const normalized = normalize([topicPayload.topic], arrayOf(topicSchema)); dispatch(receiveTopics(normalized.entities, normalized.result)); } }); }; }
export function getUserFollowers(username, offset, limit) { return (dispatch) => { return fetch(address.userFollowers(username, offset, limit)) .then(res => res.json()) .then(data => { if (data && data.error) { return { error: data.error }; } if (data && data.followers) { data.login = username; const normalized = normalize([data], arrayOf(userSchema)); return dispatch(receiveUsers(normalized.entities, normalized.result)); } return { error: "fetch user followers error" }; }) .catch( e => { return { error: e.message }}); } }
const setTodos = (res) => (dispatch, getState) => { let normalized = normalize(res.todos, arrayOf(todoSchema)); dispatch(setEntities(normalized)); dispatch(setPages(Resources.TODO, res.page, normalized.result)); }
export function* fetchComicSaga(action) { try { const request = yield call(getApiReq, `comics/${action.payload}`); if (request.status === 200) { const normalisedData = normalize(request.body.data.results, [comicsSchema]); yield put(doFetchComicsSuccess({ byId: normalisedData.entities.comics, ids: normalisedData.result })); } } catch(e) { // TODO: Dispatch error console.log(e); } }
export function getTopics(offset, limit, type) { return (dispatch) => { return fetch(address.topics(offset, limit, type)) .then(res => res.json()) .then(data => { const topics = data.topics; const normalized = normalize(topics, arrayOf(topicSchema)); dispatch(receiveTopics(normalized.entities, normalized.result)); }) .catch((error) => { return {error: error.message} }) }; }
export function* fetchComicCharactersSaga(action) { try { const params = { comics: action.payload } const request = yield call(getApiReq, 'characters', params); if (request.status === 200) { const normalisedData = normalize(request.body.data.results, [charactersSchema]); yield put(doFetchCharactersSuccess({ byId: normalisedData.entities.characters, ids: normalisedData.result })); } } catch(e) { // TODO: dispatch a fail action with error message console.log(e); } }
export function getUserTopics(username, offset, limit) { return dispatch => { return fetch(address.userTopics(username, offset, limit)) .then(res => res.json()) .then(data => { if (data && data.error) { return { error: data.error } } if (data && data.topics && Array.isArray(data.topics)) { data.login = username; let normalized = normalize([data], arrayOf(userSchema)); return dispatch(receiveUsers(normalized.entities, normalized.result)); } return { error: "fetch user topics error" }; }) .catch( e => { return { error: e.message }}); } }
export function getMoreTopicReplies(id, offset, limit) { return dispatch => { return fetch(address.topicReplies(id, offset, limit)) .then(res => res.json()) .then( replyPayload => { if (replyPayload && replyPayload.replies && replyPayload.replies.length > 0) { const normalized = normalize(replyPayload.replies, arrayOf(replySchema)); dispatch(receiveMoreReplies(normalized.entities, normalized.result)); } }); }; }