-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-page-builder): make PageOptionsMenu composable
- Loading branch information
Showing
7 changed files
with
190 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...pp-page-builder/src/pageEditor/config/editorBar/PageOptionsMenu/PageOptionsMenuPlugin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from "react"; | ||
import { createComponentPlugin } from "@webiny/react-composition"; | ||
import { EditorBar } from "~/editor"; | ||
import { PageOptionsMenu } from "~/pageEditor"; | ||
|
||
export const PageOptionsMenuPlugin = createComponentPlugin(EditorBar.RightSection, RightSection => { | ||
return function AddRevisionSelector(props) { | ||
return ( | ||
<RightSection> | ||
<PageOptionsMenu items={[]} /> | ||
{props.children} | ||
</RightSection> | ||
); | ||
}; | ||
}); |
46 changes: 0 additions & 46 deletions
46
...es/app-page-builder/src/pageEditor/config/editorBar/PageOptionsMenu/PreviewPageButton.tsx
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
.../app-page-builder/src/pageEditor/config/editorBar/PreviewPageButton/PreviewPageButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from "react"; | ||
import { ReactComponent as PreviewIcon } from "~/admin/assets/visibility.svg"; | ||
import { usePage } from "~/pageEditor/hooks/usePage"; | ||
import { usePageBuilderSettings } from "~/admin/hooks/usePageBuilderSettings"; | ||
import { useSiteStatus } from "~/admin/hooks/useSiteStatus"; | ||
import { useConfigureWebsiteUrlDialog } from "~/admin/hooks/useConfigureWebsiteUrl"; | ||
import { createComponentPlugin } from "@webiny/react-composition"; | ||
import { PageOptionsMenu, PageOptionsMenuItem } from "~/pageEditor"; | ||
|
||
const openTarget = window.Cypress ? "_self" : "_blank"; | ||
|
||
export const PreviewPageButtonPlugin = createComponentPlugin(PageOptionsMenu, Original => { | ||
return function PreviewPageButton({ items, ...props }) { | ||
const [page] = usePage(); | ||
const { getPageUrl, getWebsiteUrl } = usePageBuilderSettings(); | ||
const [isSiteRunning, refreshSiteStatus] = useSiteStatus(getWebsiteUrl()); | ||
|
||
const { showConfigureWebsiteUrlDialog } = useConfigureWebsiteUrlDialog( | ||
getWebsiteUrl(), | ||
refreshSiteStatus | ||
); | ||
|
||
const pageData = { | ||
id: page.id, | ||
status: page.status, | ||
path: page.path | ||
}; | ||
|
||
const onClick = () => { | ||
if (isSiteRunning) { | ||
window.open(getPageUrl(pageData, true), openTarget, "noopener"); | ||
} else { | ||
showConfigureWebsiteUrlDialog(); | ||
} | ||
}; | ||
|
||
const previewItem: PageOptionsMenuItem = { | ||
label: "Preview", | ||
icon: <PreviewIcon />, | ||
onClick, | ||
"data-testid": "pb-editor-page-options-menu-preview" | ||
}; | ||
return <Original {...props} items={[previewItem, ...items]} />; | ||
}; | ||
}); |
79 changes: 79 additions & 0 deletions
79
...-page-builder/src/pageEditor/config/editorBar/SetAsHomepageButton/SetAsHomepageButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useCallback } from "react"; | ||
import { useSnackbar } from "@webiny/app-admin/hooks/useSnackbar"; | ||
import { useRouter } from "@webiny/react-router"; | ||
import { ReactComponent as HomeIcon } from "~/admin/assets/round-home-24px.svg"; | ||
import { usePageBuilderSettings } from "~/admin/hooks/usePageBuilderSettings"; | ||
import { useAdminPageBuilder } from "~/admin/hooks/useAdminPageBuilder"; | ||
import { usePage } from "~/pageEditor/hooks/usePage"; | ||
import { createComponentPlugin } from "@webiny/react-composition"; | ||
import { PageOptionsMenu, PageOptionsMenuItem } from "~/pageEditor"; | ||
import { useConfirmationDialog } from "@webiny/app-admin"; | ||
|
||
export const SetAsHomepageButtonPlugin = createComponentPlugin(PageOptionsMenu, Original => { | ||
return function SetAsHomepageButton({ items, ...props }) { | ||
const [page] = usePage(); | ||
const { history } = useRouter(); | ||
const { showSnackbar } = useSnackbar(); | ||
const pageBuilder = useAdminPageBuilder(); | ||
const { showConfirmation } = useConfirmationDialog({ | ||
message: ( | ||
<span> | ||
You're about to set this page as your new homepage, are you sure you want to | ||
continue? | ||
<br /> | ||
Note that the page will automatically be published. | ||
</span> | ||
) | ||
}); | ||
|
||
const { settings, updateSettingsMutation, isSpecialPage } = usePageBuilderSettings(); | ||
|
||
const setPageAsHomepage = useCallback(async () => { | ||
const publishPageResult = await pageBuilder.publishPage(page as { id: string }, { | ||
client: pageBuilder.client | ||
}); | ||
/** | ||
* In case of exit in "publishPage" lifecycle, "publishPage" hook will return undefined, | ||
* indicating an immediate exit. | ||
*/ | ||
if (!publishPageResult) { | ||
return; | ||
} | ||
|
||
if (publishPageResult.error) { | ||
return showSnackbar(publishPageResult.error.message); | ||
} | ||
|
||
const [updateSettings] = updateSettingsMutation; | ||
const response = await updateSettings({ | ||
variables: { | ||
data: { | ||
pages: { | ||
...settings.pages, | ||
home: page.id | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const { error } = response.data.pageBuilder.updateSettings; | ||
if (error) { | ||
return showSnackbar(error.message); | ||
} | ||
|
||
history.push(`/page-builder/pages?id=${page.id}`); | ||
|
||
// Let's wait a bit, because we are also redirecting the user. | ||
setTimeout(() => showSnackbar("New homepage set successfully!"), 500); | ||
}, [page.id]); | ||
|
||
const setAsHomepageItem: PageOptionsMenuItem = { | ||
label: "Set as homepage", | ||
icon: <HomeIcon />, | ||
onClick: () => showConfirmation(setPageAsHomepage), | ||
disabled: isSpecialPage(page.pid!, "home") | ||
}; | ||
|
||
return <Original {...props} items={[setAsHomepageItem, ...items]} />; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export { PublishPageButton } from "./config/editorBar/PublishPageButton"; | ||
export { PageOptionsMenu } from "./config/editorBar/PageOptionsMenu"; | ||
export type { PageOptionsMenuItem, PageOptionsMenuProps } from "./config/editorBar/PageOptionsMenu"; | ||
export { usePage } from "./hooks/usePage"; |