@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
120 lines (119 loc) • 6.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UnpublishAction = void 0;
var _reactHooks = require("@sanity/react-hooks");
var _icons = require("@sanity/icons");
var _react = _interopRequireWildcard(require("react"));
var _hooks = require("@sanity/base/hooks");
var _components = require("@sanity/base/components");
var _confirmDeleteDialog = require("../components/confirmDeleteDialog");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var DISABLED_REASON_TITLE = {
NOT_PUBLISHED: 'This document is not published'
};
var UnpublishAction = _ref => {
var id = _ref.id,
type = _ref.type,
draft = _ref.draft,
onComplete = _ref.onComplete,
liveEdit = _ref.liveEdit;
var _useDocumentOperation = (0, _reactHooks.useDocumentOperation)(id, type),
unpublish = _useDocumentOperation.unpublish;
var _useState = (0, _react.useState)(null),
_useState2 = _slicedToArray(_useState, 2),
error = _useState2[0],
setError = _useState2[1];
var _useState3 = (0, _react.useState)(false),
_useState4 = _slicedToArray(_useState3, 2),
didUnpublish = _useState4[0],
setDidUnpublish = _useState4[1];
var _useState5 = (0, _react.useState)(false),
_useState6 = _slicedToArray(_useState5, 2),
isConfirmDialogOpen = _useState6[0],
setConfirmDialogOpen = _useState6[1];
var _useDocumentPairPermi = (0, _hooks.unstable_useDocumentPairPermissions)({
id,
type,
permission: 'unpublish'
}),
_useDocumentPairPermi2 = _slicedToArray(_useDocumentPairPermi, 2),
permissions = _useDocumentPairPermi2[0],
isPermissionsLoading = _useDocumentPairPermi2[1];
var _useCurrentUser = (0, _hooks.useCurrentUser)(),
currentUser = _useCurrentUser.value;
var handleCancel = (0, _react.useCallback)(() => {
setConfirmDialogOpen(false);
onComplete();
}, [onComplete]);
var handleConfirm = (0, _react.useCallback)(() => {
setConfirmDialogOpen(false);
unpublish.execute();
onComplete();
}, [onComplete, unpublish]);
var dialog = (() => {
if (error) {
return {
type: 'error',
onClose: () => setError(null),
title: 'An error occurred',
content: error.message
};
}
if (didUnpublish) {
return {
type: 'success',
onClose: () => {
setDidUnpublish(false);
},
title: 'Successfully unpublished the document'
};
}
if (isConfirmDialogOpen) {
return {
type: 'legacy',
content: /*#__PURE__*/_react.default.createElement(_confirmDeleteDialog.ConfirmDeleteDialog, {
id: (draft === null || draft === void 0 ? void 0 : draft._id) || id,
type: type,
action: "unpublish",
onCancel: handleCancel,
onConfirm: handleConfirm
})
};
}
return null;
})();
if (liveEdit) {
return null;
}
if (!isPermissionsLoading && !(permissions !== null && permissions !== void 0 && permissions.granted)) {
return {
color: 'danger',
icon: _icons.UnpublishIcon,
label: 'Unpublish',
title: /*#__PURE__*/_react.default.createElement(_components.InsufficientPermissionsMessage, {
operationLabel: "unpublish this document",
currentUser: currentUser
}),
disabled: true
};
}
return {
color: 'danger',
icon: _icons.UnpublishIcon,
disabled: Boolean(unpublish.disabled) || isPermissionsLoading,
label: 'Unpublish',
title: unpublish.disabled ? DISABLED_REASON_TITLE[unpublish.disabled] : '',
onHandle: () => setConfirmDialogOpen(true),
dialog
};
};
exports.UnpublishAction = UnpublishAction;