From 157c9066da3a633ec0f8e8c4c2a328d96a7009c5 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:21:37 +0800 Subject: [PATCH] fix(permalinks): default permalink for create (#1843) ## Problem We have quite a number of duplicate permalinks this does not fix the problem completely, but it does allow for some sort of preventive measure to prevents accidental dups. we are not sure if of the number of agencies that use dup permalinks for the sake of playground, and it would be hard to enforce this until we have the functionality for playground out. more context [here](https://opengovproducts.slack.com/archives/CK68JNFHR/p1710317487152189) ## Solution sane default when creating a page. **Breaking Changes** - [ ] Yes - this PR contains breaking changes - Details ... - [X] No - this PR is backwards compatible with ALL of the following feature flags in this [doc](https://www.notion.so/opengov/Existing-feature-flags-518ad2cdc325420893a105e88c432be5) ## Tests follow video https://github.com/isomerpages/isomercms-frontend/assets/42832651/3c3657dc-b9a1-4a40-9e49-07d71c32fd0a - [ ] create a page, note that you should be unable to modify the permalink - [ ] edit page modal's functionality remains the same, we dont still allow them to change to whatever. --- .../PageSettingsModal/PageSettingsModal.tsx | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/components/PageSettingsModal/PageSettingsModal.tsx b/src/components/PageSettingsModal/PageSettingsModal.tsx index 14fe98706..bfef42e12 100644 --- a/src/components/PageSettingsModal/PageSettingsModal.tsx +++ b/src/components/PageSettingsModal/PageSettingsModal.tsx @@ -96,6 +96,7 @@ export const PageSettingsModal = ({ }: PageSettingsModalParams) => { const { siteName, fileName } = params const isTiptapEnabled = useFeatureIsOn("is-tiptap-enabled") + const isPageCreated = !fileName const existingTitlesArray = pagesData .filter((page) => page.name !== fileName) @@ -146,6 +147,23 @@ export const PageSettingsModal = ({ }) } + const getDefaultPermalink = (title: string) => { + if (!title) return "" + const titleSlug = title + .toLowerCase() + .replace(/[^a-z0-9]+/g, "-") + .replace(/^-+|-+$/g, "") + return `/${titleSlug}/` + } + + const currTitle = watch("title") + const currPermalink = watch("permalink") + useEffect(() => { + if (isPageCreated && currPermalink !== getDefaultPermalink(currTitle)) { + setValue("permalink", getDefaultPermalink(currTitle)) + } + }, [isPageCreated, setValue, currTitle, currPermalink]) + return ( @@ -169,8 +187,17 @@ export const PageSettingsModal = ({ isLink={false} /> {/* Title */} - - Page title + + + Page title + + This appears on the top of the browser window or tab + + {errors.title?.message} -
{/* Permalink */} - + Page URL - {`${siteUrl}${watch("permalink")}`} + {isPageCreated + ? "You can change this later in Page Settings." + : `${siteUrl}${watch("permalink")}`} + {isPageCreated && ( + + + {`${siteUrl}${watch("permalink")}`} + + + )} + {errors.permalink?.message} -
{isTiptapEnabled && (