1
1
'use client'
2
- import type { ClientCollectionConfig , FormState } from 'payload'
2
+ import type { ClientCollectionConfig , FieldWithPathClient , FormState } from 'payload'
3
3
4
4
import { useModal } from '@faceless-ui/modal'
5
5
import { getTranslation } from '@payloadcms/translations'
@@ -36,20 +36,36 @@ export type EditManyProps = {
36
36
readonly collection : ClientCollectionConfig
37
37
}
38
38
39
+ const sanitizeUnselectedFields = ( formState : FormState , selected : FieldWithPathClient [ ] ) => {
40
+ const filteredData = selected . reduce ( ( acc , field ) => {
41
+ const foundState = formState ?. [ field . path ]
42
+
43
+ if ( foundState ) {
44
+ acc [ field . path ] = formState ?. [ field . path ] ?. value
45
+ }
46
+
47
+ return acc
48
+ } , { } as FormData )
49
+
50
+ return filteredData
51
+ }
52
+
39
53
const Submit : React . FC < {
40
54
readonly action : string
41
55
readonly disabled : boolean
42
- } > = ( { action, disabled } ) => {
56
+ readonly selected ?: FieldWithPathClient [ ]
57
+ } > = ( { action, disabled, selected } ) => {
43
58
const { submit } = useForm ( )
44
59
const { t } = useTranslation ( )
45
60
46
61
const save = useCallback ( ( ) => {
47
62
void submit ( {
48
63
action,
49
64
method : 'PATCH' ,
65
+ overrides : ( formState ) => sanitizeUnselectedFields ( formState , selected ) ,
50
66
skipValidation : true ,
51
67
} )
52
- } , [ action , submit ] )
68
+ } , [ action , submit , selected ] )
53
69
54
70
return (
55
71
< FormSubmit className = { `${ baseClass } __save` } disabled = { disabled } onClick = { save } >
@@ -58,20 +74,25 @@ const Submit: React.FC<{
58
74
)
59
75
}
60
76
61
- const PublishButton : React . FC < { action : string ; disabled : boolean } > = ( { action, disabled } ) => {
77
+ const PublishButton : React . FC < {
78
+ action : string
79
+ disabled : boolean
80
+ selected ?: FieldWithPathClient [ ]
81
+ } > = ( { action, disabled, selected } ) => {
62
82
const { submit } = useForm ( )
63
83
const { t } = useTranslation ( )
64
84
65
85
const save = useCallback ( ( ) => {
66
86
void submit ( {
67
87
action,
68
88
method : 'PATCH' ,
69
- overrides : {
89
+ overrides : ( formState ) => ( {
90
+ ...sanitizeUnselectedFields ( formState , selected ) ,
70
91
_status : 'published' ,
71
- } ,
92
+ } ) ,
72
93
skipValidation : true ,
73
94
} )
74
- } , [ action , submit ] )
95
+ } , [ action , submit , selected ] )
75
96
76
97
return (
77
98
< FormSubmit className = { `${ baseClass } __publish` } disabled = { disabled } onClick = { save } >
@@ -80,20 +101,25 @@ const PublishButton: React.FC<{ action: string; disabled: boolean }> = ({ action
80
101
)
81
102
}
82
103
83
- const SaveDraftButton : React . FC < { action : string ; disabled : boolean } > = ( { action, disabled } ) => {
104
+ const SaveDraftButton : React . FC < {
105
+ action : string
106
+ disabled : boolean
107
+ selected ?: FieldWithPathClient [ ]
108
+ } > = ( { action, disabled, selected } ) => {
84
109
const { submit } = useForm ( )
85
110
const { t } = useTranslation ( )
86
111
87
112
const save = useCallback ( ( ) => {
88
113
void submit ( {
89
114
action,
90
115
method : 'PATCH' ,
91
- overrides : {
116
+ overrides : ( formState ) => ( {
117
+ ...sanitizeUnselectedFields ( formState , selected ) ,
92
118
_status : 'draft' ,
93
- } ,
119
+ } ) ,
94
120
skipValidation : true ,
95
121
} )
96
- } , [ action , submit ] )
122
+ } , [ action , submit , selected ] )
97
123
98
124
return (
99
125
< FormSubmit
@@ -125,7 +151,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
125
151
126
152
const { count, getQueryParams, selectAll } = useSelection ( )
127
153
const { i18n, t } = useTranslation ( )
128
- const [ selected , setSelected ] = useState ( [ ] )
154
+ const [ selected , setSelected ] = useState < FieldWithPathClient [ ] > ( [ ] )
129
155
const searchParams = useSearchParams ( )
130
156
const router = useRouter ( )
131
157
const [ initialState , setInitialState ] = useState < FormState > ( )
@@ -184,7 +210,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
184
210
185
211
return state
186
212
} ,
187
- [ slug , getFormState , collectionPermissions ] ,
213
+ [ getFormState , slug , collectionPermissions ] ,
188
214
)
189
215
190
216
useEffect ( ( ) => {
@@ -289,16 +315,19 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
289
315
< SaveDraftButton
290
316
action = { `${ serverURL } ${ apiRoute } /${ slug } ${ queryString } &draft=true` }
291
317
disabled = { selected . length === 0 }
318
+ selected = { selected }
292
319
/>
293
320
< PublishButton
294
321
action = { `${ serverURL } ${ apiRoute } /${ slug } ${ queryString } &draft=true` }
295
322
disabled = { selected . length === 0 }
323
+ selected = { selected }
296
324
/>
297
325
</ React . Fragment >
298
326
) : (
299
327
< Submit
300
328
action = { `${ serverURL } ${ apiRoute } /${ slug } ${ queryString } ` }
301
329
disabled = { selected . length === 0 }
330
+ selected = { selected }
302
331
/>
303
332
) }
304
333
</ div >
0 commit comments