mirror of
https://github.com/prymitive/karma
synced 2026-05-11 03:46:48 +00:00
chore(ui): migrate more code to typescript
This commit is contained in:
committed by
Łukasz Mierzwa
parent
55170f8812
commit
4d4dd111c1
51
ui/src/Hooks/useFetchDelete.ts
Normal file
51
ui/src/Hooks/useFetchDelete.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
import merge from "lodash.merge";
|
||||
|
||||
import { CommonOptions } from "Common/Fetch";
|
||||
|
||||
const useFetchDelete = (uri: string, options: RequestInit, deps = []) => {
|
||||
const [response, setResponse] = useState(null as string | null);
|
||||
const [error, setError] = useState(null as string | null);
|
||||
const [isDeleting, setIsDeleting] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// https://dev.to/pallymore/clean-up-async-requests-in-useeffect-hooks-90h
|
||||
let isCancelled = false;
|
||||
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
setIsDeleting(true);
|
||||
const res = await fetch(
|
||||
uri,
|
||||
merge({}, { method: "DELETE" }, CommonOptions, options)
|
||||
);
|
||||
const text = await res.text();
|
||||
|
||||
if (!isCancelled) {
|
||||
if (res.ok) {
|
||||
setResponse(text);
|
||||
setIsDeleting(false);
|
||||
} else {
|
||||
setError(text);
|
||||
setIsDeleting(false);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (!isCancelled) {
|
||||
setError(error.message);
|
||||
setIsDeleting(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
fetchData();
|
||||
|
||||
return () => {
|
||||
isCancelled = true;
|
||||
};
|
||||
}, [uri, options, ...deps]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
return { response, error, isDeleting };
|
||||
};
|
||||
|
||||
export { useFetchDelete };
|
||||
Reference in New Issue
Block a user