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)); } }
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)); } }); }; }
function normalizeCharClassList(response) { const normalized = normalize(response, [charClassListSchema]); let result; if (normalized.entities.charclasslist) { result = normalized.entities; } else { result = {}; } return result; }
const getTodo = (id) => { return request.get(`/todos/${id}`).then((response) => { return normalize(response.data, todo) }) }
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 addRecipe = (recipe) => (dispatch) => { dispatch(addRecipeStart()); return axios.post('/_store', recipe) .then((res) => normalize({ ...recipe, id: res.data.id }, schema.recipeSchema)) .then((res) => dispatch(addRecipeSuccess(res))) .catch((err) => dispatch(addRecipeFail(err))); }
const setTodos = (res) => (dispatch, getState) => { let normalized = normalize(res.todos, arrayOf(todoSchema)); dispatch(setEntities(normalized)); dispatch(setPages(Resources.TODO, res.page, normalized.result)); }
const fetchContinents = () => (dispatch) => { dispatch(requests.requestContinents()); return api.fetchContinents() .then((continents) => { dispatch(requests.receiveContinentsSuccess(normalize(continents, schema.arrayOfContinents))) }, () => { errorHandler(dispatch, requests.requestContinents().type) }); }
const toggleTodo = id => dispatch => api.toggleTodo(id).then(response => { dispatch({ type: "TOGGLE_TODO_SUCCESS", response: normalize(response, schema.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); }); }
function normalizeCharClass(response) { const normalized = normalize(response, charClassSchema); let result; if (normalized.entities.charclasses) { result = normalized.entities; } else { result = {}; } return result; }
const updateRecipe = (recipe) => (dispatch) => { dispatch(updateRecipeStart()); return axios.post('/_store', recipe) .then(() => normalize(recipe, schema.recipeSchema)) .then((res) => dispatch(updateRecipeSuccess(res))) .catch((err) => dispatch(updateRecipeFail(err))); }
const addTodo = text => dispatch => api.addTodo(text).then(response => { dispatch({ type: "ADD_TODO_SUCCESS", response: normalize(response, schema.todo) }); })
const addReviewNative = (review, image) => (dispatch) => { dispatch(requests.requestAddReview()); return addReview(review, image) .then(() => { dispatch(requests.receiveAddReviewSuccess(normalize(review, schema.review))); }, () => { errorHandler(dispatch, requests.requestAddReview().type); }); }