import { useQuery } from 'react-query';

import { secondsToMilliseconds } from 'date-fns';

import useGeneralError from 'src/hooks/useGeneralError';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { acknowledgementsKeys } from '../queries';
import { getActiveKudosCategories } from '../services';

export const useGetActiveKudosCategories = () => {
  const showGeneralError = useGeneralError();
  const { t } = useLokaliseTranslation('acknowledgements');

  const { data, ...query } = useQuery({
    queryKey: acknowledgementsKeys.categories.active(),
    queryFn: getActiveKudosCategories,
    onError: err => showGeneralError(err, t('error_get_categories')),
    select: response => response.data,
    staleTime: secondsToMilliseconds(60),
  });

  return { activeCategories: data, ...query };
};
