diff --git a/client/src/components/contact-form/index.tsx b/client/src/components/contact-form/index.tsx index a8aee9f..5767819 100644 --- a/client/src/components/contact-form/index.tsx +++ b/client/src/components/contact-form/index.tsx @@ -4,12 +4,14 @@ import { Notice, TextArea, TextInput, + Select, + SelectOption, useDSFRConfig, } from "@dataesr/dsfr-plus"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import useForm from "../../hooks/useForm"; import { useMutation } from "@tanstack/react-query"; -import { useNavigate } from "react-router-dom"; +import { useNavigate, useLocation } from "react-router-dom"; import { createIntl, RawIntlProvider } from "react-intl"; import { postHeadersTicketOffice } from "../../config/api"; @@ -61,18 +63,31 @@ export default function ContactForm({ objectId, objectType }: Props) { const { locale } = useDSFRConfig(); const intl = createIntl({ locale, messages: messages[locale] }); const navigate = useNavigate(); - const api = objectId && objectType ? "contribute" : "contacts"; + const location = useLocation(); const [thanks, setThanks] = useState(false); + const [api, setApi] = useState("contacts"); const { isPending, isError, mutate } = useMutation({ mutationFn: async (data: FormState) => { - let payload = { ...data }; + let payload: any = { ...data }; + + if (data.organization || data.fonction) { + payload.extra = { + ...(data.organization && { organisation: data.organization }), + ...(data.fonction && { fonction: data.fonction }), + }; + + delete payload.organization; + delete payload.fonction; + } if (api === "contacts") { payload = { ...payload, fromApplication: "scanr" }; } + if (api === "contribute") { payload = { ...payload, objectId, objectType }; } + const resp = await fetch(`/ticket/api/${api}`, { method: "POST", body: JSON.stringify(payload), @@ -93,11 +108,24 @@ export default function ContactForm({ objectId, objectType }: Props) { console.error("error", e); }, }); + const { form, updateForm, errors } = useForm( {}, validate ); + const handleApiChange = (key: any) => { + if (key === "contact") setApi("contacts"); + else if (key === "remove") setApi("remove-user"); + else if (key === "nameChange") setApi("update-user-data"); + }; + + useEffect(() => { + if (location.pathname.includes("bugs")) { + setApi("contribute"); + } + }, [location]); + if (thanks) { return ( @@ -142,6 +170,33 @@ export default function ContactForm({ objectId, objectType }: Props) { mutate(form); }} > + {!location.pathname.includes("bugs") && ( + + )} + updateForm({ name: e.target.value })}