Skip to content

Commit

Permalink
feat(admin): added remote agency visibility toggle to aap settings v3…
Browse files Browse the repository at this point in the history
… remote agency page
  • Loading branch information
agarbe committed Sep 2, 2024
1 parent e716858 commit d47fdbe
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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)}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useQuery } from "@tanstack/react-query";
import Link from "next/link";
import { useParams } from "next/navigation";
import { getRemoteZoneLabel } from "../../../_components/getRemoteZoneLabel";
import { OrganismVisibilityToggle } from "../_components/organism-visibility-toggle/OrganismVisibilityToggle";

const getOrganismQuery = graphql(`
query getOrganismForOrganismRemotePage($organismId: ID!) {
Expand Down Expand Up @@ -92,6 +93,10 @@ export default function RemotePage() {
isEditable
buttonOnClickHref={`/agencies-settings-v3/organisms/${organismId}/remote/domaines-ccns-degrees`}
/>
<div className="flex flex-col mt-6">
<h2>Visibilité de la structure</h2>
<OrganismVisibilityToggle organismId={organismId} />
</div>
</div>
</div>
);
Expand Down

0 comments on commit d47fdbe

Please sign in to comment.