|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRouter } from "next/navigation"; |
| 4 | +import { useSession } from "next-auth/react"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | +import { impersonateSearchSchema } from "~/modules/admin/schemas"; |
| 8 | +import { useZodForm } from "~/modules/shared/useZodForm"; |
| 9 | +import { api } from "~/trpc/react"; |
| 10 | + |
| 11 | +import { CompanyPreviewCard } from "./CompanyPreviewCard"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Search + preview + start/stop impersonation. |
| 15 | + * |
| 16 | + * The impersonation state is stored in the NextAuth JWT. Starting is a |
| 17 | + * single `session.update({ impersonation: { siren, name } })` call: the |
| 18 | + * server-side `jwt` callback writes the audit row and mutates the token |
| 19 | + * atomically. Stopping is the same call with `null`. |
| 20 | + */ |
| 21 | +export function ImpersonateForm() { |
| 22 | + const session = useSession(); |
| 23 | + const router = useRouter(); |
| 24 | + const [serverError, setServerError] = useState<string | null>(null); |
| 25 | + const [starting, setStarting] = useState(false); |
| 26 | + |
| 27 | + const form = useZodForm(impersonateSearchSchema, { |
| 28 | + defaultValues: { siren: "" }, |
| 29 | + }); |
| 30 | + |
| 31 | + const lastImpersonated = api.admin.getLastImpersonated.useQuery(undefined, { |
| 32 | + enabled: session.data?.user.isAdmin === true, |
| 33 | + }); |
| 34 | + |
| 35 | + const searchMutation = api.admin.searchCompany.useMutation({ |
| 36 | + onSuccess: () => setServerError(null), |
| 37 | + onError: (err) => setServerError(err.message), |
| 38 | + }); |
| 39 | + |
| 40 | + const preview = searchMutation.data ?? null; |
| 41 | + |
| 42 | + const onSearch = form.handleSubmit((values) => { |
| 43 | + searchMutation.mutate(values); |
| 44 | + }); |
| 45 | + |
| 46 | + const onStart = async () => { |
| 47 | + if (!preview) return; |
| 48 | + setStarting(true); |
| 49 | + try { |
| 50 | + await session.update({ |
| 51 | + impersonation: { siren: preview.siren, name: preview.name }, |
| 52 | + }); |
| 53 | + router.push("/mon-espace"); |
| 54 | + } finally { |
| 55 | + setStarting(false); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const sirenError = form.formState.errors.siren?.message; |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="fr-mt-4w"> |
| 63 | + <form noValidate onSubmit={onSearch}> |
| 64 | + <div |
| 65 | + className={ |
| 66 | + sirenError |
| 67 | + ? "fr-input-group fr-input-group--error" |
| 68 | + : "fr-input-group" |
| 69 | + } |
| 70 | + > |
| 71 | + <label className="fr-label" htmlFor="impersonate-siren"> |
| 72 | + SIREN de l'entreprise |
| 73 | + </label> |
| 74 | + <input |
| 75 | + aria-describedby={ |
| 76 | + sirenError ? "impersonate-siren-error" : undefined |
| 77 | + } |
| 78 | + aria-invalid={Boolean(sirenError)} |
| 79 | + aria-required="true" |
| 80 | + autoComplete="off" |
| 81 | + className="fr-input" |
| 82 | + id="impersonate-siren" |
| 83 | + inputMode="numeric" |
| 84 | + list="impersonate-siren-list" |
| 85 | + type="text" |
| 86 | + {...form.register("siren")} |
| 87 | + /> |
| 88 | + <datalist id="impersonate-siren-list"> |
| 89 | + {(lastImpersonated.data ?? []).map((c) => ( |
| 90 | + <option key={c.siren} value={c.siren}> |
| 91 | + {c.name} |
| 92 | + </option> |
| 93 | + ))} |
| 94 | + </datalist> |
| 95 | + {sirenError && ( |
| 96 | + <p className="fr-error-text" id="impersonate-siren-error"> |
| 97 | + {sirenError} |
| 98 | + </p> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + |
| 102 | + <ul className="fr-btns-group fr-btns-group--inline-sm"> |
| 103 | + <li> |
| 104 | + <button |
| 105 | + className="fr-btn" |
| 106 | + disabled={searchMutation.isPending} |
| 107 | + type="submit" |
| 108 | + > |
| 109 | + Rechercher |
| 110 | + </button> |
| 111 | + </li> |
| 112 | + </ul> |
| 113 | + </form> |
| 114 | + |
| 115 | + {serverError && ( |
| 116 | + <div className="fr-alert fr-alert--error fr-mt-2w" role="alert"> |
| 117 | + <h3 className="fr-alert__title">Erreur</h3> |
| 118 | + <p>{serverError}</p> |
| 119 | + </div> |
| 120 | + )} |
| 121 | + |
| 122 | + {preview && ( |
| 123 | + <> |
| 124 | + <CompanyPreviewCard company={preview} /> |
| 125 | + <ul className="fr-btns-group fr-btns-group--inline-sm fr-mt-2w"> |
| 126 | + <li> |
| 127 | + <button |
| 128 | + className="fr-btn" |
| 129 | + disabled={starting} |
| 130 | + onClick={onStart} |
| 131 | + type="button" |
| 132 | + > |
| 133 | + Valider et mimoquer |
| 134 | + </button> |
| 135 | + </li> |
| 136 | + </ul> |
| 137 | + </> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + ); |
| 141 | +} |
0 commit comments