Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

909 B
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";
import uploadAvatar from "@/fetchers/user/upload-avatar";
import { authClient } from "@/lib/auth-client";
import { prepareAvatarImage } from "@/lib/prepare-avatar-image";
import invalidateUserProfileQueries from "./invalidate-user-profile-queries";

function useUpdateUserAvatar() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (file: File) => {
      const { contentType, data } = await prepareAvatarImage(file);
      const avatar = await uploadAvatar({ contentType, data });

      const { error } = await authClient.updateUser({ image: avatar.url });

      if (error) {
        throw new Error(error.message || "Failed to update profile picture");
      }

      return avatar;
    },
    onSuccess: () => invalidateUserProfileQueries(queryClient),
  });
}

export default useUpdateUserAvatar;