import { useMutation } from 'react-query';

import useSnackbar from '@material-hu/components/design-system/Snackbar';

import { createShiftRotation } from 'src/services/shifts';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { invalidateShiftRotations } from '../queries';

export const useCreateShiftRotationMutation = () => {
  const { t } = useLokaliseTranslation('shifts');
  const { enqueueSnackbar } = useSnackbar();

  return useMutation({
    mutationFn: createShiftRotation,
    onSuccess: async () => {
      await invalidateShiftRotations();
      enqueueSnackbar({
        title: t('templates.feedback.success_template'),
        variant: 'success',
      });
    },
    onError: () => {
      enqueueSnackbar({
        title: t('templates.feedback.error_template'),
        description: t('actions.try_again'),
        variant: 'error',
      });
    },
  });
};
