chore(ui): migrate more code to typescript

This commit is contained in:
Łukasz Mierzwa
2020-06-29 16:14:53 +01:00
committed by Łukasz Mierzwa
parent 55170f8812
commit 4d4dd111c1
36 changed files with 392 additions and 205 deletions

View 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 };