Skip to content

Commit

Permalink
refactor pet reducer methods to async/await (#101)
Browse files Browse the repository at this point in the history
* refactor pet reducer methods to async/await

* convert reminder reducer to async-await
  • Loading branch information
ChristopherJTrent authored May 1, 2024
1 parent 865115b commit c57ad70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
47 changes: 23 additions & 24 deletions frontend/src/store/petReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,31 @@ export const fetchPet = petInfo => dispatch => {
.then(pet => dispatch(receivePet(pet)))
}

export const createPet = petInfo => dispatch => (
postPet(petInfo)
.then(res => {
if (res.ok) {
return res.json()
} else {
throw res
}
})
.then(pet => dispatch(receivePet(pet)))
.catch(err => console.error(err))
)
export const createPet = petInfo => async dispatch => {
const res = await postPet(petInfo)

export const updatePet = (petInfo, petId) => dispatch => {
if (res.ok) {
return dispatch(receivePet(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
throw res
}
}

export const updatePet = (petInfo, petId) => async dispatch => {
if (! petId) {
console.error('attempted to update a pet without an id')
//console.error('attempted to update a pet without an id')
return petInfo
}
putPet(petInfo, petId)
.then(res => {
if (res.ok) {
return res.json()
} else {
throw res
}
})
.then(pet => dispatch(receivePet(pet)))
const res = await putPet(petInfo, petId)
if (res.ok) {
return dispatch(receivePet(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
throw res
}
}

export const destroyPet = petId => dispatch => (
Expand All @@ -96,7 +94,8 @@ const petReducer = (state = {}, action) => {
switch(action.type) {
case RECEIVE_PETS:

return (action.pets.reduce((a, e)=> {

return (action.pets.reduce((a, e)=> {
a[e._id] = e
return a
}, {}))
Expand Down
45 changes: 21 additions & 24 deletions frontend/src/store/reminderReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,27 @@ export const fetchReminder = (reminderId) => dispatch => (
.then(reminder => dispatch(receiveReminder(reminder)))
.catch(err => console.error(err))
)
export const createReminder = (reminderInfo) => dispatch => (
postReminder(reminderInfo)
.then(res => {
if(res.ok) {
return res.json()
} else {
throw res
}
})
.then(reminder => dispatch(receiveReminder(reminder)))
.catch(err => console.error(err))
)
export const updateReminder = (reminderInfo) => dispatch => (
editReminder(reminderInfo)
.then(res => {
if(res.ok) {
return res.json()
} else {
throw res
}
})
.then(reminder => dispatch(receiveReminder(reminder)))
.catch(err => console.error(err))
)
export const createReminder = (reminderInfo) => async dispatch => {
const res = await postReminder(reminderInfo)

if (res.ok) {
return dispatch(receiveReminder(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
throw res
}
}
export const updateReminder = (reminderInfo) => async dispatch => {
const res = await editReminder(reminderInfo)
if (res.ok){
return dispatch(receiveReminder(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
throw res
}
}

export const destroyReminder = (reminderId) => dispatch => (
deleteReminder(reminderId)
Expand Down

0 comments on commit c57ad70

Please sign in to comment.