This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
2022-03-24 18:13:19 +00:00
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import accountsService from "../../services/accountsService";
|
|
|
|
|
|
|
|
export const useAPIKeyRemoval = ({ key, onSuccess }) => {
|
|
|
|
const [removalInitiated, setRemovalInitiated] = useState(false);
|
|
|
|
const [removalError, setRemovalError] = useState(null);
|
|
|
|
|
2022-03-25 10:22:36 +00:00
|
|
|
const prompt = () => {
|
|
|
|
setRemovalError(null);
|
|
|
|
setRemovalInitiated(true);
|
|
|
|
};
|
2022-03-24 18:13:19 +00:00
|
|
|
const abort = () => setRemovalInitiated(false);
|
|
|
|
|
|
|
|
const confirm = useCallback(async () => {
|
|
|
|
setRemovalError(null);
|
|
|
|
try {
|
|
|
|
await accountsService.delete(`user/apikeys/${key.id}`);
|
|
|
|
setRemovalInitiated(false);
|
|
|
|
onSuccess();
|
|
|
|
} catch (err) {
|
|
|
|
let message = "There was an error processing your request. Please try again later.";
|
|
|
|
|
|
|
|
if (err.response) {
|
|
|
|
const response = await err.response.json();
|
|
|
|
if (response.message) {
|
|
|
|
message = response.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setRemovalError(message);
|
|
|
|
}
|
|
|
|
}, [onSuccess, key]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
removalInitiated,
|
|
|
|
removalError,
|
|
|
|
prompt,
|
|
|
|
abort,
|
|
|
|
confirm,
|
|
|
|
};
|
|
|
|
};
|