Source

apis/News.js

import { useMutation, useQuery } from "@tanstack/react-query";
import Axios from "../utils/ApiConfiguration";
/**
 * Custom hook to fetch a list of news articles.
 *
 * This hook sends a request to retrieve news articles from the server.
 *
 * @returns {Object} The query result object.
 * @throws {Error} If there's an issue with fetching news articles.
 *
 * @example
 * // Usage of useGetNews hook
 * const { data, isLoading, error } = useGetNews();
 */
export const useGetNews = () =>
  useQuery({
    queryKey: ["news"],
    queryFn: () => Axios.get("news/"),
    refetchOnWindowFocus: false,
  });
/**
 * Custom hook to fetch a single news article by ID.
 *
 * This hook sends a request to retrieve a single news article from the server by its ID.
 *
 * @param {number} id - The ID of the news article to fetch.
 * @returns {Object} The query result object.
 * @throws {Error} If there's an issue with fetching the single news article.
 *
 * @example
 * // Usage of useGetSingleNews hook
 * const { data, isLoading, error } = useGetSingleNews(123); // Replace 123 with the desired news article ID.
 */
export const useGetSingleNews = (id) =>
  useQuery({
    queryKey: ["single_news", id],
    queryFn: () => Axios.get(`news/${id}/`),
    refetchOnWindowFocus: false,
  });