diff --git a/src/actions/cShift/reducer.actions.js b/src/actions/cShift/reducer.actions.js index e9f895f..012a246 100644 --- a/src/actions/cShift/reducer.actions.js +++ b/src/actions/cShift/reducer.actions.js @@ -1,7 +1,7 @@ import { IS_SENDING_CSHIFT_REQUEST, - SET_CSHIFT_REQUEST_ERROR, - CLEAR_CSHIFT_REQUEST_ERROR, + SET_CSHIFT_REQUEST_ERRORS, + CLEAR_CSHIFT_REQUEST_ERRORS, SET_CSHIFT_REQUEST_SUCCESS, CLEAR_CSHIFT_REQUEST_SUCCESS, SET_FORM_EMPLOYEE_UUID, @@ -14,6 +14,13 @@ import { } from "../../constants/cShift.constants"; import { parseError } from "../common.actions"; +function transformError(error) { + if (error.get_price_uuid) { + error["Price"] = error.get_price_uuid; + delete error["get_price_uuid"]; + } +} + export function isSendingCShiftRequest(sendingRequest) { return { type: IS_SENDING_CSHIFT_REQUEST, @@ -23,15 +30,25 @@ export function isSendingCShiftRequest(sendingRequest) { export function setCShiftRequestError(exception) { let error = parseError(exception); + transformError(error); return { - type: SET_CSHIFT_REQUEST_ERROR, - data: error + type: SET_CSHIFT_REQUEST_ERRORS, + data: [error] }; } +export function setCShiftRequestErrors(exceptions) { + let errors = exceptions.map(exception => parseError(exception)); + errors.forEach(error => transformError(error)); + return { + type: SET_CSHIFT_REQUEST_ERRORS, + data: errors + } +} + export function clearCShiftRequestError() { return { - type: CLEAR_CSHIFT_REQUEST_ERROR + type: CLEAR_CSHIFT_REQUEST_ERRORS }; } @@ -90,7 +107,7 @@ export function setFormShiftDates(dates) { }; } -export function setClearCshiftState() { +export function setClearCShiftState() { return { type: SET_CLEAR_CSHIFT_STATE }; diff --git a/src/actions/cShift/saga.actions.js b/src/actions/cShift/saga.actions.js new file mode 100644 index 0000000..7205cee --- /dev/null +++ b/src/actions/cShift/saga.actions.js @@ -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 + }; +} diff --git a/src/api/baseApi.js b/src/api/baseApi.js index 4332b11..0cef4eb 100644 --- a/src/api/baseApi.js +++ b/src/api/baseApi.js @@ -8,7 +8,7 @@ const localStorage = global.process && process.env.NODE_ENV === "test" require("localStorage") : global.window.localStorage; -function headers() { +export function headers() { const token = localStorage.getItem("userToken") || ""; let header = { Accept: "application/json", @@ -59,3 +59,5 @@ export function del(url) { .then(response => response.data) .catch(error => Promise.reject(error)); } + +export default apiInstance; diff --git a/src/api/cShift.api.js b/src/api/cShift.api.js new file mode 100644 index 0000000..bf1c4c5 --- /dev/null +++ b/src/api/cShift.api.js @@ -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); + }); +} diff --git a/src/components/User/Client/ClientAddShiftForm.jsx b/src/components/User/Client/ClientAddShiftForm.jsx index ef409d9..fab0295 100644 --- a/src/components/User/Client/ClientAddShiftForm.jsx +++ b/src/components/User/Client/ClientAddShiftForm.jsx @@ -9,6 +9,7 @@ import { Form, Header, Label, + Message, TextArea } from "semantic-ui-react"; @@ -18,9 +19,11 @@ import { setFormShiftStartTime, setFormShiftDuration, setFormShiftNote, - setClearCshiftState, + setClearCShiftState, setFormShiftDates } from "../../../actions/cShift/reducer.actions"; +import { createMultipleCShiftRequest } from "../../../actions/cShift/saga.actions"; +import Error from "../../Shared/Error"; import "react-datepicker/dist/react-datepicker.css"; import "./shiftStartTimeOverrides.css"; @@ -41,7 +44,7 @@ for (let min = 60; min <= 480; min += 30) { class ClientAddShiftForm extends Component { componentWillMount = () => { - this.props.dispatch(setClearCshiftState()); + this.props.dispatch(setClearCShiftState()); }; changeSelectedEmployee = (e, { value }) => { @@ -86,8 +89,36 @@ class ClientAddShiftForm extends Component { this.props.dispatch(setFormShiftDates(shiftDatesCopy)); }; + onSubmitShifts = event => { + event.preventDefault(); + // change this into interable cshift post request bodies + const { priceUUID, startTime, duration, note, shiftDates } = this.props; + const postRequestBodies = []; + for (let shiftDateString in shiftDates) { + const dynamicStartTime = utc(startTime); + const startDate = shiftDates[shiftDateString]; + dynamicStartTime.set({ + year: startDate.get("year"), + month: startDate.get("month"), + date: startDate.get("date") + }); + const dynamicEndTime = utc(dynamicStartTime); + dynamicEndTime.add(duration, "minutes"); + postRequestBodies.push({ + get_price_uuid: priceUUID, + set_start: dynamicStartTime.format(), + set_end: dynamicEndTime.format(), + description: note ? note : undefined + }); + } + this.props.dispatch(createMultipleCShiftRequest(postRequestBodies)); + }; + render() { const { + isSendingCShiftRequest, + cShiftRequestErrors, + cShiftRequestSuccess, selfUser, employeeUUID, priceUUID, @@ -101,6 +132,10 @@ class ClientAddShiftForm extends Component { return ; } + if (cShiftRequestSuccess) { + console.log(cShiftRequestSuccess); + } + const employeeChoices = selfUser.client.employees .filter(employee => !employee.deleted && !!employee.approved) .map(({ uuid, provider }) => ({ @@ -120,7 +155,7 @@ class ClientAddShiftForm extends Component { .map(({ amount, uuid, work_type }) => ({ key: uuid, value: uuid, - text: work_type.label, + text: `${work_type.label} ($${amount}/hr)`, content: (