Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Oct 9, 2024
2 parents 2e21aba + 3a00afc commit 864f351
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions client/src/components/contact-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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),
Expand All @@ -93,11 +108,24 @@ export default function ContactForm({ objectId, objectType }: Props) {
console.error("error", e);
},
});

const { form, updateForm, errors } = useForm<FormState, FormState>(
{},
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 (
<RawIntlProvider value={intl}>
Expand Down Expand Up @@ -142,6 +170,33 @@ export default function ContactForm({ objectId, objectType }: Props) {
mutate(form);
}}
>
{!location.pathname.includes("bugs") && (
<Select
isRequired
onSelectionChange={(key) => handleApiChange(key)}
label="Sélectionner le type de requête"
>
<SelectOption
key="contact"
description="Envoyer un message via le formulaire de contact"
>
Formulaire de contact
</SelectOption>
<SelectOption
key="remove"
description="Supprimer un profil utilisateur"
>
Suppression de profil
</SelectOption>
<SelectOption
key="nameChange"
description="Demander un changement de nom"
>
Changement de nom
</SelectOption>
</Select>
)}

<TextInput
value={form.name}
onChange={(e) => updateForm({ name: e.target.value })}
Expand Down

0 comments on commit 864f351

Please sign in to comment.