diff --git a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useAddUserToCollectivite.ts b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useAddUserToCollectivite.ts index faceb9dc2f4..55c0f03efab 100644 --- a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useAddUserToCollectivite.ts +++ b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useAddUserToCollectivite.ts @@ -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; @@ -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, }); @@ -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 { @@ -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, diff --git a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useRemoveFromCollectivite.tsx b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useRemoveFromCollectivite.tsx index 6276fa54549..b8d3bc70bcf 100644 --- a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useRemoveFromCollectivite.tsx +++ b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useRemoveFromCollectivite.tsx @@ -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; @@ -12,7 +13,7 @@ const removeMembre = async ( collectiviteId: number, userEmail: string ): Promise => { - const {data, error} = await supabaseClient.rpc( + const { data, error } = await supabaseClient.rpc( 'remove_membre_from_collectivite', { email: userEmail, @@ -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 }); }, } ); diff --git a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useUpdateCollectiviteMembre.ts b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useUpdateCollectiviteMembre.ts index 7875483d483..e6f9f1b0449 100644 --- a/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useUpdateCollectiviteMembre.ts +++ b/app.territoiresentransitions.react/src/app/pages/collectivite/Users/useUpdateCollectiviteMembre.ts @@ -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 @@ -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 => - 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 }); }, }); @@ -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, })