-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): added organism remote information page to agencies setti…
…ngs v3
- Loading branch information
Showing
4 changed files
with
490 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
.../agencies-settings-v3/organisms/[organismId]/remote/information/informationRemote.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
98 changes: 98 additions & 0 deletions
98
...cies-settings-v3/organisms/[organismId]/remote/information/informationRemoteFormSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
>; |
Oops, something went wrong.