parent
530593b721
commit
dac6daa933
9 changed files with 194 additions and 21 deletions
@ -0,0 +1,12 @@ |
||||
import { CREATE_MULTIPLE_CSHIFT_REQUEST } from "../../constants/cShift.constants"; |
||||
|
||||
/** |
||||
* Saga action for creating a list of shifts |
||||
* @param postBody[] array of post body for cshift post endpoint |
||||
*/ |
||||
export function createMultipleCShiftRequest(postBodies) { |
||||
return { |
||||
type: CREATE_MULTIPLE_CSHIFT_REQUEST, |
||||
data: postBodies |
||||
}; |
||||
} |
@ -0,0 +1,19 @@ |
||||
import { post } from "./baseApi"; |
||||
|
||||
export function createCShifts(postBodies) { |
||||
const cShiftUrl = `/cshift/`; |
||||
// return Promise.all(
|
||||
// postBodies.map(postBody => post(cShiftUrl, postBody).catch(() => false))
|
||||
// ).then(postResponses => {
|
||||
// Promise.resolve(postResponses);
|
||||
// });
|
||||
return Promise.all( |
||||
postBodies.map(postBody => |
||||
post(cShiftUrl, postBody).catch(err => { |
||||
return { error: true, ...err }; |
||||
}) |
||||
) |
||||
).then(resp => { |
||||
return Promise.resolve(resp); |
||||
}); |
||||
} |
@ -0,0 +1,61 @@ |
||||
import { effects } from "redux-saga"; |
||||
import { |
||||
isSendingCShiftRequest, |
||||
setCShiftRequestError, |
||||
setCShiftRequestErrors, |
||||
setCShiftRequestSuccess, |
||||
clearCShiftRequestError, |
||||
clearCShiftRequestSuccess, |
||||
setFormEmployeeUUID, |
||||
setFormPriceUUID, |
||||
setFormShiftDates, |
||||
setFormShiftDuration, |
||||
setFormShiftNote, |
||||
setFormShiftStartTime |
||||
} from "../actions/cShift/reducer.actions"; |
||||
import { createCShifts } from "../api/cShift.api"; |
||||
|
||||
function* createCShiftsCall(postBodies) { |
||||
yield effects.put(isSendingCShiftRequest(true)); |
||||
try { |
||||
if (postBodies.length > 0) { |
||||
return yield effects.call(createCShifts, postBodies); |
||||
} else { |
||||
yield effects.put( |
||||
setCShiftRequestError({ |
||||
response: { data: { dates: ["No dates selected."] } } |
||||
}) |
||||
); |
||||
return false; |
||||
} |
||||
} catch (exception) { |
||||
yield effects.put(setCShiftRequestError(exception)); |
||||
return false; |
||||
} finally { |
||||
yield effects.put(isSendingCShiftRequest(false)); |
||||
} |
||||
} |
||||
|
||||
export function* createCShiftsFlow(request) { |
||||
yield effects.put(clearCShiftRequestSuccess()); |
||||
yield effects.put(clearCShiftRequestError()); |
||||
const arrResponses = yield effects.call(createCShiftsCall, request.data); |
||||
console.log(arrResponses); |
||||
if (arrResponses) { |
||||
const errorResps = arrResponses.filter(resp => !!resp.error); |
||||
if (errorResps.length > 0) { |
||||
console.log(arrResponses); |
||||
yield effects.put(setCShiftRequestErrors(errorResps)); |
||||
} |
||||
const succResps = arrResponses.filter(resp => !resp.error); |
||||
if (succResps.length > 0) { |
||||
yield effects.put(setCShiftRequestSuccess(succResps)); |
||||
yield effects.put(setFormEmployeeUUID("")); |
||||
yield effects.put(setFormPriceUUID("")); |
||||
yield effects.put(setFormShiftStartTime(null)); |
||||
yield effects.put(setFormShiftDuration("")); |
||||
yield effects.put(setFormShiftNote("")); |
||||
yield effects.put(setFormShiftDates({})); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue