Merge pull request #993 from othrayte/simplify-isloading

website: Simplify isLoading handling
This commit is contained in:
AbdBarho
2023-01-29 11:00:48 +01:00
committed by GitHub
@@ -7,19 +7,16 @@ import useSWRMutation from "swr/mutation";
export const useGenericTaskAPI = <TaskType extends BaseTask>(taskType: TaskTypeEnum): TaskApiHook<TaskType> => {
const [response, setReponse] = useState<TaskResponse<TaskType>>({ taskAvailability: "AWAITING_INITIAL" });
const [isLoading, setIsLoading] = useState(true);
const { mutate: requestNewTask } = useSWRImmutable<TaskAvailableResponse<TaskType>>(
// Note: We use isValidating to indiate we are loading beause it signals eash load, not just the first one.
const { isValidating: isLoading, mutate: requestNewTask } = useSWRImmutable<TaskAvailableResponse<TaskType>>(
"/api/new_task/" + taskType,
get,
{
onSuccess: (response) => {
setIsLoading(false);
setReponse({ taskAvailability: "AVAILABLE", ...response });
},
onError: () => {
// We could check for code 503 here for truely unavailable, but we need to do something with other errors anyway.
setIsLoading(false);
setReponse({ taskAvailability: "NONE_AVAILABLE" });
},
revalidateOnMount: true,
@@ -29,12 +26,10 @@ export const useGenericTaskAPI = <TaskType extends BaseTask>(taskType: TaskTypeE
const { trigger: completeTask } = useSWRMutation<TaskAvailableResponse<TaskType>>("/api/update_task", post, {
onSuccess: () => {
setIsLoading(true);
requestNewTask();
},
onError: () => {
// We could check for code 503 here for truely unavailable, but we need to do something with other errors anyway.
setIsLoading(false);
setReponse({ taskAvailability: "NONE_AVAILABLE" });
},
});