1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-04-20 08:16:38 +02:00

Compare commits

..

No commits in common. "13ca74541237bad9548bb22e53be89d0614897e4" and "0a839bf3690343b3d7045349c538d938a548db91" have entirely different histories.

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/)
* Copyright (C) 2019-2024 CZ.NIC z.s.p.o. (https://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
@ -9,7 +9,7 @@ import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import { useAPIPost, useAPIPut } from "../../api/hooks";
import { useAPIPost } from "../../api/hooks";
import { API_STATE } from "../../api/utils";
import Button from "../../bootstrap/Button";
import {
@ -23,8 +23,6 @@ import { useAlert } from "../../context/alertContext/AlertContext";
ActionButtonWithModal.propTypes = {
/** Component that triggers the action. */
actionTrigger: PropTypes.elementType.isRequired,
/** Method to use for the action. */
actionMethod: PropTypes.string,
/** URL to send the action to. */
actionUrl: PropTypes.string.isRequired,
/** Title of the modal. */
@ -43,7 +41,6 @@ ActionButtonWithModal.propTypes = {
function ActionButtonWithModal({
actionTrigger: ActionTriggerComponent,
actionMethod = "POST",
actionUrl,
modalTitle,
modalMessage,
@ -54,43 +51,24 @@ function ActionButtonWithModal({
}) {
const [triggered, setTriggered] = useState(false);
const [modalShown, setModalShown] = useState(false);
const [triggerPostActionStatus, triggerPostAction] = useAPIPost(actionUrl);
const [triggerPutActionStatus, triggerPutAction] = useAPIPut(actionUrl);
const [triggerActionStatus, triggerAction] = useAPIPost(actionUrl);
const [setAlert] = useAlert();
useEffect(() => {
if (
triggerPostActionStatus.state === API_STATE.SUCCESS ||
triggerPutActionStatus.state === API_STATE.SUCCESS
) {
if (triggerActionStatus.state === API_STATE.SUCCESS) {
setAlert(
successMessage || _("Action successful."),
API_STATE.SUCCESS
);
setTriggered(false);
}
if (
triggerPostActionStatus.state === API_STATE.ERROR ||
triggerPutActionStatus.state === API_STATE.ERROR
) {
if (triggerActionStatus.state === API_STATE.ERROR) {
setAlert(errorMessage || _("Action failed."));
setTriggered(false);
}
}, [
triggerPostActionStatus,
triggerPutActionStatus,
setAlert,
successMessage,
errorMessage,
]);
}, [triggerActionStatus, setAlert, successMessage, errorMessage]);
const actionHandler = () => {
setTriggered(true);
if (actionMethod === "POST") {
triggerPostAction();
} else {
triggerPutAction();
}
triggerAction();
setModalShown(false);
};