import { useInfiniteQuery } from 'react-query';

import { POSTS_LIMIT_PER_PAGE } from 'src/constants/posts';
import useGeneralError from 'src/hooks/useGeneralError';
import { getScheduledPosts } from 'src/services/posts';
import { useLokaliseTranslation as useTranslation } from 'src/utils/i18n';

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

const useScheduledPostsQuery = () => {
  const { t } = useTranslation();
  const showGeneralError = useGeneralError();

  return useInfiniteQuery(
    feedKeys.scheduled(),
    ({ pageParam = '' }) => getScheduledPosts(pageParam, POSTS_LIMIT_PER_PAGE),
    {
      getNextPageParam: lastPage => lastPage?.data?.cursor,
      onError: err => {
        showGeneralError(err, t('post:error_loading_posts'));
      },
      keepPreviousData: true,
      staleTime: 5 * 1000,
    },
  );
};

export default useScheduledPostsQuery;
