{"version":3,"file":"index.js","names":["useModal","formatAdminURL","React","useCallback","toast","useForm","useConfig","useDocumentInfo","useLocale","useTranslation","requests","Button","ConfirmationModal","baseClass","Status","id","collectionSlug","docPermissions","globalSlug","hasPublishedDoc","incrementVersionCount","isTrashed","setMostRecentVersionIsAutosaved","setUnpublishedVersionCount","unpublishedVersionCount","toggleModal","config","routes","api","reset","resetForm","code","locale","i18n","t","revertModalSlug","statusToRender","displayStatusKey","performAction","url","method","apiRoute","path","publishedDoc","get","headers","language","then","res","json","body","JSON","stringify","status","data","result","doc","success","message","errors","error","err","canUpdate","update","_jsx","className","title","_jsxs","Fragment","buttonStyle","onClick","confirmingLabel","heading","modalSlug","onConfirm"],"sources":["../../../src/elements/Status/index.tsx"],"sourcesContent":["'use client'\nimport { useModal } from '@faceless-ui/modal'\nimport { formatAdminURL } from 'payload/shared'\nimport React, { useCallback } from 'react'\nimport { toast } from 'sonner'\n\nimport { useForm } from '../../forms/Form/context.js'\nimport { useConfig } from '../../providers/Config/index.js'\nimport { useDocumentInfo } from '../../providers/DocumentInfo/index.js'\nimport { useLocale } from '../../providers/Locale/index.js'\nimport { useTranslation } from '../../providers/Translation/index.js'\nimport { requests } from '../../utilities/api.js'\nimport { Button } from '../Button/index.js'\nimport { ConfirmationModal } from '../ConfirmationModal/index.js'\nimport './index.scss'\n\nconst baseClass = 'status'\n\nexport const Status: React.FC = () => {\n const {\n id,\n collectionSlug,\n docPermissions,\n globalSlug,\n hasPublishedDoc,\n incrementVersionCount,\n isTrashed,\n setMostRecentVersionIsAutosaved,\n setUnpublishedVersionCount,\n unpublishedVersionCount,\n } = useDocumentInfo()\n\n const { toggleModal } = useModal()\n\n const {\n config: {\n routes: { api },\n },\n } = useConfig()\n\n const { reset: resetForm } = useForm()\n const { code: locale } = useLocale()\n const { i18n, t } = useTranslation()\n\n const revertModalSlug = `confirm-revert-${id}`\n\n let statusToRender: 'changed' | 'draft' | 'published'\n\n if (unpublishedVersionCount > 0 && hasPublishedDoc) {\n statusToRender = 'changed'\n } else if (!hasPublishedDoc) {\n statusToRender = 'draft'\n } else if (hasPublishedDoc && unpublishedVersionCount <= 0) {\n statusToRender = 'published'\n }\n\n const displayStatusKey = isTrashed\n ? hasPublishedDoc\n ? 'previouslyPublished'\n : 'previouslyDraft'\n : statusToRender\n\n const performAction = useCallback(async () => {\n let url\n let method\n\n if (collectionSlug) {\n url = formatAdminURL({\n apiRoute: api,\n path: `/${collectionSlug}/${id}?locale=${locale}&fallback-locale=null&depth=0`,\n })\n method = 'patch'\n }\n\n if (globalSlug) {\n url = formatAdminURL({\n apiRoute: api,\n path: `/globals/${globalSlug}?locale=${locale}&fallback-locale=null&depth=0`,\n })\n method = 'post'\n }\n\n const publishedDoc = await requests\n .get(url, {\n headers: {\n 'Accept-Language': i18n.language,\n 'Content-Type': 'application/json',\n },\n })\n .then((res) => res.json())\n\n const body = publishedDoc\n\n const res = await requests[method](url, {\n body: JSON.stringify(body),\n headers: {\n 'Accept-Language': i18n.language,\n 'Content-Type': 'application/json',\n },\n })\n\n if (res.status === 200) {\n let data\n const json = await res.json()\n\n if (globalSlug) {\n data = json.result\n } else if (collectionSlug) {\n data = json.doc\n }\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n resetForm(data)\n toast.success(json.message)\n incrementVersionCount()\n setMostRecentVersionIsAutosaved(false)\n\n setUnpublishedVersionCount(0)\n } else {\n try {\n const json = await res.json()\n if (json.errors?.[0]?.message) {\n toast.error(json.errors[0].message)\n } else if (json.error) {\n toast.error(json.error)\n } else {\n toast.error(t('error:revertingDocument'))\n }\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (err) {\n toast.error(t('error:revertingDocument'))\n }\n }\n }, [\n api,\n collectionSlug,\n globalSlug,\n id,\n i18n.language,\n incrementVersionCount,\n locale,\n resetForm,\n setUnpublishedVersionCount,\n setMostRecentVersionIsAutosaved,\n t,\n ])\n\n const canUpdate = docPermissions?.update\n\n if (statusToRender) {\n return (\n