mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2025-05-05 09:40:54 +02:00
Compare commits
No commits in common. "13ca74541237bad9548bb22e53be89d0614897e4" and "0a839bf3690343b3d7045349c538d938a548db91" have entirely different histories.
13ca745412
...
0a839bf369
@ -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.
|
* This is free software, licensed under the GNU General Public License v3.
|
||||||
* See /LICENSE for more information.
|
* See /LICENSE for more information.
|
||||||
@ -9,7 +9,7 @@ import React, { useState, useEffect } from "react";
|
|||||||
|
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
import { useAPIPost, useAPIPut } from "../../api/hooks";
|
import { useAPIPost } from "../../api/hooks";
|
||||||
import { API_STATE } from "../../api/utils";
|
import { API_STATE } from "../../api/utils";
|
||||||
import Button from "../../bootstrap/Button";
|
import Button from "../../bootstrap/Button";
|
||||||
import {
|
import {
|
||||||
@ -23,8 +23,6 @@ import { useAlert } from "../../context/alertContext/AlertContext";
|
|||||||
ActionButtonWithModal.propTypes = {
|
ActionButtonWithModal.propTypes = {
|
||||||
/** Component that triggers the action. */
|
/** Component that triggers the action. */
|
||||||
actionTrigger: PropTypes.elementType.isRequired,
|
actionTrigger: PropTypes.elementType.isRequired,
|
||||||
/** Method to use for the action. */
|
|
||||||
actionMethod: PropTypes.string,
|
|
||||||
/** URL to send the action to. */
|
/** URL to send the action to. */
|
||||||
actionUrl: PropTypes.string.isRequired,
|
actionUrl: PropTypes.string.isRequired,
|
||||||
/** Title of the modal. */
|
/** Title of the modal. */
|
||||||
@ -43,7 +41,6 @@ ActionButtonWithModal.propTypes = {
|
|||||||
|
|
||||||
function ActionButtonWithModal({
|
function ActionButtonWithModal({
|
||||||
actionTrigger: ActionTriggerComponent,
|
actionTrigger: ActionTriggerComponent,
|
||||||
actionMethod = "POST",
|
|
||||||
actionUrl,
|
actionUrl,
|
||||||
modalTitle,
|
modalTitle,
|
||||||
modalMessage,
|
modalMessage,
|
||||||
@ -54,43 +51,24 @@ function ActionButtonWithModal({
|
|||||||
}) {
|
}) {
|
||||||
const [triggered, setTriggered] = useState(false);
|
const [triggered, setTriggered] = useState(false);
|
||||||
const [modalShown, setModalShown] = useState(false);
|
const [modalShown, setModalShown] = useState(false);
|
||||||
const [triggerPostActionStatus, triggerPostAction] = useAPIPost(actionUrl);
|
const [triggerActionStatus, triggerAction] = useAPIPost(actionUrl);
|
||||||
const [triggerPutActionStatus, triggerPutAction] = useAPIPut(actionUrl);
|
|
||||||
|
|
||||||
const [setAlert] = useAlert();
|
const [setAlert] = useAlert();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (triggerActionStatus.state === API_STATE.SUCCESS) {
|
||||||
triggerPostActionStatus.state === API_STATE.SUCCESS ||
|
|
||||||
triggerPutActionStatus.state === API_STATE.SUCCESS
|
|
||||||
) {
|
|
||||||
setAlert(
|
setAlert(
|
||||||
successMessage || _("Action successful."),
|
successMessage || _("Action successful."),
|
||||||
API_STATE.SUCCESS
|
API_STATE.SUCCESS
|
||||||
);
|
);
|
||||||
setTriggered(false);
|
|
||||||
}
|
}
|
||||||
if (
|
if (triggerActionStatus.state === API_STATE.ERROR) {
|
||||||
triggerPostActionStatus.state === API_STATE.ERROR ||
|
|
||||||
triggerPutActionStatus.state === API_STATE.ERROR
|
|
||||||
) {
|
|
||||||
setAlert(errorMessage || _("Action failed."));
|
setAlert(errorMessage || _("Action failed."));
|
||||||
setTriggered(false);
|
|
||||||
}
|
}
|
||||||
}, [
|
}, [triggerActionStatus, setAlert, successMessage, errorMessage]);
|
||||||
triggerPostActionStatus,
|
|
||||||
triggerPutActionStatus,
|
|
||||||
setAlert,
|
|
||||||
successMessage,
|
|
||||||
errorMessage,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const actionHandler = () => {
|
const actionHandler = () => {
|
||||||
setTriggered(true);
|
setTriggered(true);
|
||||||
if (actionMethod === "POST") {
|
triggerAction();
|
||||||
triggerPostAction();
|
|
||||||
} else {
|
|
||||||
triggerPutAction();
|
|
||||||
}
|
|
||||||
setModalShown(false);
|
setModalShown(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user