Skip to content

Instantly share code, notes, and snippets.

@nurpax
Created April 6, 2018 19:55
Show Gist options
  • Save nurpax/4515e710794a18cd0e214ec28b9b798b to your computer and use it in GitHub Desktop.
Save nurpax/4515e710794a18cd0e214ec28b9b798b to your computer and use it in GitHub Desktop.
// 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