Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

fix: use PATCH instead of PUT for Job and RB saving #284

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/components/TCForm/utils.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
};
28 changes: 6 additions & 22 deletions src/routes/JobForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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) => {
Expand Down
15 changes: 2 additions & 13 deletions src/routes/ResourceBookingForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/services/resourceBookings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down