-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Clickable generic list items (#15003)
* Add model fields * Validate contentful field * Add generic list item detail page * Remove json stringify call * Add org subpage layout * Handle language toggle for org subpages * Add project subpage layout to generic list item page * Skip article layouts for now * Add og:title * Remove comment * Add type imports * Stop considering only within lists as needing to have a unique slug * Render custom content for project subpages * Use template literals * Remove console.log --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
Showing
27 changed files
with
1,000 additions
and
257 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
166 changes: 166 additions & 0 deletions
166
apps/contentful-apps/pages/fields/generic-list-item-slug-field.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,166 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useDebounce } from 'react-use' | ||
import type { FieldExtensionSDK } from '@contentful/app-sdk' | ||
import { Stack, Text, TextInput } from '@contentful/f36-components' | ||
import { useCMA, useSDK } from '@contentful/react-apps-toolkit' | ||
import slugify from '@sindresorhus/slugify' | ||
|
||
import { CUSTOM_SLUGIFY_REPLACEMENTS } from '../../constants' | ||
import { slugifyDate } from '../../utils' | ||
|
||
const DEBOUNCE_TIME = 100 | ||
|
||
const GenericListItemSlugField = () => { | ||
const sdk = useSDK<FieldExtensionSDK>() | ||
const cma = useCMA() | ||
|
||
const [genericList, setGenericList] = useState(null) | ||
const [hasEntryBeenPublished, setHasEntryBeenPublished] = useState( | ||
Boolean(sdk.entry.getSys()?.firstPublishedAt), | ||
) | ||
|
||
const [value, setValue] = useState(sdk.field.getValue() ?? '') | ||
const [isValid, setIsValid] = useState(true) | ||
|
||
useEffect(() => { | ||
sdk.entry.onSysChanged((newSys) => { | ||
setHasEntryBeenPublished(Boolean(newSys?.firstPublishedAt)) | ||
}) | ||
}, [sdk.entry]) | ||
|
||
useEffect(() => { | ||
const unsubscribe = sdk.entry.fields.genericList.onValueChanged( | ||
(updatedList) => { | ||
if (updatedList?.sys?.id) { | ||
cma.entry | ||
.get({ | ||
entryId: updatedList.sys.id, | ||
}) | ||
.then(setGenericList) | ||
} else { | ||
setGenericList(null) | ||
} | ||
}, | ||
) | ||
|
||
return unsubscribe | ||
}, [cma.entry, sdk.entry.fields.genericList]) | ||
|
||
useEffect(() => { | ||
sdk.window.startAutoResizer() | ||
}, [sdk.window]) | ||
|
||
useEffect(() => { | ||
const unsubscribeFromTitleValueChanges = sdk.entry.fields.title | ||
.getForLocale(sdk.field.locale) | ||
.onValueChanged((newTitle) => { | ||
if (!newTitle || hasEntryBeenPublished) { | ||
return | ||
} | ||
const date = sdk.entry.fields.date.getValue() | ||
setValue( | ||
`${slugify(newTitle, { | ||
customReplacements: CUSTOM_SLUGIFY_REPLACEMENTS, | ||
})}${date ? '-' + slugifyDate(date) : ''}`, | ||
) | ||
}) | ||
|
||
const unsubscribeFromDateValueChanges = | ||
sdk.entry.fields.date.onValueChanged((newDate) => { | ||
const date = newDate | ||
const title = sdk.entry.fields.title | ||
.getForLocale(sdk.field.locale) | ||
.getValue() | ||
if (!title || hasEntryBeenPublished) { | ||
return | ||
} | ||
setValue( | ||
`${slugify(title, { | ||
customReplacements: CUSTOM_SLUGIFY_REPLACEMENTS, | ||
})}${date ? `-${slugifyDate(date)}` : ''}`, | ||
) | ||
}) | ||
|
||
return () => { | ||
unsubscribeFromTitleValueChanges() | ||
unsubscribeFromDateValueChanges() | ||
} | ||
}, [ | ||
hasEntryBeenPublished, | ||
sdk.entry.fields.date, | ||
sdk.entry.fields.title, | ||
sdk.field.locale, | ||
]) | ||
|
||
useDebounce( | ||
async () => { | ||
const genericListId = sdk.entry.fields.genericList.getValue()?.sys?.id | ||
if (!genericListId || !value) { | ||
return | ||
} | ||
const itemsInSameListWithSameSlug = | ||
( | ||
await cma.entry.getMany({ | ||
environmentId: sdk.ids.environment, | ||
spaceId: sdk.ids.space, | ||
query: { | ||
locale: sdk.field.locale, | ||
content_type: 'genericListItem', | ||
'fields.slug': value, | ||
'sys.id[ne]': sdk.entry.getSys().id, | ||
'sys.archivedVersion[exists]': false, | ||
}, | ||
}) | ||
)?.items ?? [] | ||
setIsValid(itemsInSameListWithSameSlug.length <= 0) | ||
}, | ||
DEBOUNCE_TIME, | ||
[value], | ||
) | ||
|
||
useDebounce( | ||
() => { | ||
if (isValid) { | ||
sdk.field.setValue(value) | ||
} else { | ||
sdk.field.setValue(null) // Set to null to prevent entry publish | ||
} | ||
sdk.field.setInvalid(!isValid) | ||
}, | ||
DEBOUNCE_TIME, | ||
[isValid, value], | ||
) | ||
|
||
if ( | ||
genericList && | ||
genericList?.fields?.itemType?.[sdk.locales.default] !== 'Clickable' | ||
) { | ||
return ( | ||
<Text> | ||
Slug can only be changed if the list item type is {`"Clickable"`} | ||
</Text> | ||
) | ||
} | ||
|
||
const isInvalid = value.length === 0 || !isValid | ||
|
||
return ( | ||
<Stack spacing="spacingXs" flexDirection="column" alignItems="flex-start"> | ||
<TextInput | ||
value={value} | ||
onChange={(ev) => { | ||
setValue(ev.target.value) | ||
}} | ||
isInvalid={isInvalid} | ||
/> | ||
{value.length === 0 && sdk.field.locale === sdk.locales.default && ( | ||
<Text fontColor="red400">Invalid slug</Text> | ||
)} | ||
{value.length > 0 && isInvalid && ( | ||
<Text fontColor="red400">Item already exists with this slug</Text> | ||
)} | ||
</Stack> | ||
) | ||
} | ||
|
||
export default GenericListItemSlugField |
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
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
Oops, something went wrong.