Skip to content

Commit 337409b

Browse files
authored
refactor(ui): strongly type render-list server action (#14611)
The render-list server action call was incorrectly types, resulting in unnecessary arguments being passed into it from the `ListDrawer` component. This PR fixes the incorrect types, moves the into the ui package so that they can be accessed within it, and strongly types the server function call.
1 parent 2394fe6 commit 337409b

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

packages/next/src/views/List/handleServerFunction.tsx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { CollectionPreferences, ListQuery, ServerFunction, VisibleEntities } from 'payload'
1+
import type { RenderListServerFnArgs, RenderListServerFnReturnType } from '@payloadcms/ui'
2+
import type { CollectionPreferences, ServerFunction, VisibleEntities } from 'payload'
23

34
import { getClientConfig } from '@payloadcms/ui/utilities/getClientConfig'
45
import { headers as getHeaders } from 'next/headers.js'
@@ -7,27 +8,9 @@ import { applyLocaleFiltering } from 'payload/shared'
78

89
import { renderListView } from './index.js'
910

10-
type RenderListResult = {
11-
List: React.ReactNode
12-
preferences: CollectionPreferences
13-
}
14-
1511
export const renderListHandler: ServerFunction<
16-
{
17-
collectionSlug: string
18-
disableActions?: boolean
19-
disableBulkDelete?: boolean
20-
disableBulkEdit?: boolean
21-
disableQueryPresets?: boolean
22-
documentDrawerSlug: string
23-
drawerSlug?: string
24-
enableRowSelections: boolean
25-
overrideEntityVisibility?: boolean
26-
query: ListQuery
27-
redirectAfterDelete: boolean
28-
redirectAfterDuplicate: boolean
29-
},
30-
Promise<RenderListResult>
12+
RenderListServerFnArgs,
13+
Promise<RenderListServerFnReturnType>
3114
> = async (args) => {
3215
const {
3316
collectionSlug,

packages/ui/src/elements/ListDrawer/DrawerContent.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { hoistQueryParamsToAnd } from 'payload/shared'
66
import React, { useCallback, useEffect, useState } from 'react'
77

88
import type { ListDrawerContextProps, ListDrawerContextType } from '../ListDrawer/Provider.js'
9-
import type { ListDrawerProps } from './types.js'
9+
import type {
10+
ListDrawerProps,
11+
RenderListServerFnArgs,
12+
RenderListServerFnReturnType,
13+
} from './types.js'
1014

1115
import { useDocumentDrawer } from '../../elements/DocumentDrawer/index.js'
1216
import { useEffectEvent } from '../../hooks/useEffectEvent.js'
@@ -92,10 +96,9 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
9296
}
9397

9498
if (slug) {
95-
const result: { List: React.ReactNode } = (await serverFunction({
99+
const result: RenderListServerFnReturnType = (await serverFunction({
96100
name: 'render-list',
97101
args: {
98-
allowCreate,
99102
collectionSlug: slug,
100103
disableBulkDelete: true,
101104
disableBulkEdit: true,
@@ -104,8 +107,8 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
104107
enableRowSelections,
105108
overrideEntityVisibility,
106109
query: newQuery,
107-
},
108-
})) as { List: React.ReactNode }
110+
} satisfies RenderListServerFnArgs,
111+
})) as RenderListServerFnReturnType
109112

110113
setListView(result?.List || null)
111114
} else {
@@ -123,7 +126,6 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
123126
[
124127
serverFunction,
125128
closeModal,
126-
allowCreate,
127129
drawerSlug,
128130
isOpen,
129131
enableRowSelections,

packages/ui/src/elements/ListDrawer/types.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
import type { FilterOptionsResult, SanitizedCollectionConfig } from 'payload'
1+
import type {
2+
CollectionPreferences,
3+
FilterOptionsResult,
4+
ListQuery,
5+
SanitizedCollectionConfig,
6+
} from 'payload'
27
import type React from 'react'
38
import type { HTMLAttributes } from 'react'
49

510
import type { ListDrawerContextProps } from './Provider.js'
611

12+
/**
13+
* @internal - this may change in a minor release
14+
*/
15+
export type RenderListServerFnArgs = {
16+
collectionSlug: string
17+
disableActions?: boolean
18+
disableBulkDelete?: boolean
19+
disableBulkEdit?: boolean
20+
disableQueryPresets?: boolean
21+
drawerSlug?: string
22+
enableRowSelections: boolean
23+
overrideEntityVisibility?: boolean
24+
query: ListQuery
25+
redirectAfterDelete?: boolean
26+
redirectAfterDuplicate?: boolean
27+
}
28+
29+
/**
30+
* @internal - this may change in a minor release
31+
*/
32+
export type RenderListServerFnReturnType = {
33+
List: React.ReactNode
34+
preferences: CollectionPreferences
35+
}
36+
737
export type ListDrawerProps = {
838
readonly allowCreate?: boolean
939
readonly collectionSlugs: SanitizedCollectionConfig['slug'][]

packages/ui/src/exports/client/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export { useListDrawer } from '../../elements/ListDrawer/index.js'
9999
export type {
100100
ListDrawerProps,
101101
ListTogglerProps,
102+
RenderListServerFnArgs,
103+
RenderListServerFnReturnType,
102104
UseListDrawer,
103105
} from '../../elements/ListDrawer/types.js'
104106
export { ListSelection } from '../../views/List/ListSelection/index.js'

0 commit comments

Comments
 (0)