diff --git a/src/components/TCForm/utils.js b/src/components/TCForm/utils.js index bdb33703..64a4970c 100644 --- a/src/components/TCForm/utils.js +++ b/src/components/TCForm/utils.js @@ -1,7 +1,7 @@ /** * TC Form utilty */ -import _ from "lodash"; +import _, { filter } from "lodash"; import { getSelectOptionByValue } from "utils/helpers"; import { FORM_FIELD_TYPE } from "../../constants"; @@ -91,25 +91,43 @@ export const getValidator = (fields) => { }; }; +/** + * Prepare form field value to be submitted to API + * + * @param {Object} field form field + * @param {Any} value form field value + * @returns prepared value for the field + */ +const prepareSubmitField = (field, value) => { + switch (field.type) { + case FORM_FIELD_TYPE.SELECT: + return field.isMulti + ? value?.map((option) => option.value) + : value?.value + + default: + // we have to send `null` to API to clear the value instead of `undefined` + if (_.isUndefined(value)) { + return null + } + + return value + } +} + /** * Prepare form submit data * @param {any} values form value * @param {Array} fields form fields - * @param {any} originalData original form data * @returns {any} converted submitted data */ -export const prepareSubmitData = (values, fields, originalData) => { - const data = fields.reduce((obj, item) => { +export const prepareSubmitData = (values, fields) => { + const data = fields.reduce((obj, field) => { return { ...obj, - [item.name]: - item.type === FORM_FIELD_TYPE.SELECT - ? item.isMulti - ? values[item.name]?.map((x) => x.value) - : values[item.name]?.value - : values[item.name], + [field.name]: prepareSubmitField(field, values[field.name]) }; }, {}); - return Object.assign(originalData, data); + return data; }; diff --git a/src/routes/JobForm/index.jsx b/src/routes/JobForm/index.jsx index 8f34d31f..724f98ca 100644 --- a/src/routes/JobForm/index.jsx +++ b/src/routes/JobForm/index.jsx @@ -33,9 +33,8 @@ const JobForm = ({ teamId, jobId }) => { const title = isEdit ? "Edit Job Details" : "Create Job"; const onSubmit = async (values) => { - const data = getRequestData(values); if (isEdit) { - await updateJob(data, jobId).then( + await updateJob(values, jobId).then( () => { toastr.success("Job updated successfully."); setSubmitting(false); @@ -47,7 +46,11 @@ const JobForm = ({ teamId, jobId }) => { } ); } else { - await createJob(data).then( + const createValues = { + ...values, + projectId: teamId, + } + await createJob(createValues).then( () => { toastr.success("Job created successfully."); setSubmitting(false); @@ -61,25 +64,6 @@ const JobForm = ({ teamId, jobId }) => { } }; - // as we are using `PUT` method (not `PATCH`) we have send ALL the fields - // fields which we don't send would become `null` otherwise - const getRequestData = (values) => { - return _.pick(values, [ - "projectId", - "externalId", - "description", - "title", - "startDate", - "duration", - "numPositions", - "resourceType", - "rateType", - "workload", - "skills", - "status", - ]); - }; - useEffect(() => { if (skills && job && !options) { const skillOptions = skills.map((item) => { diff --git a/src/routes/ResourceBookingForm/index.jsx b/src/routes/ResourceBookingForm/index.jsx index f351e0bb..10d2c994 100644 --- a/src/routes/ResourceBookingForm/index.jsx +++ b/src/routes/ResourceBookingForm/index.jsx @@ -65,20 +65,9 @@ const ResourceBookingDetails = ({ teamId, resourceBookingId }) => { ); }; - // as we are using `PUT` method (not `PATCH`) we have send ALL the fields - // fields which we don't send would become `null` otherwise const getRequestData = (values) => { - const data = _.pick(values, [ - "projectId", - "userId", - "jobId", - "status", - "startDate", - "endDate", - "memberRate", - "customerRate", - "rateType", - ]); + // omit read-only fields + const data = _.omit(values, ['handle', 'jobTitle']) // convert dates to the API format before sending if (data.startDate) { diff --git a/src/services/jobs.js b/src/services/jobs.js index 4cd66450..bffca01f 100644 --- a/src/services/jobs.js +++ b/src/services/jobs.js @@ -62,7 +62,7 @@ export const createJob = (data) => { export const updateJob = (data, jobId) => { return ( axios - .put(`${config.API.V5}/jobs/${jobId}`, data) + .patch(`${config.API.V5}/jobs/${jobId}`, data) // temporary fix: // after updating a job we are reloading the list of jobs // so we have to wait a bit to make sure job is indexed in the ES diff --git a/src/services/resourceBookings.js b/src/services/resourceBookings.js index a6f1f361..7f2f722d 100644 --- a/src/services/resourceBookings.js +++ b/src/services/resourceBookings.js @@ -25,7 +25,8 @@ export const getReourceBookingById = (resourceBookingId) => { export const updateReourceBooking = (data, resourceBookingId) => { return ( axios - .put(`${config.API.V5}/resourceBookings/${resourceBookingId}`, data) // temporary fix: + .patch(`${config.API.V5}/resourceBookings/${resourceBookingId}`, data) + // temporary fix: // after updating a resource booking we are reloading the list of resource bookings // so we have to wait a bit to make sure job is indexed in the ES .then((response) => delay(ES_REINDEX_DELAY).then(() => response))