Created
April 6, 2018 19:55
-
-
Save nurpax/4515e710794a18cd0e214ec28b9b798b to your computer and use it in GitHub Desktop.
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
// Diary action creators | |
export class Diary { | |
static RECEIVE = 'DIARY_RECEIVE' | |
static actions = { | |
fetchByDate: (date) => { | |
return function (dispatch, getState) { | |
const urls = [ | |
'/api/food', | |
'/api/recipe', | |
'/api/diary' | |
] | |
const params = [ | |
{ method: 'GET' }, | |
{ method: 'GET' }, | |
{ method: 'POST', body: { date } } | |
] | |
return fetchWithAuth(dispatch, getState, urls, params, function (json) { | |
dispatch(makeAction(Food.RECEIVE_LIST, json[0])) | |
dispatch(makeAction(Recipe.RECEIVE_LIST, json[1])) | |
dispatch(makeAction(Diary.RECEIVE, json[2])) | |
dispatch(replace(`/diary/${json[2].id.toString()}`)) | |
}) | |
} | |
}, | |
update: (diary) => { | |
return post(`/api/diary/${diary.id}`, Diary.RECEIVE, diary) | |
}, | |
// Fetch a diary + all other app state (foods, recipes) | |
fetchById (logId) { | |
return function (dispatch, getState) { | |
const urls = [ | |
'/api/food', | |
'/api/recipe', | |
`/api/diary/${logId}` | |
] | |
const params = urls.map(elt => { | |
return { method: 'GET' } | |
}) | |
return fetchWithAuth(dispatch, getState, urls, params, function (json) { | |
dispatch(makeAction(Food.RECEIVE_LIST, json[0])) | |
dispatch(makeAction(Recipe.RECEIVE_LIST, json[1])) | |
dispatch(makeAction(Diary.RECEIVE, json[2])) | |
}) | |
} | |
} | |
} | |
static bindDispatch (dispatch) { | |
return bindActionCreators(Diary.actions, dispatch) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment