Use swagger api for delete Policy (#3198)
This commit is contained in:
@@ -14,14 +14,15 @@
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import React, { Fragment } from "react";
|
||||
import { ErrorResponseHandler } from "../../../common/types";
|
||||
import useApi from "../Common/Hooks/useApi";
|
||||
import React, { Fragment, useState } from "react";
|
||||
import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
|
||||
import { ConfirmDeleteIcon } from "mds";
|
||||
import { encodeURLString } from "../../../common/utils";
|
||||
import { setErrorSnackMessage } from "../../../systemSlice";
|
||||
import { useAppDispatch } from "../../../store";
|
||||
import { api } from "api";
|
||||
import { ApiError, HttpResponse } from "api/consoleApi";
|
||||
import { errorToHandler } from "api/errors";
|
||||
import { encodeURLString } from "common/utils";
|
||||
|
||||
interface IDeletePolicyProps {
|
||||
closeDeleteModalAndRefresh: (refresh: boolean) => void;
|
||||
@@ -35,22 +36,27 @@ const DeletePolicy = ({
|
||||
selectedPolicy,
|
||||
}: IDeletePolicyProps) => {
|
||||
const dispatch = useAppDispatch();
|
||||
const onDelSuccess = () => closeDeleteModalAndRefresh(true);
|
||||
const onDelError = (err: ErrorResponseHandler) =>
|
||||
dispatch(setErrorSnackMessage(err));
|
||||
const onClose = () => closeDeleteModalAndRefresh(false);
|
||||
|
||||
const [deleteLoading, invokeDeleteApi] = useApi(onDelSuccess, onDelError);
|
||||
const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
|
||||
|
||||
if (!selectedPolicy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const onConfirmDelete = () => {
|
||||
invokeDeleteApi(
|
||||
"DELETE",
|
||||
`/api/v1/policy/${encodeURLString(selectedPolicy)}`,
|
||||
);
|
||||
setLoadingDelete(true);
|
||||
api.policy
|
||||
.removePolicy(encodeURLString(selectedPolicy))
|
||||
.then((_) => {
|
||||
closeDeleteModalAndRefresh(true);
|
||||
})
|
||||
.catch(async (res: HttpResponse<void, ApiError>) => {
|
||||
const err = (await res.json()) as ApiError;
|
||||
dispatch(setErrorSnackMessage(errorToHandler(err)));
|
||||
closeDeleteModalAndRefresh(false);
|
||||
})
|
||||
.finally(() => setLoadingDelete(false));
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -59,7 +65,7 @@ const DeletePolicy = ({
|
||||
confirmText={"Delete"}
|
||||
isOpen={deleteOpen}
|
||||
titleIcon={<ConfirmDeleteIcon />}
|
||||
isLoading={deleteLoading}
|
||||
isLoading={loadingDelete}
|
||||
onConfirm={onConfirmDelete}
|
||||
onClose={onClose}
|
||||
confirmationContent={
|
||||
|
||||
Reference in New Issue
Block a user