add hook useProfileMutation

This commit is contained in:
vchikalkin 2025-02-08 21:27:17 +03:00
parent 5958fb443e
commit d73879d0a6
3 changed files with 14 additions and 7 deletions

View File

@ -3,7 +3,6 @@ import { authOptions } from '@/config/auth';
import { getCustomer, updateCustomerProfile } from '@repo/graphql/api';
import { type CustomerInput, type GetCustomerQueryVariables } from '@repo/graphql/types';
import { getServerSession } from 'next-auth/next';
import { revalidatePath } from 'next/cache';
export async function getProfile(input?: GetCustomerQueryVariables) {
const session = await getServerSession(authOptions);
@ -30,6 +29,4 @@ export async function updateProfile(input: CustomerInput) {
data: input,
documentId: customer.documentId,
});
revalidatePath('/profile');
}

View File

@ -2,8 +2,7 @@
import { type ProfileProps } from '../types';
import { CheckboxWithText, DataField, ProfileCardHeader } from './components';
import { updateRole } from './lib/actions';
import { updateProfile } from '@/actions/profile';
import { useProfile } from '@/hooks/profile';
import { useProfile, useProfileMutation } from '@/hooks/profile';
import { Button } from '@repo/ui/components/ui/button';
import { Card } from '@repo/ui/components/ui/card';
import Link from 'next/link';
@ -31,6 +30,7 @@ export function ContactDataCard({ telegramId }: Readonly<ProfileProps>) {
export function ProfileDataCard() {
const { data: customer } = useProfile({});
const { mutate: updateProfile } = useProfileMutation({});
if (!customer) return null;

View File

@ -1,7 +1,7 @@
'use client';
import { getProfile } from '@/actions/profile';
import { getProfile, updateProfile } from '@/actions/profile';
import { type ProfileProps } from '@/components/profile/types';
import { useQuery } from '@tanstack/react-query';
import { useMutation, useQuery } from '@tanstack/react-query';
export const useProfile = ({ telegramId }: ProfileProps) => {
return useQuery({
@ -9,3 +9,13 @@ export const useProfile = ({ telegramId }: ProfileProps) => {
queryKey: telegramId ? ['profile', 'telegramId', telegramId] : ['profile'],
});
};
export const useProfileMutation = ({ telegramId }: ProfileProps) => {
const { refetch } = useProfile({ telegramId });
return useMutation({
mutationFn: updateProfile,
mutationKey: ['profile', 'telegramId', telegramId, 'update'],
onSuccess: () => refetch(),
});
};