-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(next, ui): widget fields #15700
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
442d79d
add widget fields
GermanJablo 66e3d3c
fix ts error
GermanJablo dff9de4
fix ts error
GermanJablo 29562d7
fix ts error
GermanJablo 054a0da
add types
GermanJablo 3aa8221
save
GermanJablo 4b33b82
change WidgetServerProps parameter
GermanJablo 2527a72
hide buttons instead of disabling
GermanJablo c316baf
fix errors
GermanJablo 07bfe87
fix errors
GermanJablo b929c8c
add docs
GermanJablo 8368e6a
fix tests
GermanJablo 2ad23ad
fix css bug
GermanJablo 39b3d0d
fix nit from copilot
GermanJablo e87920c
copilot nit 2
GermanJablo 671c6ca
fix validation
GermanJablo 3db2852
move utils to separate file
GermanJablo 281a75c
add missing required
GermanJablo ba62eaf
refactor RenderWidget to reset component state before rendering
GermanJablo 129e040
add more tests
GermanJablo 88b881b
Fix locale data handling for nested fields
GermanJablo 7c4c9c6
Add integration tests for dashboard widget types to validate configur…
GermanJablo 4839e72
Update widget width property to be required and adjust related types …
GermanJablo 405d317
fix ts error
GermanJablo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
159 changes: 159 additions & 0 deletions
159
packages/next/src/views/Dashboard/Default/ModularDashboard/WidgetConfigDrawer.tsx
This file contains hidden or 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,159 @@ | ||
| 'use client' | ||
|
|
||
| import type { ClientWidget, FormState } from 'payload' | ||
|
|
||
| import { | ||
| Drawer, | ||
| Form, | ||
| FormSubmit, | ||
| OperationProvider, | ||
| RenderFields, | ||
| ShimmerEffect, | ||
| useLocale, | ||
| useModal, | ||
| useServerFunctions, | ||
| useTranslation, | ||
| } from '@payloadcms/ui' | ||
| import { abortAndIgnore } from '@payloadcms/ui/shared' | ||
| import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' | ||
| import { v4 as uuid } from 'uuid' | ||
|
|
||
| import { extractLocaleData, mergeLocaleData } from './utils/localeUtils.js' | ||
|
|
||
| type WidgetConfigDrawerProps = { | ||
| drawerSlug: string | ||
| onSave: (data: Record<string, unknown>) => void | ||
| widget: ClientWidget | ||
| widgetData?: Record<string, unknown> | ||
| } | ||
|
|
||
| const EMPTY_WIDGET_PREFERENCES = { | ||
| fields: {}, | ||
| } | ||
|
|
||
| export function WidgetConfigDrawer({ | ||
| drawerSlug, | ||
| onSave, | ||
| widget, | ||
| widgetData, | ||
| }: WidgetConfigDrawerProps) { | ||
| const { closeModal, modalState } = useModal() | ||
| const { getFormState } = useServerFunctions() | ||
| const { t } = useTranslation() | ||
| const locale = useLocale() | ||
| const localeCode = locale?.code ?? 'en' | ||
| const onChangeAbortControllerRef = useRef<AbortController>(null) | ||
|
|
||
| const [initialState, setInitialState] = useState<false | FormState | undefined>(false) | ||
|
|
||
| const isOpen = Boolean(modalState?.[drawerSlug]?.isOpen) | ||
| const formUUID = useMemo(() => uuid(), []) | ||
| const widgetLabel = useMemo( | ||
| () => (typeof widget.label === 'string' ? widget.label : widget.slug), | ||
| [widget.label, widget.slug], | ||
| ) | ||
| const fields = useMemo(() => widget.fields ?? [], [widget.fields]) | ||
|
|
||
| useEffect(() => { | ||
| if (!isOpen || fields.length === 0) { | ||
| setInitialState(false) | ||
| return | ||
| } | ||
|
|
||
| const controller = new AbortController() | ||
|
|
||
| const loadInitialState = async () => { | ||
| const localeFilteredData = extractLocaleData(widgetData ?? {}, localeCode, fields) | ||
|
|
||
| const { state } = await getFormState({ | ||
| data: localeFilteredData, | ||
| docPermissions: { | ||
| fields: true, | ||
| }, | ||
| docPreferences: EMPTY_WIDGET_PREFERENCES, | ||
| locale: localeCode, | ||
| operation: 'update', | ||
| renderAllFields: true, | ||
| schemaPath: widget.slug, | ||
| signal: controller.signal, | ||
| widgetSlug: widget.slug, | ||
| }) | ||
|
|
||
| if (state) { | ||
| setInitialState(state) | ||
| } | ||
| } | ||
|
|
||
| void loadInitialState() | ||
|
|
||
| return () => { | ||
| abortAndIgnore(controller) | ||
| } | ||
| }, [fields, getFormState, isOpen, localeCode, widget.slug, widgetData]) | ||
|
|
||
| const onChange = useCallback( | ||
| async ({ formState: prevFormState }: { formState: FormState }) => { | ||
| abortAndIgnore(onChangeAbortControllerRef.current) | ||
|
|
||
| const controller = new AbortController() | ||
| onChangeAbortControllerRef.current = controller | ||
|
|
||
| const { state } = await getFormState({ | ||
| docPermissions: { | ||
| fields: true, | ||
| }, | ||
| docPreferences: EMPTY_WIDGET_PREFERENCES, | ||
| formState: prevFormState, | ||
| operation: 'update', | ||
| schemaPath: widget.slug, | ||
| signal: controller.signal, | ||
| widgetSlug: widget.slug, | ||
| }) | ||
|
|
||
| if (!state) { | ||
| return prevFormState | ||
| } | ||
|
|
||
| return state | ||
| }, | ||
| [getFormState, widget.slug], | ||
| ) | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| abortAndIgnore(onChangeAbortControllerRef.current) | ||
| } | ||
| }, []) | ||
|
|
||
| return ( | ||
| <Drawer slug={drawerSlug} title={`${t('general:edit')} ${widgetLabel}`}> | ||
| {initialState === false ? ( | ||
| <ShimmerEffect height="250px" /> | ||
| ) : ( | ||
| <OperationProvider operation="update"> | ||
| <Form | ||
| fields={fields} | ||
| initialState={initialState} | ||
| onChange={[onChange]} | ||
| onSubmit={(_, data) => { | ||
| onSave(mergeLocaleData(widgetData ?? {}, data, localeCode, fields)) | ||
| closeModal(drawerSlug) | ||
| }} | ||
| uuid={formUUID} | ||
| > | ||
| <RenderFields | ||
| fields={fields} | ||
| forceRender | ||
| parentIndexPath="" | ||
| parentPath="" | ||
| parentSchemaPath={widget.slug} | ||
| permissions={true} | ||
| readOnly={false} | ||
| /> | ||
| <FormSubmit>{t('fields:saveChanges')}</FormSubmit> | ||
| </Form> | ||
| </OperationProvider> | ||
| )} | ||
| </Drawer> | ||
| ) | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
packages/next/src/views/Dashboard/Default/ModularDashboard/WidgetEditControl.tsx
This file contains hidden or 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,59 @@ | ||
| 'use client' | ||
|
|
||
| import type { Data } from 'payload' | ||
|
|
||
| import { EditIcon, useConfig, useModal, useTranslation } from '@payloadcms/ui' | ||
| import React, { useId } from 'react' | ||
|
|
||
| import { WidgetConfigDrawer } from './WidgetConfigDrawer.js' | ||
|
|
||
| const getWidgetSlugFromID = (widgetID: string): string => | ||
| widgetID.slice(0, widgetID.lastIndexOf('-')) | ||
|
|
||
| export function WidgetEditControl({ | ||
| onSave, | ||
| widgetData, | ||
| widgetID, | ||
| }: { | ||
| onSave: (data: Data) => void | ||
| widgetData?: Record<string, unknown> | ||
| widgetID: string | ||
| }) { | ||
| const { t } = useTranslation() | ||
| const { openModal } = useModal() | ||
| const { widgets: configWidgets = [] } = useConfig().config.admin.dashboard ?? {} | ||
|
|
||
| const widgetSlug = getWidgetSlugFromID(widgetID) | ||
| const widgetConfig = configWidgets.find((widget) => widget.slug === widgetSlug) | ||
| const hasEditableFields = Boolean(widgetConfig?.fields?.length) | ||
|
|
||
| const drawerID = useId() | ||
| const drawerSlug = `widget-editor-${drawerID}` | ||
|
|
||
| if (!hasEditableFields) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <button | ||
| className="widget-wrapper__edit-btn" | ||
| onClick={() => { | ||
| openModal(drawerSlug) | ||
| }} | ||
| type="button" | ||
| > | ||
| <span className="sr-only"> | ||
| {t('general:edit')} {widgetID} | ||
| </span> | ||
| <EditIcon /> | ||
| </button> | ||
| <WidgetConfigDrawer | ||
| drawerSlug={drawerSlug} | ||
| onSave={onSave} | ||
| widget={widgetConfig} | ||
| widgetData={widgetData} | ||
| /> | ||
| </> | ||
| ) | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.