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 toggleTodo = id => dispatch => api.toggleTodo(id).then(response => { dispatch({ type: "TOGGLE_TODO_SUCCESS", response: normalize(response, schema.todo) }); })
// Fetches a single repository from Github API. // Relies on the custom API middleware defined in ../middleware/api.js. const fetchRepo = fullName => ({ [CALL_API]: { types: [ REPO_REQUEST, REPO_SUCCESS, REPO_FAILURE ], endpoint: `repos/${fullName}`, schema: Schemas.REPO } })
// Fetches a page of starred repos by a particular user. // Relies on the custom API middleware defined in ../middleware/api.js. const fetchStarred = (login, nextPageUrl) => ({ login, [CALL_API]: { types: [ STARRED_REQUEST, STARRED_SUCCESS, STARRED_FAILURE ], endpoint: nextPageUrl, schema: Schemas.REPO_ARRAY } })
// Fetches a page of stargazers for a particular repo. // Relies on the custom API middleware defined in ../middleware/api.js. const fetchStargazers = (fullName, nextPageUrl) => ({ fullName, [CALL_API]: { types: [ STARGAZERS_REQUEST, STARGAZERS_SUCCESS, STARGAZERS_FAILURE ], endpoint: nextPageUrl, schema: Schemas.USER_ARRAY } })
function normalizeCharClassList(response) { const normalized = normalize(response, [charClassListSchema]); let result; if (normalized.entities.charclasslist) { result = normalized.entities; } else { result = {}; } return result; }
// Fetches a single user from Github API. // Relies on the custom API middleware defined in ../middleware/api.js. const fetchUser = login => ({ [CALL_API]: { types: [ USER_REQUEST, USER_SUCCESS, USER_FAILURE ], endpoint: `users/${login}`, schema: Schemas.USER } })
const fetchContinents = () => (dispatch) => { dispatch(requests.requestContinents()); return api.fetchContinents() .then((continents) => { dispatch(requests.receiveContinentsSuccess(normalize(continents, schema.arrayOfContinents))) }, () => { errorHandler(dispatch, requests.requestContinents().type) }); }
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)); } }); }; }
const handleGetArticle = _id => async dispatch => { dispatch(fetchDetailRequest()); try { const { data } = await api.getArticle(_id); const normalizedData = normalize(data, article); dispatch(addArticles(normalizedData.entities.articles)); dispatch(addUsers(normalizedData.entities.users)); dispatch(fetchDetailSuccess()); } catch (error) { dispatch(fetchDetailFailure(error)); } }
const getTodo = (id) => { return request.get(`/todos/${id}`).then((response) => { return normalize(response.data, todo) }) }
const fetchRecipe = (recipeId) => (dispatch) => { dispatch(fetchRecipeStart()); return axios.post('/_find', { id: recipeId }) .then((res) => normalize(res.data[0], schema.recipeSchema)) .then((res) => dispatch(fetchRecipeSuccess(res))) .catch((err) => dispatch(fetchRecipeFail(err))); }
const addReview = (review, image) => (dispatch) => { dispatch(requests.requestAddReview()); return api.addReview(review, image) .then(() => { dispatch(requests.receiveAddReviewSuccess(normalize(review, schema.review))); }, () => { errorHandler(dispatch, requests.requestAddReview().type); }); }
const addTodo = text => dispatch => api.addTodo(text).then(response => { dispatch({ type: "ADD_TODO_SUCCESS", response: normalize(response, schema.todo) }); })