mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-07-05 17:30:48 +08:00
Fetch available tasks
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
import { useState } from "react";
|
||||
import { get, post } from "src/lib/api";
|
||||
import { BaseTask, TaskResponse } from "src/types/Task";
|
||||
import { BaseTask, TaskResponse, TaskType as TaskTypeEnum } from "src/types/Task";
|
||||
import useSWRImmutable from "swr/immutable";
|
||||
import useSWRMutation from "swr/mutation";
|
||||
|
||||
export const useGenericTaskAPI = <TaskType extends BaseTask>(taskApiEndpoint: string) => {
|
||||
export const useGenericTaskAPI = <TaskType extends BaseTask>(taskType: TaskTypeEnum) => {
|
||||
type ConcreteTaskResponse = TaskResponse<TaskType>;
|
||||
|
||||
const [tasks, setTasks] = useState<ConcreteTaskResponse[]>([]);
|
||||
|
||||
const { isLoading, mutate, error } = useSWRImmutable<ConcreteTaskResponse>("/api/new_task/" + taskApiEndpoint, get, {
|
||||
const { isLoading, mutate, error } = useSWRImmutable<ConcreteTaskResponse>("/api/new_task/" + taskType, get, {
|
||||
onSuccess: (data) => setTasks([data]),
|
||||
revalidateOnMount: true,
|
||||
dedupingInterval: 500,
|
||||
});
|
||||
|
||||
const { trigger } = useSWRMutation("/api/update_task", post, {
|
||||
onSuccess: async (response) => {
|
||||
const newTask: ConcreteTaskResponse = response;
|
||||
onSuccess: async (newTask: ConcreteTaskResponse) => {
|
||||
setTasks((oldTasks) => [...oldTasks, newTask]);
|
||||
mutate();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user