burdy
Version:
Open Source Headless Platform
87 lines (86 loc) • 4.52 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const rhf_components_1 = require("../../../components/rhf-components");
const posts_context_1 = require("../context/posts.context");
const react_1 = require("@fluentui/react");
const react_2 = __importStar(require("react"));
const react_hook_form_1 = require("react-hook-form");
const validators_1 = require("../../../../shared/validators");
const slugify_1 = __importDefault(require("slugify"));
const PostDuplicateDialog = ({ isOpen, post, onDismiss, onCreated, }) => {
var _a;
const { copyPosts } = (0, posts_context_1.usePosts)();
const { control, handleSubmit, reset, watch, formState, setValue } = (0, react_hook_form_1.useForm)({
mode: 'all',
});
(0, react_2.useEffect)(() => {
var _a;
if (!((_a = formState === null || formState === void 0 ? void 0 : formState.dirtyFields) === null || _a === void 0 ? void 0 : _a.slug) && watch('name')) {
const parsed = (0, slugify_1.default)(watch('name'), {
replacement: '-',
remove: undefined,
lower: true,
});
setValue('slug', parsed);
}
}, [watch('name')]);
(0, react_2.useEffect)(() => {
copyPosts.reset();
reset();
}, [isOpen]);
(0, react_2.useEffect)(() => {
if (isOpen && (copyPosts === null || copyPosts === void 0 ? void 0 : copyPosts.result)) {
onCreated(copyPosts === null || copyPosts === void 0 ? void 0 : copyPosts.result);
}
}, [copyPosts === null || copyPosts === void 0 ? void 0 : copyPosts.result]);
return (react_2.default.createElement(react_1.Dialog, { hidden: !isOpen, onDismiss: onDismiss, dialogContentProps: {
type: react_1.DialogType.close,
title: `Duplicate ${post === null || post === void 0 ? void 0 : post.name}`,
}, modalProps: {
styles: { main: { maxWidth: 450 } },
} },
react_2.default.createElement(react_1.Stack, { tokens: {
childrenGap: 8,
} },
((_a = copyPosts.error) === null || _a === void 0 ? void 0 : _a.message) && (react_2.default.createElement(react_1.MessageBar, { messageBarType: react_1.MessageBarType.error }, copyPosts.error.message)),
react_2.default.createElement(rhf_components_1.ControlledTextField, { rules: {
required: 'Name is required',
}, name: "name", label: "Name", control: control }),
react_2.default.createElement(rhf_components_1.ControlledTextField, { name: "slug", label: "Slug", control: control, rules: {
required: 'Slug is required',
pattern: {
value: validators_1.slugRegex,
message: validators_1.slugRegexMessage,
}
} })),
react_2.default.createElement(react_1.DialogFooter, null,
react_2.default.createElement(react_1.DefaultButton, { onClick: onDismiss, text: "Cancel" }),
react_2.default.createElement(react_1.PrimaryButton, { onClick: () => {
handleSubmit((data) => {
copyPosts.execute(post === null || post === void 0 ? void 0 : post.id, Object.assign({}, data));
})();
}, text: "Duplicate", disabled: copyPosts === null || copyPosts === void 0 ? void 0 : copyPosts.loading }))));
};
exports.default = PostDuplicateDialog;