Skip to content

Commit

Permalink
fix: admin referent list buttons and edit actions (#2210)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturlg authored Mar 18, 2024
1 parent 8e8d691 commit e7717d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export class EditReferent implements UseCase<EditReferentDTO, ReferentDTO> {
: found.value,
county: dto.county ? new County(dto.county) : found.county,
region: dto.region ? new Region(dto.region) : found.region,
substitute: {
email: dto.substitute?.email ? new Email(dto.substitute.email) : found.substitute?.email,
name: dto.substitute?.name ?? found.substitute?.name, // ?? because "" is allowed
},
substitute: Object.keys(dto.substitute || {}).length
? {
email: dto.substitute?.email ? new Email(dto.substitute.email) : found.substitute?.email,
name: dto.substitute?.name ?? found.substitute?.name, // ?? because "" is allowed
}
: undefined,
},
new UniqueID(dto.id),
);
Expand Down
36 changes: 17 additions & 19 deletions packages/app/src/app/admin/liste-referents/ReferentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { FormFieldset, FormLayout } from "@design-system";
import { zodResolver } from "@hookform/resolvers/zod";
import { noop, sortBy } from "lodash";
import { useRouter } from "next/navigation";
import { useCallback, useEffect } from "react";
import { useEffect } from "react";
import { useForm } from "react-hook-form";

import { createReferent, saveReferent } from "./actions";
Expand Down Expand Up @@ -42,14 +42,6 @@ export const ReferentModal = ({ mode = "edit", modal }: ReferentModalProps) => {
mode: "onChange",
});

const cleanForm = useCallback(() => {
reset({
type: "email",
principal: true,
region: void 0,
});
}, [reset]);

useEffect(() => {
if (mode === "edit") {
reset({
Expand All @@ -60,8 +52,8 @@ export const ReferentModal = ({ mode = "edit", modal }: ReferentModalProps) => {
name: currentEdited?.substitute?.name ?? "",
},
});
} else cleanForm();
}, [reset, currentEdited, mode, cleanForm]);
}
}, [reset, currentEdited, mode]);

const writtenName = watch("name");
const selectedRegion = watch("region");
Expand All @@ -71,11 +63,14 @@ export const ReferentModal = ({ mode = "edit", modal }: ReferentModalProps) => {
const doSave = async () => {
const go = await trigger();
if (go) {
handleSubmit(async formData => {
await handleSubmit(async formData => {
if (mode === "edit") {
saveReferent(formData);
if (formData.substitute?.name === "" && formData.substitute?.email === "") {
formData.substitute = void 0;
}
await saveReferent(formData);
} else {
createReferent(formData);
await createReferent(formData);
}
modal.close();
router.refresh();
Expand All @@ -93,9 +88,9 @@ export const ReferentModal = ({ mode = "edit", modal }: ReferentModalProps) => {
children: "Sauvegarder",
disabled: !isValid || !isDirty,
doClosesModal: false,
onClick() {
doSave();
cleanForm();
async onClick() {
await doSave();
reset();
},
},
]}
Expand Down Expand Up @@ -180,12 +175,15 @@ export const ReferentModal = ({ mode = "edit", modal }: ReferentModalProps) => {
label: "Email",
nativeInputProps: {
value: "email",
...register("type", { deps: "value" }),
...register("type"),
},
},
{
label: "URL",
nativeInputProps: { value: "url", ...register("type", { deps: "value" }) },
nativeInputProps: {
value: "url",
...register("type", { setValueAs: value => (value === "URL" ? "url" : "email") }),
},
},
]}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import ButtonsGroup from "@codegouvfr/react-dsfr/ButtonsGroup";

import { CreateReferentModal, createReferentModalButtonProps } from "./CreateReferentModal";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import ButtonsGroup from "@codegouvfr/react-dsfr/ButtonsGroup";
import { createModal } from "@codegouvfr/react-dsfr/Modal";
import { config } from "@common/config";
Expand Down

0 comments on commit e7717d5

Please sign in to comment.