Skip to content

Commit

Permalink
fixed the suggestion and correction forms
Browse files Browse the repository at this point in the history
  • Loading branch information
kurikurichan committed Apr 23, 2024
1 parent 68b6d8e commit 6e22fa9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import { withFormik } from "formik";
import PropTypes from "prop-types";
import * as suggestionService from "services/suggestion-service";
import * as Yup from "yup";
import { DEFAULT_STAKEHOLDER } from "../../../../constants/stakeholder";

function SuggestionDialog(props) {
function CorrectionDialog(props) {
const { values, touched, errors, handleChange, handleBlur, handleSubmit } =
props;

Expand Down Expand Up @@ -138,7 +137,17 @@ const validationsForm = {
.required("Please enter corrections"),
};

const SuggestionForm = withFormik({
// formats the categories and hours data to an ugly stringify blob to pass validation on backend.
// corrections and suggestions share the same data schema, so this is so it matches
// the suggestion's schema format.
// @TODO maybe clean this up later
const formatArrayToString = (data) => {
return data.reduce((accl, ele) => {
return (accl += JSON.stringify(ele));
}, "");
};

const CorrectionForm = withFormik({
mapPropsToValues: ({ notes, tipsterName, tipsterPhone, tipsterEmail }) => ({
notes: notes || "",
tipsterName: tipsterName || "",
Expand All @@ -147,9 +156,27 @@ const SuggestionForm = withFormik({
}),
validationSchema: Yup.object(validationsForm),
handleSubmit: (values, formikBag) => {
// cherry pick the data we need for backend validation
const org = formikBag.props.stakeholder;
const orgDetails = {
stakeholderId: org.id,
adminNotes: org.adminNotes || "",
suggestionStatusId: null,
name: org.name,
address1: org.address1,
address2: org.address2,
city: org.city,
state: org.state,
zip: org.zip,
phone: org.phone,
email: org.email,
hours: formatArrayToString(org.hours),
category: formatArrayToString(org.categories),
tenantId: org.tenantId,
};

const stakeholder = {
...DEFAULT_STAKEHOLDER,
...formikBag.stakeholder,
...orgDetails,
};

// Construct the suggestion by starting with the stakeholder record,
Expand All @@ -175,16 +202,16 @@ const SuggestionForm = withFormik({
.catch(() => {
formikBag.props.setToast({
message:
"Sorry, submitting your correction failed, please email us at the address on the About page.",
"Sorry, submitting your correction failed, please email us via the Contact Us page.",
});
});
},
})(SuggestionDialog);
})(CorrectionDialog);

SuggestionDialog.propTypes = {
CorrectionDialog.propTypes = {
onClose: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired,
stakeholder: PropTypes.object,
};

export default SuggestionForm;
export default CorrectionForm;
2 changes: 1 addition & 1 deletion client/src/components/FoodSeeker/Suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function Suggestion(props) {
formikBag.setSubmitting(false);
setToast({
message:
"Sorry, submitting your correction failed, please email us at the address on the About page.",
"Sorry, submitting your correction failed, please email us via the Contact Us page.",
});
});
}}
Expand Down
5 changes: 3 additions & 2 deletions server/app/validation-schema/suggestion-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ export const suggestionPostRequestSchema: JSONSchemaType<
properties: {
stakeholderId: {
type: "integer",
anyOf: [{ minimum: 1 }, { type: "null" }],
nullable: true,
anyOf: [{ minimum: 1 }, { type: ["integer", "null"] }],
},
adminNotes: {
type: "string",
},
suggestionStatusId: { type: "integer" },
suggestionStatusId: { type: "integer", nullable: true },
name: {
type: "string",
minLength: 1,
Expand Down
4 changes: 2 additions & 2 deletions server/types/suggestion-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Suggestion {
id: number;
id: number | null;
name: string;
address1: string;
address2: string;
Expand All @@ -15,7 +15,7 @@ export interface Suggestion {
hours: string;
category: string;
suggestionStatusId: number;
adminNotes: string;
adminNotes?: string;
stakeholderId: number | null;
tenantId: number;
}

0 comments on commit 6e22fa9

Please sign in to comment.