Created
May 1, 2018 08:10
-
-
Save konstantin24121/d83663c4c9603b640dcbef20ebc9f5eb to your computer and use it in GitHub Desktop.
Reducer and async action
This file contains hidden or 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
import TYPES from './types'; | |
// Fetch hypothesis | |
export const fetchStart = ({ id, segment }) => ({ | |
type: TYPES.fetching, | |
payload: { | |
id, | |
segment, | |
}, | |
}); | |
// Fetch hypothesis successes, lets write data to store | |
const fetchSuccess = ({ response, segment }) => ({ | |
type: TYPES.fetchSuccess, | |
payload: { response: { ...response }, segment }, | |
apiEvent: { hypothesisId: response.id, segment, formId: response.form.id }, | |
}); | |
// Fetch hypothesis reject, lets notify about this API | |
const fetchReject = ({ id, segment, error }) => ({ | |
type: TYPES.fetchReject, | |
payload: { | |
id, | |
segment, | |
}, | |
apiEvent: { hypothesisId: id, segment, error }, | |
}); | |
// Remove old hypothesis | |
export const removeHypothesis = ({ id, segment }) => ({ | |
type: TYPES.hypothesisRemove, | |
payload: { id, segment }, | |
sync: true, | |
}); | |
// Fetch hypothesises data from server | |
export const fetchHypothesis = ({ id, segment }) => { | |
return async (dispatch, getState, { api }) => { | |
dispatch(fetchStart({ id, segment })); | |
try { | |
const requestData = { segment }; | |
if (id) requestData.id = id; | |
const response = await api.optimizer.getHypothesis(requestData); | |
dispatch(fetchSuccess({ id, response, segment })); | |
} catch (e) { | |
console.error(`Hypothesis not fetched ${id} for ${segment}`); | |
if ((id && e.message === 'NO_ENABLED_TESTS') || e.message === 'OBJECT_NOT_FOUND') { | |
dispatch(removeHypothesis({ id, segment })); | |
return; | |
} | |
dispatch(fetchReject({ id, segment, error: e.message })); | |
} | |
}; | |
}; |
This file contains hidden or 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
import { localStorageStoreSync as localStorageStoreSyncer, | |
deleteFromList, changeItemInList } from 'common/utils'; | |
import TYPES from './types'; | |
import { hypothesisScheme } from './schemes'; | |
/* initialization stuff here */ | |
export default function (state = localStorageSyncer.initialState, { type, payload }) { | |
return localStorageSyncer.sync(() => { | |
switch (type) { | |
/* other actions */ | |
case TYPES.fetching: { | |
const newHypotesisList = changeItemInList( | |
state.list, | |
(item) => (payload.id && item.id === payload.id) || item.segment === payload.segment, | |
(hypothesis) => ({ | |
...hypothesis, | |
isLoading: true, | |
}) | |
); | |
return { ...state, list: newHypotesisList }; | |
} | |
case TYPES.fetchReject: { | |
const newHypotesisList = changeItemInList( | |
state.list, | |
(item) => (payload.id && item.id === payload.id) || item.segment === payload.segment, | |
(hypothesis) => ({ | |
...hypothesis, | |
isLoading: false, | |
}) | |
); | |
return { ...state, list: newHypotesisList }; | |
} | |
case TYPES.fetchSuccess: { | |
const { id } = payload.response; | |
const newHypotesisList = changeItemInList( | |
state.list, | |
(item) => (id && item.id === id) || item.segment === payload.segment, | |
(hypothesis) => ({ | |
...hypothesis, | |
id, | |
isLoading: false, | |
isFetched: true, | |
}) | |
); | |
const newHypotesisDataList = changeItemInList( | |
state.data, | |
(item) => item.id === payload.response.id || item.segment === payload.segment, | |
(hypothesis) => ({ | |
...hypothesis, | |
...hypothesisScheme(payload.response), | |
}) | |
); | |
return { ...state, list: newHypotesisList, data: newHypotesisDataList }; | |
} | |
case TYPES.hypothesisRemove: { | |
const newHypotesisList = deleteFromList( | |
state.list, | |
({ segment, id }) => (id === payload.id || segment === payload.segment) | |
); | |
const newDataHypotesisList = deleteFromList( | |
state.data, | |
({ segment, id }) => (id === payload.id || segment === payload.segment) | |
); | |
return { ...state, list: newHypotesisList, data: newDataHypotesisList }; | |
} | |
default: | |
return state; | |
} | |
}); | |
} |
This file contains hidden or 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
export default { | |
/* other types */ | |
fetching: 'HYPOTHESIS_FETCH_START', | |
fetchSuccess: 'HYPOTHESIS_FETCH_SUCCESS', // with API callback | |
fetchReject: 'HYPOTHESIS_FETCH_REJECT', // with API callback | |
hypothesisRemove: 'HYPOTHESIS_WAS_REMOVED', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment