Skip to content

Commit

Permalink
feat(admin): added organism remote information page to agencies setti…
Browse files Browse the repository at this point in the history
…ngs v3
  • Loading branch information
agarbe committed Aug 30, 2024
1 parent 07477bd commit 312e607
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useGraphQlClient } from "@/components/graphql/graphql-client/GraphqlClient";
import { graphql } from "@/graphql/generated";
import { RemoteZone } from "@/graphql/generated/graphql";

import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useParams } from "next/navigation";

const getOrganismQuery = graphql(`
query getOrganismForInformationRemotePage($organismId: ID!) {
organism_getOrganism(id: $organismId) {
id
isRemote
remoteZones
informationsCommerciales {
id
nom
telephone
siteInternet
emailContact
}
}
}
`);

const createOrUpdateInformationsCommercialesAndRemoteStatusMutation = graphql(`
mutation createOrUpdateInformationsCommercialesAndRemoteStatusMutation(
$createOrUpdateInformationsCommercialesInput: CreateOrUpdateInformationsCommercialesInput!
$organismId: String!
$isRemote: Boolean!
$remoteZones: [RemoteZone!]!
) {
organism_createOrUpdateInformationsCommerciales(
informationsCommerciales: $createOrUpdateInformationsCommercialesInput
) {
id
}
organism_updateOrganismOnSiteAndRemoteStatus(
organismId: $organismId
isOnSite: false
isRemote: $isRemote
remoteZones: $remoteZones
) {
id
}
}
`);

export const useInformationRemotePage = () => {
const queryClient = useQueryClient();
const { graphqlClient } = useGraphQlClient();

const { organismId } = useParams<{ organismId: string }>();

const { data: getOrganismResponse, status: getOrganismStatus } = useQuery({
queryKey: [organismId, "organism"],
queryFn: () =>
graphqlClient.request(getOrganismQuery, {
organismId,
}),
});

const organism = getOrganismResponse?.organism_getOrganism;

const createOrUpdateInformationsCommercialesAndRemoteStatus = useMutation({
mutationFn: ({
organismId,
informationsCommerciales,
isRemote,
remoteZones,
}: {
organismId: string;
isRemote: boolean;
remoteZones: RemoteZone[];
informationsCommerciales: {
nom: string;
};
}) =>
graphqlClient.request(
createOrUpdateInformationsCommercialesAndRemoteStatusMutation,
{
organismId,
isRemote,
remoteZones,
createOrUpdateInformationsCommercialesInput: {
organismId,
...informationsCommerciales,
},
},
),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [organismId, "organisms"] });
},
});

return {
organism,
getOrganismStatus,
createOrUpdateInformationsCommercialesAndRemoteStatus,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { z } from "zod";

export const informationRemoteFormSchema = z
.object({
nom: z.string(),
telephone: z.string().optional(),
siteInternet: z.string().optional(),
emailContact: z
.union([
z
.string()
.length(0, "Le champ doit être vide ou contenir une adresse email"),
z
.string()
.email("Le champ doit être vide ou contenir une adresse email"),
])
.optional()
.default(""),
isNotRemote: z.boolean(),
isRemoteFranceMetropolitaine: z.boolean(),
isRemoteGuadeloupe: z.boolean(),
isRemoteGuyane: z.boolean(),
isRemoteMartinique: z.boolean(),
isRemoteMayotte: z.boolean(),
isRemoteLaReunion: z.boolean(),
isRemoteSaintPierreEtMiquelon: z.boolean(),
isRemoteSainteLucieSaintMartin: z.boolean(),
})
.superRefine(
(
{
isNotRemote,
isRemoteFranceMetropolitaine,
isRemoteGuadeloupe,
isRemoteGuyane,
isRemoteLaReunion,
isRemoteMartinique,
isRemoteMayotte,
nom,
emailContact,
telephone,
},
{ addIssue },
) => {
const assertRequiredField = ({
fieldName,
fieldValue,
}: {
fieldName: string;
fieldValue: unknown;
}) => {
if (!fieldValue) {
console.log({ fieldValue });
addIssue({
path: [fieldName],
message: "Merci de remplir ce champ",
code: z.ZodIssueCode.custom,
});
}
};
if (!isNotRemote) {
[
{
fieldName: "nom",
fieldValue: nom,
},
{
fieldName: "emailContact",
fieldValue: emailContact,
},
{
fieldName: "telephone",
fieldValue: telephone,
},
].forEach(assertRequiredField);
if (
![
isRemoteFranceMetropolitaine,
isRemoteGuadeloupe,
isRemoteGuyane,
isRemoteLaReunion,
isRemoteMartinique,
isRemoteMayotte,
].some((r) => !!r)
) {
addIssue({
path: ["isRemoteFranceMetropolitaine"],
message: "Au moins une case doit être cochée",
code: z.ZodIssueCode.custom,
});
}
}
},
);

export type InformationRemoteFormData = z.infer<
typeof informationRemoteFormSchema
>;
Loading

0 comments on commit 312e607

Please sign in to comment.