All files / app/features/profile/hooks useGallery.ts

59.45% Statements 22/37
72.72% Branches 8/11
56.25% Functions 9/16
59.45% Lines 22/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 1075x 5x   5x   256x 256x   33x         216x 216x   17x         216x 216x   216x             1x   1x 1x       1x                 216x 216x   216x                                   216x 216x   216x                                                                      
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { galleryEditInfoKey, galleryKey, userKey } from "features/queryKeys";
import { PhotoGallery } from "proto/galleries_pb";
import { service } from "service";
 
export function useGallery(galleryId: number | undefined) {
  return useQuery({
    queryKey: galleryKey(galleryId || 0),
    queryFn: () => service.gallery.getGallery(galleryId!),
    enabled: !!galleryId && galleryId > 0,
  });
}
 
export function useGalleryEditInfo(galleryId: number | undefined) {
  return useQuery({
    queryKey: galleryEditInfoKey(galleryId || 0),
    queryFn: () => service.gallery.getGalleryEditInfo(galleryId!),
    enabled: !!galleryId && galleryId > 0,
  });
}
 
export function useAddPhotoToGallery(galleryId: number, userId?: number) {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({
      uploadKey,
      caption,
    }: {
      uploadKey: string;
      caption?: string;
    }) => service.gallery.addPhotoToGallery(galleryId, uploadKey, caption),
    onSuccess: (updatedGallery) => {
      queryClient.setQueryData(galleryKey(galleryId), updatedGallery);
      queryClient.invalidateQueries({
        queryKey: galleryEditInfoKey(galleryId),
      });
      // Invalidate user query to update avatar
      Iif (userId) {
        queryClient.invalidateQueries({
          queryKey: userKey(userId),
        });
      }
    },
  });
}
 
export function useRemovePhotoFromGallery(galleryId: number, userId?: number) {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (itemId: number) =>
      service.gallery.removePhotoFromGallery(galleryId, itemId),
    onSuccess: (updatedGallery) => {
      queryClient.setQueryData(galleryKey(galleryId), updatedGallery);
      queryClient.invalidateQueries({
        queryKey: galleryEditInfoKey(galleryId),
      });
      // Invalidate user query to update avatar
      Iif (userId) {
        queryClient.invalidateQueries({
          queryKey: userKey(userId),
        });
      }
    },
  });
}
 
export function useMovePhoto(galleryId: number, userId?: number) {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({
      itemId,
      afterItemId,
    }: {
      itemId: number;
      afterItemId: number;
    }) => service.gallery.movePhoto(galleryId, itemId, afterItemId),
    onSuccess: (updatedGallery) => {
      queryClient.setQueryData(galleryKey(galleryId), updatedGallery);
      // Invalidate user query to update avatar
      Iif (userId) {
        queryClient.invalidateQueries({
          queryKey: userKey(userId),
        });
      }
    },
  });
}
 
export function useUpdatePhotoCaption(galleryId: number) {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({ itemId, caption }: { itemId: number; caption: string }) =>
      service.gallery.updatePhotoCaption(galleryId, itemId, caption),
    onSuccess: (updatedGallery) => {
      queryClient.setQueryData(galleryKey(galleryId), updatedGallery);
    },
  });
}
 
// Type for the gallery object from proto
export type GalleryData = PhotoGallery.AsObject;
export type GalleryItemData = PhotoGallery.AsObject["photosList"][number];