UNPKG

envoc-form

Version:

Envoc form components

67 lines (66 loc) 3.42 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var jsx_runtime_1 = require("react/jsx-runtime"); var react_router_dom_1 = require("react-router-dom"); var react_toastify_1 = require("react-toastify"); var ConfirmBaseForm_1 = __importDefault(require("../ConfirmBaseForm/ConfirmBaseForm")); var FormDefaults_1 = require("../FormDefaults"); /** * Deletion confirmation. Navigates to a different route to allow the user to either confirm or cancel an action. * * Wraps `<ConfirmBaseForm/>`. */ function ConfirmDeleteForm(_a) { var successUrl = _a.successUrl, form = _a.form, title = _a.title, handleComplete = _a.handleComplete, handleError = _a.handleError, children = _a.children, props = __rest(_a, ["successUrl", "form", "title", "handleComplete", "handleError", "children"]); var navigate = (0, react_router_dom_1.useNavigate)(); var entityId = (0, react_router_dom_1.useParams)().entityId; var onComplete = handleComplete !== null && handleComplete !== void 0 ? handleComplete : function () { react_toastify_1.toast.success('Deleted!'); goBack(); }; var onError = handleError !== null && handleError !== void 0 ? handleError : function (error) { var _a, _b, _c, _d; react_toastify_1.toast.error((_d = (_c = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.validationFailures[0]) === null || _c === void 0 ? void 0 : _c.errorMessage) !== null && _d !== void 0 ? _d : 'Something went wrong talking to the portal. Contact support if this continues.'); }; var request = { method: 'delete', url: "/api/".concat(form, "/").concat(entityId), onComplete: onComplete, onError: onError, }; return ((0, jsx_runtime_1.jsx)(ConfirmBaseForm_1.default, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?', confirmButtonText: "Yes", confirmButtonClass: FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-delete-form-yes-button' }, props, { children: children }))); function goBack() { if (successUrl) { navigate(successUrl); } else { navigate(-1); } } } exports.default = ConfirmDeleteForm;