import { useMutation, useQuery } from "@tanstack/react-query";
import Axios from "../utils/ApiConfiguration";
import { toast } from "react-toastify";
/**
* Custom hook to fetch a list of events.
*
* This function makes an API call to retrieve a list of events.
*
* @returns {import("@tanstack/react-query").UseQueryResult} The query result object.
*
* @throws {Error} If the API call fails or encounters an error.
*
* @example
* const { data, isLoading, isError } = useGetEventsList();
* if (isLoading) {
* return <div>Loading events...</div>;
* }
* if (isError) {
* return <div>Error loading events</div>;
* }
* return (
* <div>
* * Render the list of events here
* </div>
* );
*/
export const useGetEventsList = () =>
useQuery({
queryKey: ["events"],
queryFn: () => Axios.get("event/"),
refetchOnWindowFocus: false,
});
/**
* Custom hook to register for an event.
*
* This function makes an API call to register for an event.
*
* @returns {import("@tanstack/react-query").UseMutationResult} The mutation result object.
*
* @param {object} data - The data for event registration.
*
* @throws {Error} If the API call fails or encounters an error.
*
* @example
* const { mutate, isLoading, isError } = useRegisterEvent();
* const registrationData = { eventId: "event123", userId: "user456" };
*
* const handleRegistration = async () => {
* try {
* await mutate(registrationData);
* toast.success("Event registration successful");
* } catch (error) {
* toast.error("Event registration failed");
* }
* };
*/
export const useRegisterEvent = () =>
useMutation({
mutationFn: (data) => Axios.post("registerevent/", data),
onError: (err) => {
let error = Object.values(err?.response?.data?.errors || {});
toast.error(error[0][0]);
},
});
Source