From a69ce5cd15957776ef0721e193420fbbacb7d482 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 13 May 2024 21:41:31 +0530 Subject: [PATCH] Change contact API response type to formData (#9349) * Change contact API response type to formData * Review changes * Review changes --- app/controllers/contacts_controller.rb | 9 +++-- .../components/ContactsPage/ContactForm.jsx | 8 ++-- .../SubForms/Competition/index.jsx | 2 +- .../ContactsPage/SubForms/Wct/index.jsx | 2 +- .../ContactsPage/SubForms/Wrt/index.jsx | 2 +- .../ContactsPage/SubForms/Wst/index.jsx | 2 +- .../components/ContactsPage/UserData.jsx | 2 +- .../components/ContactsPage/store/reducer.js | 37 ++++++++++++------- 8 files changed, 39 insertions(+), 25 deletions(-) diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index d8fa1dd43e..5fe137de78 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -62,9 +62,12 @@ class ContactsController < ApplicationController end def contact - contact_recipient = params.require(:contactRecipient) - contact_params = params.require(contact_recipient) - requestor_details = current_user || params.require(:userData) + formValues = JSON.parse(params.require(:formValues), symbolize_names: true) + contact_recipient = formValues[:contactRecipient] + contact_params = formValues[contact_recipient.to_sym] + requestor_details = current_user || formValues[:userData] + + render status: :bad_request, json: { error: "Invalid arguments" } if contact_recipient.nil? || contact_params.nil? || requestor_details.nil? case contact_recipient when UserGroup.teams_committees_group_wct.metadata.friendly_id diff --git a/app/webpacker/components/ContactsPage/ContactForm.jsx b/app/webpacker/components/ContactsPage/ContactForm.jsx index 53a6417281..8127d8265d 100644 --- a/app/webpacker/components/ContactsPage/ContactForm.jsx +++ b/app/webpacker/components/ContactsPage/ContactForm.jsx @@ -33,7 +33,7 @@ export default function ContactForm({ loggedInUserData }) { const [contactSuccess, setContactSuccess] = useState(false); const contactFormState = useStore(); const dispatch = useDispatch(); - const { contactRecipient: selectedContactRecipient, userData } = contactFormState; + const { formValues: { contactRecipient: selectedContactRecipient, userData } } = contactFormState; const isFormValid = ( selectedContactRecipient && userData.name && userData.email && captchaValue @@ -78,11 +78,13 @@ export default function ContactForm({ loggedInUserData }) {
{ if (isFormValid) { + const formData = new FormData(); + formData.append('formValues', JSON.stringify(contactFormState.formValues)); save( contactUrl, - contactFormState, + formData, contactSuccessHandler, - { method: 'POST' }, + { method: 'POST', headers: {}, body: formData }, ); } }} diff --git a/app/webpacker/components/ContactsPage/SubForms/Competition/index.jsx b/app/webpacker/components/ContactsPage/SubForms/Competition/index.jsx index f0c0ef0d75..12a83ee987 100644 --- a/app/webpacker/components/ContactsPage/SubForms/Competition/index.jsx +++ b/app/webpacker/components/ContactsPage/SubForms/Competition/index.jsx @@ -9,7 +9,7 @@ import { updateSectionData } from '../../store/actions'; const SECTION = 'competition'; export default function Competition() { - const { competition } = useStore(); + const { formValues: { competition } } = useStore(); const dispatch = useDispatch(); const handleFormChange = (_, { name, value }) => dispatch( updateSectionData(SECTION, name, value), diff --git a/app/webpacker/components/ContactsPage/SubForms/Wct/index.jsx b/app/webpacker/components/ContactsPage/SubForms/Wct/index.jsx index deec36cfa9..1c41006a44 100644 --- a/app/webpacker/components/ContactsPage/SubForms/Wct/index.jsx +++ b/app/webpacker/components/ContactsPage/SubForms/Wct/index.jsx @@ -7,7 +7,7 @@ import { updateSectionData } from '../../store/actions'; const SECTION = 'wct'; export default function Wct() { - const { wct } = useStore(); + const { formValues: { wct } } = useStore(); const dispatch = useDispatch(); const handleFormChange = (_, { name, value }) => dispatch( updateSectionData(SECTION, name, value), diff --git a/app/webpacker/components/ContactsPage/SubForms/Wrt/index.jsx b/app/webpacker/components/ContactsPage/SubForms/Wrt/index.jsx index 6d6c45e526..d5e0040649 100644 --- a/app/webpacker/components/ContactsPage/SubForms/Wrt/index.jsx +++ b/app/webpacker/components/ContactsPage/SubForms/Wrt/index.jsx @@ -7,7 +7,7 @@ import { updateSectionData } from '../../store/actions'; const SECTION = 'wrt'; export default function Wrt() { - const { wrt } = useStore(); + const { formValues: { wrt } } = useStore(); const dispatch = useDispatch(); const handleFormChange = (_, { name, value }) => dispatch( updateSectionData(SECTION, name, value), diff --git a/app/webpacker/components/ContactsPage/SubForms/Wst/index.jsx b/app/webpacker/components/ContactsPage/SubForms/Wst/index.jsx index 9a35c1e0bc..e18d37b994 100644 --- a/app/webpacker/components/ContactsPage/SubForms/Wst/index.jsx +++ b/app/webpacker/components/ContactsPage/SubForms/Wst/index.jsx @@ -7,7 +7,7 @@ import { updateSectionData } from '../../store/actions'; const SECTION = 'wst'; export default function Wst() { - const { wst } = useStore(); + const { formValues: { wst } } = useStore(); const dispatch = useDispatch(); const handleFormChange = (_, { name, value }) => dispatch( updateSectionData(SECTION, name, value), diff --git a/app/webpacker/components/ContactsPage/UserData.jsx b/app/webpacker/components/ContactsPage/UserData.jsx index 075a9ff78b..d5d5e305c4 100644 --- a/app/webpacker/components/ContactsPage/UserData.jsx +++ b/app/webpacker/components/ContactsPage/UserData.jsx @@ -7,7 +7,7 @@ import { updateSectionData } from './store/actions'; const SECTION = 'userData'; export default function UserData({ loggedInUserData }) { - const { userData } = useStore(); + const { formValues: { userData } } = useStore(); const dispatch = useDispatch(); const handleFormChange = (_, { name, value }) => dispatch( updateSectionData(SECTION, name, value), diff --git a/app/webpacker/components/ContactsPage/store/reducer.js b/app/webpacker/components/ContactsPage/store/reducer.js index f72fcec4b7..ea753bd14a 100644 --- a/app/webpacker/components/ContactsPage/store/reducer.js +++ b/app/webpacker/components/ContactsPage/store/reducer.js @@ -5,31 +5,40 @@ import { } from './actions'; export const getContactFormInitialState = (loggedInUserData, queryParams) => ({ - userData: { - name: loggedInUserData?.user?.name, - email: loggedInUserData?.user?.email, - }, - contactRecipient: queryParams?.contactRecipient, - competition: { - competitionId: queryParams?.competitionId, - }, - wst: { - requestId: queryParams?.requestId, + formValues: { + userData: { + name: loggedInUserData?.user?.name, + email: loggedInUserData?.user?.email, + }, + contactRecipient: queryParams?.contactRecipient, + competition: { + competitionId: queryParams?.competitionId, + }, + wst: { + requestId: queryParams?.requestId, + }, }, + attachments: [], }); const reducers = { [UpdateSectionData]: (state, { payload }) => ({ ...state, - [payload.section]: { - ...(state[payload.section] || {}), - [payload.name]: payload.value, + formValues: { + ...state.formValues, + [payload.section]: { + ...(state.formValues[payload.section] || {}), + [payload.name]: payload.value, + }, }, }), [UpdateContactRecipient]: (state, { payload }) => ({ ...state, - contactRecipient: payload.contactRecipient, + formValues: { + ...state.formValues, + contactRecipient: payload.contactRecipient, + }, }), [ClearForm]: (__, { payload }) => (