Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle publish locale with autosave enabled and close dropdown #8719

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/views/Version/Restore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const Restore: React.FC<Props> = ({
className={[canRestoreAsDraft && `${baseClass}__button`].filter(Boolean).join(' ')}
onClick={() => toggleModal(modalSlug)}
size="small"
SubMenuPopupContent={
SubMenuPopupContent={() =>
canRestoreAsDraft && (
<PopupList.ButtonGroup>
<PopupList.Button onClick={() => [setDraft(true), toggleModal(modalSlug)]}>
Expand Down
12 changes: 7 additions & 5 deletions packages/payload/src/collections/operations/updateByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ export const updateByIDOperation = async <TSlug extends CollectionSlug>(
}

if (publishSpecificLocale) {
publishedDocWithLocales = await getLatestCollectionVersion({
versionSnapshotResult = await beforeChange({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the change of order that we have moving the beforeChange up above the getLatestCollectionVersion.

it seems to me that any beforeChange hooks that return values will be overriden by version data and you wouldn't be able to use the latest version data in your beforeChange? Is that expected? I might not be thinking about this correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I think the confusion here is coming from the double beforeChange in this file. What you are saying makes complete sense, the beforeChange that runs on the actual doc happens after on line 316:

    let result = await beforeChange({...})

The versionSnapshotResult = await beforeChange on line 299 is to save a snapshot that is passed to saveVersion on line 367 and is used no where else in the file.

saveVersion({
        ...
        snapshot: versionSnapshotResult,
      })

I only moved the versionSnapshotResult = await beforeChange up because when I was re-thinking through the steps that happen here, it felt less confusing to me to take the snapshot first and get it out of the way - but no operational reason it needed to be moved.

...beforeChangeArgs,
docWithLocales,
})

const lastPublished = await getLatestCollectionVersion({
id,
config: collectionConfig,
payload,
Expand All @@ -305,10 +310,7 @@ export const updateByIDOperation = async <TSlug extends CollectionSlug>(
req,
})

versionSnapshotResult = await beforeChange({
...beforeChangeArgs,
docWithLocales,
})
publishedDocWithLocales = lastPublished ? lastPublished : {}
}

let result = await beforeChange({
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/elements/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,10 @@ export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, Props>((
className={disabled ? `${baseClass}--popup-disabled` : ''}
horizontalAlign="right"
noBackground
render={({ close }) => SubMenuPopupContent({ close: () => close() })}
size="large"
verticalAlign="bottom"
>
{SubMenuPopupContent}
</Popup>
/>
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/elements/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type Props = {
round?: boolean
secondaryActions?: secondaryAction | secondaryAction[]
size?: 'large' | 'medium' | 'small'
SubMenuPopupContent?: React.ReactNode
SubMenuPopupContent?: (any) => React.ReactNode
JessChowdhury marked this conversation as resolved.
Show resolved Hide resolved
to?: string
tooltip?: string
type?: 'button' | 'submit'
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/elements/PublishButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const DefaultPublishButton: React.FC<{ label?: string }> = ({ label: labe
disabled={!canPublish}
onClick={publish}
size="medium"
SubMenuPopupContent={
SubMenuPopupContent={({ close }) =>
localization
? localization.locales.map((locale) => {
const formattedLabel =
Expand All @@ -139,7 +139,7 @@ export const DefaultPublishButton: React.FC<{ label?: string }> = ({ label: labe
if (isActive) {
return (
<PopupList.ButtonGroup key={locale.code}>
<PopupList.Button onClick={() => publishSpecificLocale(locale.code)}>
<PopupList.Button onClick={() => [publishSpecificLocale(locale.code), close()]}>
{t('version:publishIn', { locale: formattedLabel || locale.code })}
</PopupList.Button>
</PopupList.ButtonGroup>
Expand Down
Loading