import { effects } from "redux-saga"; import { isSendingPShiftRequest, setPShiftRequestError, setPShiftRequestSuccess, clearPShiftRequestError, clearPShiftRequestSuccess } from "../actions/pShift/reducer.actions"; import { getPShifts, getPShift, updatePShift } from "../api/pShift.api"; import { getPShiftsRequest, getPShiftRequest } from "../actions/pShift/saga.actions"; function* getPShiftsCall(params) { yield effects.put(isSendingPShiftRequest(true)); try { return yield effects.call(getPShifts, params); } catch (exception) { yield effects.put(setPShiftRequestError(exception)); return false; } finally { yield effects.put(isSendingPShiftRequest(false)); } } function* getPShiftCall(payload) { yield effects.put(isSendingPShiftRequest(true)); try { return yield effects.call(getPShift, payload.uuid); } catch (exception) { yield effects.put(setPShiftRequestError(exception)); return false; } finally { yield effects.put(isSendingPShiftRequest(false)); } } function* updatePShiftCall(payload) { yield effects.put(isSendingPShiftRequest(true)); try { return yield effects.call(updatePShift, payload.uuid, payload); } catch (exception) { yield effects.put(setPShiftRequestError(exception)); return false; } finally { yield effects.put(isSendingPShiftRequest(false)); } } export function* getPShiftsFlow(request) { yield effects.put(clearPShiftRequestSuccess()); yield effects.put(clearPShiftRequestError()); const wasSuccessful = yield effects.call(getPShiftsCall, request.data); if (wasSuccessful) { yield effects.put(setPShiftRequestSuccess(wasSuccessful)); } } export function* getPShiftFlow(request) { yield effects.put(clearPShiftRequestSuccess()); yield effects.put(clearPShiftRequestError()); const wasSuccessful = yield effects.call(getPShiftCall, request.data); if (wasSuccessful) { yield effects.put(setPShiftRequestSuccess(wasSuccessful)); } } export function* updatePShiftFlow(request) { if (!request.data.single) { yield effects.put(clearPShiftRequestSuccess()); } yield effects.put(clearPShiftRequestError()); const wasSuccessful = yield effects.call(updatePShiftCall, request.data); if (wasSuccessful) { yield effects.put(setPShiftRequestSuccess(wasSuccessful)); if (request.data.single) { yield effects.put(getPShiftRequest({ uuid: request.data.uuid })); } else { yield effects.put(getPShiftsRequest({ page: 1 })); } } }