Skip to content

Commit

Permalink
Met à jour le cache après avoir fait des changements dans la gestion …
Browse files Browse the repository at this point in the history
…des membres
  • Loading branch information
marc-rutkowski committed Dec 2, 2024
1 parent 3e0efbc commit 18ae214
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {useMutation, useQueryClient} from 'react-query';
import {supabaseClient} from 'core-logic/api/supabase';
import {TNiveauAcces} from 'types/alias';
import {CurrentCollectivite} from 'core-logic/hooks/useCurrentCollectivite';
import {UserData} from 'core-logic/api/auth/AuthProvider';
import {useSendInvitation} from 'app/pages/collectivite/Users/useSendInvitation';
import { trpc } from '@tet/api/utils/trpc/client';
import { supabaseClient } from 'core-logic/api/supabase';
import { TNiveauAcces } from 'types/alias';
import { CurrentCollectivite } from 'core-logic/hooks/useCurrentCollectivite';
import { UserData } from 'core-logic/api/auth/AuthProvider';
import { useSendInvitation } from 'app/pages/collectivite/Users/useSendInvitation';

type AddUserToCollectiviteArgs = {
email: string;
Expand Down Expand Up @@ -32,15 +33,16 @@ export const useAddUserToCollectivite = (
collectivite: CurrentCollectivite,
user: UserData
) => {
const {collectivite_id} = collectivite;
const { collectivite_id: collectiviteId } = collectivite;
const queryClient = useQueryClient();
const {mutate: sendInvitation} = useSendInvitation(collectivite, user);
const { mutate: sendInvitation } = useSendInvitation(collectivite, user);
const utils = trpc.useUtils();

return useMutation(
async ({email: rawEmail, niveau}: AddUserToCollectiviteArgs) => {
async ({ email: rawEmail, niveau }: AddUserToCollectiviteArgs) => {
const email = rawEmail.toLowerCase();
const {data, error} = await supabaseClient.rpc('add_user', {
collectivite_id,
const { data, error } = await supabaseClient.rpc('add_user', {
collectivite_id: collectiviteId,
email,
niveau,
});
Expand All @@ -58,7 +60,7 @@ export const useAddUserToCollectivite = (

// envoi le mail d'invitation ou un mail de notification du rattachement à la collectivité
if (invitationId || response.added) {
await sendInvitation({email, invitationId});
await sendInvitation({ email, invitationId });
}

return {
Expand All @@ -69,10 +71,9 @@ export const useAddUserToCollectivite = (
},
{
onSuccess: () => {
queryClient.invalidateQueries([
'collectivite_membres',
collectivite_id,
]);
queryClient.invalidateQueries(['collectivite_membres', collectiviteId]);
if (collectiviteId)
utils.collectivites.membres.list.invalidate({ collectiviteId });
},
meta: {
disableToast: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {useMutation, useQueryClient} from 'react-query';
import {useCollectiviteId} from 'core-logic/hooks/params';
import {TRemoveFromCollectivite} from './types';
import {getQueryKey} from './useCollectiviteMembres';
import {supabaseClient} from 'core-logic/api/supabase';
import { trpc } from '@tet/api/utils/trpc/client';
import { useCollectiviteId } from 'core-logic/hooks/params';
import { TRemoveFromCollectivite } from './types';
import { getQueryKey } from './useCollectiviteMembres';
import { supabaseClient } from 'core-logic/api/supabase';

type RemoveMembreResponse = {
message?: string;
Expand All @@ -12,7 +13,7 @@ const removeMembre = async (
collectiviteId: number,
userEmail: string
): Promise<RemoveMembreResponse | null> => {
const {data, error} = await supabaseClient.rpc(
const { data, error } = await supabaseClient.rpc(
'remove_membre_from_collectivite',
{
email: userEmail,
Expand All @@ -30,17 +31,21 @@ const removeMembre = async (
* Retire un membre de la collectivité courante
*/
export const useRemoveFromCollectivite = () => {
const collectivite_id = useCollectiviteId();
const collectiviteId = useCollectiviteId();
const queryClient = useQueryClient();
const {isLoading, mutate} = useMutation(
const utils = trpc.useUtils();

const { isLoading, mutate } = useMutation(
(userEmail: string) =>
collectivite_id
? removeMembre(collectivite_id, userEmail)
collectiviteId
? removeMembre(collectiviteId, userEmail)
: Promise.resolve(null),
{
onSuccess: () => {
// recharge la liste après avoir retiré l'utilisateur de la collectivité
queryClient.invalidateQueries(getQueryKey(collectivite_id));
queryClient.invalidateQueries(getQueryKey(collectiviteId));
if (collectiviteId)
utils.collectivites.membres.list.invalidate({ collectiviteId });
},
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useMutation, useQueryClient} from 'react-query';
import { trpc } from '@tet/api/utils/trpc/client';
import {supabaseClient} from 'core-logic/api/supabase';
import {TUpdateMembre, TUpdateMembreArgs} from './types';
import {useCollectiviteId} from 'core-logic/hooks/params';
import { useCollectiviteId } from 'core-logic/hooks/params';

/**
* Met à jour une propriété d'un des membres de la collectivité courante
Expand All @@ -10,18 +11,21 @@ export const useUpdateCollectiviteMembre = () => {
const queryClient = useQueryClient();

// associe la fonction d'update avec l'id de la collectivité
const collectivite_id = useCollectiviteId();
const collectiviteId = useCollectiviteId();
const updateCollectiviteMembre = (
args: TUpdateMembreArgs
): Promise<boolean> =>
collectivite_id
? updateMembre({collectivite_id, ...args})
collectiviteId
? updateMembre({ collectiviteId, ...args })
: Promise.resolve(false);

const {isLoading, mutate} = useMutation(updateCollectiviteMembre, {
const utils = trpc.useUtils();
const { isLoading, mutate } = useMutation(updateCollectiviteMembre, {
onSuccess: () => {
// recharge les données après un changement
queryClient.invalidateQueries(['collectivite_membres']);
if (collectiviteId)
utils.collectivites.membres.list.invalidate({ collectiviteId });
},
});

Expand All @@ -35,14 +39,14 @@ const fieldNameToRPCName = (name: TUpdateMembreArgs['name']) =>
`update_collectivite_membre_${name}`;

const updateMembre = async ({
collectivite_id,
collectiviteId,
membre_id,
name,
value,
}: TUpdateMembreArgs & {collectivite_id: number}) => {
const {error} = await supabaseClient
}: TUpdateMembreArgs & { collectiviteId: number }) => {
const { error } = await supabaseClient
.rpc(fieldNameToRPCName(name) as any, {
collectivite_id,
collectivite_id: collectiviteId,
membre_id,
[name]: value,
})
Expand Down

0 comments on commit 18ae214

Please sign in to comment.