envoc-form
Version:
Envoc form components
61 lines (60 loc) • 3.02 kB
JavaScript
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;
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
import ConfirmBaseForm from '../ConfirmBaseForm/ConfirmBaseForm';
import { FormDefaults } from '../FormDefaults';
/**
* Deletion confirmation. Navigates to a different route to allow the user to either confirm or cancel an action.
*
* Wraps `<ConfirmBaseForm/>`.
*/
export default 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 = useNavigate();
var entityId = useParams().entityId;
var onComplete = handleComplete !== null && handleComplete !== void 0 ? handleComplete : function () {
toast.success('Deleted!');
goBack();
};
var onError = handleError !== null && handleError !== void 0 ? handleError : function (error) {
var _a, _b, _c, _d;
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 (_jsx(ConfirmBaseForm, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?', confirmButtonText: "Yes", confirmButtonClass: FormDefaults.cssClassPrefix + 'confirm-delete-form-yes-button' }, props, { children: children })));
function goBack() {
if (successUrl) {
navigate(successUrl);
}
else {
navigate(-1);
}
}
}