import { useMutation, useQueryClient } from 'react-query';

import { goalsKeys } from '../../queries';
import { updateGoalProgress } from '../../services';
import { type GoalProgress } from '../../types';

export const useUpdateGoalProgress = (goalId: number) => {
  const queryClient = useQueryClient();

  return useMutation(
    (payload: GoalProgress) => updateGoalProgress(goalId, payload),
    {
      onSuccess: () => {
        queryClient.invalidateQueries({ queryKey: goalsKeys.lists() });
        queryClient.invalidateQueries({ queryKey: goalsKeys.detail(goalId) });
      },
    },
  );
};
