From 2cd9594f55cb057bdd0dd07c227c4163a89c94d3 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Fri, 4 Oct 2024 07:47:49 -0400 Subject: [PATCH] brings back actions for everything except lexical --- .../next/src/views/Edit/Default/index.tsx | 39 +++++------- .../utilities/fieldsDrawer/DrawerContent.tsx | 4 +- .../elements/BulkUpload/EditForm/index.tsx | 26 ++++---- .../BulkUpload/FormsManager/index.tsx | 20 +++---- packages/ui/src/elements/EditMany/index.tsx | 31 ++++------ packages/ui/src/forms/Form/index.tsx | 45 +++++--------- .../ui/src/providers/DocumentInfo/index.tsx | 60 +++++++++---------- 7 files changed, 89 insertions(+), 136 deletions(-) diff --git a/packages/next/src/views/Edit/Default/index.tsx b/packages/next/src/views/Edit/Default/index.tsx index f5a6d31aa9..2a1f13102a 100644 --- a/packages/next/src/views/Edit/Default/index.tsx +++ b/packages/next/src/views/Edit/Default/index.tsx @@ -15,10 +15,9 @@ import { useDocumentEvents, useDocumentInfo, useEditDepth, - // useServerFunctions, + useServerFunctions, useUploadEdits, } from '@payloadcms/ui' -import { getFormStateFetch } from '@payloadcms/ui' import { formatAdminURL, handleBackToDashboard, @@ -80,14 +79,13 @@ export const DefaultEditView: React.FC = () => { const { refreshCookieAsync, user } = useAuth() - // const { getFormState } = useServerFunctions() + const { getFormState } = useServerFunctions() const { config, config: { admin: { user: userSlug }, - routes: { admin: adminRoute, api: apiRoute }, - serverURL, + routes: { admin: adminRoute }, }, getEntityConfig, } = useConfig() @@ -245,20 +243,16 @@ export const DefaultEditView: React.FC = () => { const docPreferences = await getDocPreferences() - const { lockedState, state } = await getFormStateFetch({ - apiRoute, - body: { - id, - collectionSlug, - docPreferences, - formState: prevFormState, - globalSlug, - operation, - returnLockStatus: isLockingEnabled ? true : false, - schemaPath, - updateLastEdited, - }, - serverURL, + const { lockedState, state } = await getFormState({ + id, + collectionSlug, + docPreferences, + formState: prevFormState, + globalSlug, + operation, + returnLockStatus: isLockingEnabled ? true : false, + schemaPath, + updateLastEdited, }) setDocumentIsLocked(true) @@ -281,9 +275,9 @@ export const DefaultEditView: React.FC = () => { documentLockStateRef.current = documentLockStateRef.current = { hasShownLockedModal: documentLockStateRef.current?.hasShownLockedModal || false, isLocked: true, - user: lockedState.user, + user: lockedState.user as ClientUser, } - setCurrentEditor(lockedState.user) + setCurrentEditor(lockedState.user as ClientUser) } } } @@ -302,8 +296,7 @@ export const DefaultEditView: React.FC = () => { isLockingEnabled, setDocumentIsLocked, lastUpdateTime, - apiRoute, - serverURL, + getFormState, ], ) diff --git a/packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx b/packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx index 21a032ae11..770d0e877a 100644 --- a/packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx +++ b/packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx @@ -9,7 +9,7 @@ import { useConfig, useDocumentInfo, useFieldProps, - useServerFunctions, + // useServerFunctions, useTranslation, } from '@payloadcms/ui' import React, { useCallback, useEffect, useState } from 'react' @@ -44,7 +44,7 @@ export const DrawerContent: React.FC[0] = useCallback( async ({ formState: prevFormState }) => { const docPreferences = await getDocPreferences() - const { state: newFormState } = await getFormStateFetch({ - apiRoute: api, - body: { - collectionSlug, - docPreferences, - formState: prevFormState, - operation: 'create', - schemaPath, - }, - serverURL, + const { state: newFormState } = await getFormState({ + collectionSlug, + docPreferences, + formState: prevFormState, + operation: 'create', + schemaPath, }) return newFormState }, - [collectionSlug, schemaPath, getDocPreferences, api, serverURL], + [collectionSlug, schemaPath, getDocPreferences, getFormState], ) return ( diff --git a/packages/ui/src/elements/BulkUpload/FormsManager/index.tsx b/packages/ui/src/elements/BulkUpload/FormsManager/index.tsx index 9209541171..d14ab9376b 100644 --- a/packages/ui/src/elements/BulkUpload/FormsManager/index.tsx +++ b/packages/ui/src/elements/BulkUpload/FormsManager/index.tsx @@ -12,8 +12,8 @@ import type { State } from './reducer.js' import { fieldReducer } from '../../../forms/Form/fieldReducer.js' import { useConfig } from '../../../providers/Config/index.js' import { useLocale } from '../../../providers/Locale/index.js' +import { useServerFunctions } from '../../../providers/ServerFunctions/index.js' import { useTranslation } from '../../../providers/Translation/index.js' -import { getFormStateFetch } from '../../../utilities/getFormStateFetch.js' import { hasSavePermission as getHasSavePermission } from '../../../utilities/hasSavePermission.js' import { useLoadingOverlay } from '../../LoadingOverlay/index.js' import { useBulkUpload } from '../index.js' @@ -81,7 +81,7 @@ export function FormsManagerProvider({ children }: FormsManagerProps) { const { code } = useLocale() const { i18n, t } = useTranslation() - // const { getFormState } = useServerFunctions() + const { getFormState } = useServerFunctions() const [hasSubmitted, setHasSubmitted] = React.useState(false) const [docPermissions, setDocPermissions] = React.useState() @@ -155,15 +155,11 @@ export function FormsManagerProvider({ children }: FormsManagerProps) { } try { - const { state: formStateWithoutFiles } = await getFormStateFetch({ - apiRoute: api, - body: { - collectionSlug, - locale: code, - operation: 'create', - schemaPath: collectionSlug, - }, - serverURL, + const { state: formStateWithoutFiles } = await getFormState({ + collectionSlug, + locale: code, + operation: 'create', + schemaPath: collectionSlug, }) initialStateRef.current = formStateWithoutFiles setHasInitializedState(true) @@ -171,7 +167,7 @@ export function FormsManagerProvider({ children }: FormsManagerProps) { // swallow error } }, - [code, collectionSlug, api, serverURL], + [code, collectionSlug, getFormState], ) const setActiveIndex: FormsManagerContext['setActiveIndex'] = React.useCallback( diff --git a/packages/ui/src/elements/EditMany/index.tsx b/packages/ui/src/elements/EditMany/index.tsx index 3053147a4d..b5e258010a 100644 --- a/packages/ui/src/elements/EditMany/index.tsx +++ b/packages/ui/src/elements/EditMany/index.tsx @@ -22,7 +22,6 @@ import { useSearchParams } from '../../providers/SearchParams/index.js' import { SelectAllStatus, useSelection } from '../../providers/Selection/index.js' import { useServerFunctions } from '../../providers/ServerFunctions/index.js' import { useTranslation } from '../../providers/Translation/index.js' -import { getFormStateFetch } from '../../utilities/getFormStateFetch.js' import { Drawer, DrawerToggler } from '../Drawer/index.js' import { FieldSelect } from '../FieldSelect/index.js' import './index.scss' @@ -132,15 +131,11 @@ export const EditMany: React.FC = (props) => { React.useEffect(() => { if (!hasInitializedState.current) { const getInitialState = async () => { - const { state: result } = await getFormStateFetch({ - apiRoute, - body: { - collectionSlug: slug, - data: {}, - operation: 'update', - schemaPath: slug, - }, - serverURL, + const { state: result } = await getFormState({ + collectionSlug: slug, + data: {}, + operation: 'update', + schemaPath: slug, }) setInitialState(result) @@ -153,20 +148,16 @@ export const EditMany: React.FC = (props) => { const onChange: FormProps['onChange'][0] = useCallback( async ({ formState: prevFormState }) => { - const { state } = await getFormStateFetch({ - apiRoute, - body: { - collectionSlug: slug, - formState: prevFormState, - operation: 'update', - schemaPath: slug, - }, - serverURL, + const { state } = await getFormState({ + collectionSlug: slug, + formState: prevFormState, + operation: 'update', + schemaPath: slug, }) return state }, - [slug, serverURL, apiRoute], + [slug, getFormState], ) if (selectAll === SelectAllStatus.None || !hasUpdatePermission) { diff --git a/packages/ui/src/forms/Form/index.tsx b/packages/ui/src/forms/Form/index.tsx index 5a301188a0..ef3d7e9210 100644 --- a/packages/ui/src/forms/Form/index.tsx +++ b/packages/ui/src/forms/Form/index.tsx @@ -29,7 +29,6 @@ import { useOperation } from '../../providers/Operation/index.js' import { useServerFunctions } from '../../providers/ServerFunctions/index.js' import { useTranslation } from '../../providers/Translation/index.js' import { requests } from '../../utilities/api.js' -import { getFormStateFetch } from '../../utilities/getFormStateFetch.js' import { FormContext, FormFieldsContext, @@ -82,13 +81,7 @@ export const Form: React.FC = (props) => { const { refreshCookie, user } = useAuth() const operation = useOperation() - const { - config, - config: { - routes: { api: apiURL }, - serverURL, - }, - } = useConfig() + const { config } = useConfig() const [disabled, setDisabled] = useState(disabledFromProps || false) const [isMounted, setIsMounted] = useState(false) @@ -455,24 +448,20 @@ export const Form: React.FC = (props) => { const reset = useCallback( async (data: unknown) => { - const { state: newState } = await getFormStateFetch({ - apiRoute: apiURL, - body: { - id, - collectionSlug, - data, - globalSlug, - operation, - schemaPath: collectionSlug || globalSlug, - }, - serverURL, + const { state: newState } = await getFormState({ + id, + collectionSlug, + data, + globalSlug, + operation, + schemaPath: collectionSlug || globalSlug, }) contextRef.current = { ...initContextState } as FormContextType setModified(false) dispatchFields({ type: 'REPLACE_STATE', state: newState }) }, - [collectionSlug, dispatchFields, globalSlug, id, operation, serverURL, apiURL], + [collectionSlug, dispatchFields, globalSlug, id, operation, getFormState], ) const replaceState = useCallback( @@ -486,20 +475,16 @@ export const Form: React.FC = (props) => { const getFieldStateBySchemaPath = useCallback( async ({ data, schemaPath }) => { - const { state: fieldSchema } = await getFormStateFetch({ - apiRoute: apiURL, - body: { - collectionSlug, - data, - globalSlug, - schemaPath, - }, - serverURL, + const { state: fieldSchema } = await getFormState({ + collectionSlug, + data, + globalSlug, + schemaPath, }) return fieldSchema }, - [collectionSlug, globalSlug, serverURL, apiURL], + [collectionSlug, globalSlug, getFormState], ) const addFieldRow: FormContextType['addFieldRow'] = useCallback( diff --git a/packages/ui/src/providers/DocumentInfo/index.tsx b/packages/ui/src/providers/DocumentInfo/index.tsx index d531dadc63..2feb47c296 100644 --- a/packages/ui/src/providers/DocumentInfo/index.tsx +++ b/packages/ui/src/providers/DocumentInfo/index.tsx @@ -21,14 +21,13 @@ import type { DocumentInfoContext, DocumentInfoProps } from './types.js' import { requests } from '../../utilities/api.js' import { formatDocTitle } from '../../utilities/formatDocTitle.js' -import { getFormStateFetch } from '../../utilities/getFormStateFetch.js' import { hasSavePermission as getHasSavePermission } from '../../utilities/hasSavePermission.js' import { isEditing as getIsEditing } from '../../utilities/isEditing.js' import { useAuth } from '../Auth/index.js' import { useConfig } from '../Config/index.js' import { useLocale } from '../Locale/index.js' import { usePreferences } from '../Preferences/index.js' -// import { useServerFunctions } from '../ServerFunctions/index.js' +import { useServerFunctions } from '../ServerFunctions/index.js' import { useTranslation } from '../Translation/index.js' import { UploadEditsProvider, useUploadEdits } from '../UploadEdits/index.js' @@ -56,7 +55,7 @@ const DocumentInfo: React.FC< onSave: onSaveFromProps, } = props - // const { getFormState } = useServerFunctions() + const { getFormState } = useServerFunctions() const { config: { @@ -496,19 +495,15 @@ const DocumentInfo: React.FC< const newData = collectionSlug ? json.doc : json.result - const { state: newState } = await getFormStateFetch({ - apiRoute: api, - body: { - id, - collectionSlug, - data: newData, - docPreferences, - globalSlug, - locale, - operation, - schemaPath: collectionSlug || globalSlug, - }, - serverURL, + const { state: newState } = await getFormState({ + id, + collectionSlug, + data: newData, + docPreferences, + globalSlug, + locale, + operation, + schemaPath: collectionSlug || globalSlug, }) setInitialState(newState) @@ -525,8 +520,7 @@ const DocumentInfo: React.FC< locale, onSaveFromProps, getDocPermissions, - serverURL, - api, + getFormState, ], ) @@ -548,29 +542,28 @@ const DocumentInfo: React.FC< setIsLoading(true) try { - const { state: result } = await getFormStateFetch({ - apiRoute: api, - body: { - id, - collectionSlug, - globalSlug, - locale, - operation, - schemaPath: collectionSlug || globalSlug, - }, - onError: onLoadError, - serverURL, - signal: abortController.signal, + const result = await getFormState({ + id, + collectionSlug, + globalSlug, + locale, + operation, + schemaPath: collectionSlug || globalSlug, }) - const data = reduceFieldsToValues(result, true) + if ('errors' in result) { + await onLoadError(result.errors) + return + } + + const data = reduceFieldsToValues(result.state, true) setData(data) if (localeChanged) { void getDocPermissions(data) } - setInitialState(result) + setInitialState(result.state) } catch (_err) { if (!abortController.signal.aborted) { if (typeof onLoadError === 'function') { @@ -606,6 +599,7 @@ const DocumentInfo: React.FC< initialStateFromProps, getDocPermissions, user, + getFormState, ]) useEffect(() => {