/*
* 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.
*/
import React, { useEffect } from "react";
import PropTypes from "prop-types";
import { Prompt } from "react-router-dom";
import { STATES as SUBMIT_BUTTON_STATES, SubmitButton } from "./SubmitButton";
import { useAPIPost } from "../../api/hooks";
import { API_STATE } from "../../api/utils";
import { ALERT_TYPES } from "../../bootstrap/Alert";
import { formFieldsSize } from "../../bootstrap/constants";
import { Spinner } from "../../bootstrap/Spinner";
import { useAlert } from "../../context/alertContext/AlertContext";
import ErrorMessage from "../../utils/ErrorMessage";
import { useForisModule, useForm } from "../hooks";
ForisForm.propTypes = {
/** Optional WebSocket object. See `scr/common/WebSockets.js`.
* `forisConfig.wsModule` should be specified when it's passed.
* */
ws: PropTypes.object,
/** Foris configuration object. See usage in main components. */
forisConfig: PropTypes.shape({
/** reForis Flask application API endpoint from `src/common/API.js`. */
endpoint: PropTypes.string.isRequired,
/** `foris-controller` module name to be used via WebSockets.
* It can be use only with `ws` prop.
* */
wsModule: PropTypes.string,
/** `foris-controller` action name to be used via WebSockets.
* If it's not passed then `update_settings` is used. see `src/common/WebSocketHooks.js`
* */
wsAction: PropTypes.string,
}).isRequired,
/** Function to prepare data received from the API before using in forms. */
prepData: PropTypes.func,
/** Function to prepare data from form before submitting. */
prepDataToSubmit: PropTypes.func,
/** Function to handle response to POST request. */
postCallback: PropTypes.func,
/** Validate data and provide validation object. Then validation errors passed to children. */
validator: PropTypes.func,
/** Disables form */
disabled: PropTypes.bool,
/** Optional override of form submit callback */
onSubmitOverridden: PropTypes.func,
/** Reference to actual form element (useful for programmatically submitting it).
* Pass the output of useRef hook to this prop.
*/
formReference: PropTypes.object,
/** reForis form components. */
children: PropTypes.node.isRequired,
// eslint-disable-next-line react/no-unused-prop-types
customWSProp(props) {
const wsModuleIsSpecified = !!(
props.forisConfig && props.forisConfig.wsModule
);
if (props.ws && !wsModuleIsSpecified) {
return new Error(
"forisConfig.wsModule should be specified when ws object is passed."
);
}
if (!props.ws && wsModuleIsSpecified) {
return new Error(
"forisConfig.wsModule is specified without passing ws object."
);
}
},
};
ForisForm.defaultProps = {
prepData: (data) => data,
prepDataToSubmit: (data) => data,
postCallback: () => undefined,
validator: () => undefined,
disabled: false,
};
/** Serves as HOC for all foris forms components.
*
* As `