UNPKG

burdy

Version:

Open Source Headless Platform

116 lines (115 loc) 8.5 kB
"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 react_1 = __importStar(require("react")); const react_2 = require("@fluentui/react"); const hoc_1 = require("../helpers/hoc"); const react_hook_form_1 = require("react-hook-form"); // eslint-disable-next-line max-len const dynamic_form_1 = require("./dynamic-form"); const posts_context_1 = require("../features/posts/context/posts.context"); const uuid_1 = require("uuid"); const posts_select_panel_1 = __importDefault(require("../features/posts/components/posts-select-panel")); const status_1 = __importDefault(require("../components/status")); const theme = (0, react_2.getTheme)(); const styles = (0, react_2.mergeStyleSets)({ headingWrapper: { display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 20, border: `1px solid ${theme.palette.neutralLight}`, marginBottom: 20, }, component: { position: 'relative', }, }); const DynamicReferenceImpl = ({ field, name, }) => { var _a, _b; const [selectPostOpen, setSelectPostOpen] = (0, react_1.useState)(false); const { control, disabled } = (0, dynamic_form_1.useExtendedFormContext)(); const { getBySlug } = (0, posts_context_1.usePosts)(); return (react_1.default.createElement("div", null, ((_a = field === null || field === void 0 ? void 0 : field.label) === null || _a === void 0 ? void 0 : _a.length) > 0 && react_1.default.createElement(react_2.Label, null, `${field === null || field === void 0 ? void 0 : field.label} (Single)`), react_1.default.createElement(react_hook_form_1.Controller, { name: name, control: control, defaultValue: (_b = getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.result) === null || _b === void 0 ? void 0 : _b.slugPath, render: ({ field: controllerField }) => { var _a, _b, _c, _d, _e, _f, _g; const { setLoadingContent, getBySlug } = (0, posts_context_1.usePosts)(); (0, react_1.useEffect)(() => { var _a, _b; if ((_a = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _a === void 0 ? void 0 : _a.slugPath) { getBySlug.execute((_b = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _b === void 0 ? void 0 : _b.slugPath); } else { controllerField.onChange(null); } }, [(_a = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _a === void 0 ? void 0 : _a.slugPath]); (0, react_1.useEffect)(() => { const id = (0, uuid_1.v4)(); setLoadingContent(id, getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.loading); return () => { setLoadingContent(id, false); }; }, [getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.loading]); return (react_1.default.createElement("div", null, ((_b = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _b === void 0 ? void 0 : _b.slugPath) && (react_1.default.createElement("div", { className: styles.component }, react_1.default.createElement("div", { className: styles.headingWrapper }, react_1.default.createElement(react_2.Stack, { style: { overflowX: 'auto', alignItems: 'flex-start', } }, react_1.default.createElement(react_2.Label, { style: { overflow: 'hidden', wordBreak: 'break-all' } }, (typeof ((_c = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _c === void 0 ? void 0 : _c.slugPath) === 'string') ? (_d = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _d === void 0 ? void 0 : _d.slugPath : ''), (getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.error) ? (react_1.default.createElement(status_1.default, { type: "error" }, "Invalid post")) : (react_1.default.createElement(status_1.default, { type: ((_e = getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.result) === null || _e === void 0 ? void 0 : _e.status) === 'published' ? 'success' : undefined }, (_f = getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.result) === null || _f === void 0 ? void 0 : _f.status))), react_1.default.createElement(react_2.Stack, { horizontal: true, tokens: { childrenGap: 8 } }, react_1.default.createElement(react_2.IconButton, { disabled: (getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.loading) || !!(getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.error), iconProps: { iconName: 'OpenInNewTab', }, title: "Open in new tab", onClick: () => { var _a; window.open(`/admin/sites/editor/${(_a = getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.result) === null || _a === void 0 ? void 0 : _a.id}`, '_blank'); } }), react_1.default.createElement(react_2.IconButton, { iconProps: { iconName: 'Delete' }, disabled: (getBySlug === null || getBySlug === void 0 ? void 0 : getBySlug.loading) || disabled, title: "Delete", ariaLabel: "Delete", onClick: () => controllerField.onChange(null) }))))), !((_g = controllerField === null || controllerField === void 0 ? void 0 : controllerField.value) === null || _g === void 0 ? void 0 : _g.slugPath) && (react_1.default.createElement(react_2.Separator, { style: { marginTop: 30 } }, react_1.default.createElement(react_2.ActionButton, { disabled: disabled, iconProps: { iconName: 'Add', }, onClick: () => { setSelectPostOpen(true); } }, "Add reference"))), react_1.default.createElement(posts_select_panel_1.default, { isOpen: selectPostOpen, selectionMode: react_2.SelectionMode.single, onDismiss: () => setSelectPostOpen(false), onSubmit: (data) => { var _a; controllerField.onChange({ slugPath: (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.slugPath, }); setSelectPostOpen(false); }, params: { contentTypeName: field.posts, } }))); } }))); }; const DynamicReference = (0, hoc_1.composeWrappers)({ postsContext: posts_context_1.PostsContextProvider, })(DynamicReferenceImpl); exports.default = DynamicReference;