feat: delete space and edit space slug (#307)

* feat: make space slug editable

* feat: delete space

* client
This commit is contained in:
Philip Okugbe
2024-09-16 17:43:40 +01:00
committed by GitHub
parent dea9f4c063
commit fb27282886
18 changed files with 435 additions and 111 deletions

View File

@ -3,14 +3,14 @@ import {
useQuery,
useQueryClient,
UseQueryResult,
} from "@tanstack/react-query";
} from '@tanstack/react-query';
import {
IAddSpaceMember,
IChangeSpaceMemberRole,
IRemoveSpaceMember,
ISpace,
ISpaceMember,
} from "@/features/space/types/space.types";
} from '@/features/space/types/space.types';
import {
addSpaceMember,
changeMemberRole,
@ -20,23 +20,24 @@ import {
removeSpaceMember,
createSpace,
updateSpace,
} from "@/features/space/services/space-service.ts";
import { notifications } from "@mantine/notifications";
import { IPagination } from "@/lib/types.ts";
deleteSpace,
} from '@/features/space/services/space-service.ts';
import { notifications } from '@mantine/notifications';
import { IPagination } from '@/lib/types.ts';
export function useGetSpacesQuery(): UseQueryResult<
IPagination<ISpace>,
Error
> {
return useQuery({
queryKey: ["spaces"],
queryKey: ['spaces'],
queryFn: () => getSpaces(),
});
}
export function useSpaceQuery(spaceId: string): UseQueryResult<ISpace, Error> {
return useQuery({
queryKey: ["spaces", spaceId],
queryKey: ['spaces', spaceId],
queryFn: () => getSpaceById(spaceId),
enabled: !!spaceId,
staleTime: 5 * 60 * 1000,
@ -50,22 +51,22 @@ export function useCreateSpaceMutation() {
mutationFn: (data) => createSpace(data),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ["spaces"],
queryKey: ['spaces'],
});
notifications.show({ message: "Space created successfully" });
notifications.show({ message: 'Space created successfully' });
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;
notifications.show({ message: errorMessage, color: "red" });
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}
export function useGetSpaceBySlugQuery(
spaceId: string,
spaceId: string
): UseQueryResult<ISpace, Error> {
return useQuery({
queryKey: ["spaces", spaceId],
queryKey: ['spaces', spaceId],
queryFn: () => getSpaceById(spaceId),
enabled: !!spaceId,
staleTime: 5 * 60 * 1000,
@ -78,34 +79,64 @@ export function useUpdateSpaceMutation() {
return useMutation<ISpace, Error, Partial<ISpace>>({
mutationFn: (data) => updateSpace(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Space updated successfully" });
notifications.show({ message: 'Space updated successfully' });
const space = queryClient.getQueryData([
"space",
'space',
variables.spaceId,
]) as ISpace;
if (space) {
const updatedSpace = { ...space, ...data };
queryClient.setQueryData(["space", variables.spaceId], updatedSpace);
queryClient.setQueryData(["space", data.slug], updatedSpace);
queryClient.setQueryData(['space', variables.spaceId], updatedSpace);
queryClient.setQueryData(['space', data.slug], updatedSpace);
}
queryClient.invalidateQueries({
queryKey: ["spaces"],
queryKey: ['spaces'],
});
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;
notifications.show({ message: errorMessage, color: "red" });
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}
export function useDeleteSpaceMutation() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: Partial<ISpace>) => deleteSpace(data.id),
onSuccess: (data, variables) => {
notifications.show({ message: 'Space deleted successfully' });
if (variables.slug) {
queryClient.removeQueries({
queryKey: ['spaces', variables.slug],
exact: true,
});
}
const spaces = queryClient.getQueryData(['spaces']) as any;
if (spaces) {
spaces.items = spaces.items?.filter(
(space: ISpace) => space.id !== variables.id
);
queryClient.setQueryData(['spaces'], spaces);
}
},
onError: (error) => {
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}
export function useSpaceMembersQuery(
spaceId: string,
spaceId: string
): UseQueryResult<IPagination<ISpaceMember>, Error> {
return useQuery({
queryKey: ["spaceMembers", spaceId],
queryKey: ['spaceMembers', spaceId],
queryFn: () => getSpaceMembers(spaceId),
enabled: !!spaceId,
});
@ -117,14 +148,14 @@ export function useAddSpaceMemberMutation() {
return useMutation<void, Error, IAddSpaceMember>({
mutationFn: (data) => addSpaceMember(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Members added successfully" });
notifications.show({ message: 'Members added successfully' });
queryClient.invalidateQueries({
queryKey: ["spaceMembers", variables.spaceId],
queryKey: ['spaceMembers', variables.spaceId],
});
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;
notifications.show({ message: errorMessage, color: "red" });
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}
@ -135,14 +166,14 @@ export function useRemoveSpaceMemberMutation() {
return useMutation<void, Error, IRemoveSpaceMember>({
mutationFn: (data) => removeSpaceMember(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Removed successfully" });
notifications.show({ message: 'Removed successfully' });
queryClient.refetchQueries({
queryKey: ["spaceMembers", variables.spaceId],
queryKey: ['spaceMembers', variables.spaceId],
});
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;
notifications.show({ message: errorMessage, color: "red" });
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}
@ -153,15 +184,15 @@ export function useChangeSpaceMemberRoleMutation() {
return useMutation<void, Error, IChangeSpaceMemberRole>({
mutationFn: (data) => changeMemberRole(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Member role updated successfully" });
notifications.show({ message: 'Member role updated successfully' });
// due to pagination levels, change in cache instead
queryClient.refetchQueries({
queryKey: ["spaceMembers", variables.spaceId],
queryKey: ['spaceMembers', variables.spaceId],
});
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;
notifications.show({ message: errorMessage, color: "red" });
const errorMessage = error['response']?.data?.message;
notifications.show({ message: errorMessage, color: 'red' });
},
});
}