import { useState } from "react"; import { get, post } from "src/lib/api"; import { BaseTask, TaskResponse } from "src/types/Task"; import useSWRImmutable from "swr/immutable"; import useSWRMutation from "swr/mutation"; export const useGenericTaskAPI = (taskApiEndpoint: string) => { type ConcreteTaskResponse = TaskResponse; const [tasks, setTasks] = useState([]); const { isLoading, mutate, error } = useSWRImmutable("/api/new_task/" + taskApiEndpoint, get, { onSuccess: (data) => setTasks([data]), revalidateOnMount: true, dedupingInterval: 500, }); const { trigger } = useSWRMutation("/api/update_task", post, { onSuccess: async (response) => { const newTask: ConcreteTaskResponse = response; setTasks((oldTasks) => [...oldTasks, newTask]); mutate(); }, }); return { tasks, isLoading, trigger, error, reset: mutate }; };