Skip to content

Commit

Permalink
Change contact API response type to formData (#9349)
Browse files Browse the repository at this point in the history
* Change contact API response type to formData

* Review changes

* Review changes
  • Loading branch information
danieljames-dj authored May 13, 2024
1 parent c376bb2 commit a69ce5c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
9 changes: 6 additions & 3 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions app/webpacker/components/ContactsPage/ContactForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,11 +78,13 @@ export default function ContactForm({ loggedInUserData }) {
<Form
onSubmit={() => {
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 },
);
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion app/webpacker/components/ContactsPage/UserData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
37 changes: 23 additions & 14 deletions app/webpacker/components/ContactsPage/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
Expand Down

0 comments on commit a69ce5c

Please sign in to comment.