-
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 remote agency visibility toggle to aap settings v3…
… remote agency page
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...rganisms/[organismId]/_components/organism-visibility-toggle/OrganismVisibilityToggle.tsx
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,84 @@ | ||
import { useGraphQlClient } from "@/components/graphql/graphql-client/GraphqlClient"; | ||
import { graphqlErrorToast, successToast } from "@/components/toast/toast"; | ||
import { graphql } from "@/graphql/generated"; | ||
import { ToggleSwitch } from "@codegouvfr/react-dsfr/ToggleSwitch"; | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
|
||
const organismQuery = graphql(` | ||
query getOrganismForOrganismVisibilityToggle($organismId: ID!) { | ||
organism_getOrganism(id: $organismId) { | ||
id | ||
fermePourAbsenceOuConges | ||
} | ||
} | ||
`); | ||
|
||
const updateOrganismVisibilityMutation = graphql(` | ||
mutation updateOrganismVisibilityForOrganismVisibilityToggle( | ||
$organismId: ID! | ||
$fermePourAbsenceOuConges: Boolean! | ||
) { | ||
organism_updateFermePourAbsenceOuConges( | ||
organismId: $organismId | ||
fermePourAbsenceOuConges: $fermePourAbsenceOuConges | ||
) { | ||
id | ||
} | ||
} | ||
`); | ||
|
||
export const OrganismVisibilityToggle = ({ | ||
organismId, | ||
}: { | ||
organismId: string; | ||
}) => { | ||
const { graphqlClient } = useGraphQlClient(); | ||
const queryClient = useQueryClient(); | ||
|
||
const { data: organismResponse } = useQuery({ | ||
queryKey: [organismId, "organisms", "organism_visibility_toggle"], | ||
queryFn: () => graphqlClient.request(organismQuery, { organismId }), | ||
}); | ||
|
||
const organism = organismResponse?.organism_getOrganism; | ||
|
||
const updateFermePourAbsenceOuConges = useMutation({ | ||
mutationFn: ({ | ||
organismId, | ||
fermePourAbsenceOuConges, | ||
}: { | ||
organismId: string; | ||
fermePourAbsenceOuConges: boolean; | ||
}) => | ||
graphqlClient.request(updateOrganismVisibilityMutation, { | ||
organismId, | ||
fermePourAbsenceOuConges, | ||
}), | ||
}); | ||
|
||
const handleVisibilityChange = async (checked: boolean) => { | ||
try { | ||
await updateFermePourAbsenceOuConges.mutateAsync({ | ||
organismId: organism?.id, | ||
fermePourAbsenceOuConges: !checked, | ||
}); | ||
queryClient.invalidateQueries({ | ||
queryKey: [organism?.id], | ||
}); | ||
successToast("modifications enregistrées"); | ||
} catch (e) { | ||
graphqlErrorToast(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<ToggleSwitch | ||
label="Je souhaite que la structure soit visible dans les résultats de recherche" | ||
inputTitle="Je souhaite que la structure soit visible dans les résultats de recherche" | ||
labelPosition="left" | ||
showCheckedHint={false} | ||
checked={!organism?.fermePourAbsenceOuConges} | ||
onChange={(checked) => handleVisibilityChange(checked)} | ||
/> | ||
); | ||
}; |
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