@@ -5,7 +5,6 @@ import type { ClientCollectionConfig, ClientGlobalConfig } from 'payload'
5
5
import { dequal } from 'dequal/lite'
6
6
import { reduceFieldsToValues , versionDefaults } from 'payload/shared'
7
7
import React , { useDeferredValue , useEffect , useRef , useState } from 'react'
8
- import { toast } from 'sonner'
9
8
10
9
import {
11
10
useAllFormFields ,
@@ -17,13 +16,11 @@ import { useDebounce } from '../../hooks/useDebounce.js'
17
16
import { useEffectEvent } from '../../hooks/useEffectEvent.js'
18
17
import { useQueues } from '../../hooks/useQueues.js'
19
18
import { useConfig } from '../../providers/Config/index.js'
20
- import { useDocumentEvents } from '../../providers/DocumentEvents/index.js'
21
19
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js'
22
20
import { useLocale } from '../../providers/Locale/index.js'
23
21
import { useTranslation } from '../../providers/Translation/index.js'
24
22
import { formatTimeToNow } from '../../utilities/formatDocTitle/formatDateTitle.js'
25
23
import { reduceFieldsToValuesWithValidation } from '../../utilities/reduceFieldsToValuesWithValidation.js'
26
- import { useDocumentDrawerContext } from '../DocumentDrawer/Provider.js'
27
24
import { LeaveWithoutSaving } from '../LeaveWithoutSaving/index.js'
28
25
import './index.scss'
29
26
@@ -51,16 +48,11 @@ export const Autosave: React.FC<Props> = ({ id, collection, global: globalDoc })
51
48
incrementVersionCount,
52
49
lastUpdateTime,
53
50
mostRecentVersionIsAutosaved,
54
- setLastUpdateTime,
55
51
setMostRecentVersionIsAutosaved,
56
52
setUnpublishedVersionCount,
57
- updateSavedDocumentData,
58
53
} = useDocumentInfo ( )
59
54
60
- const { onSave : onSaveFromDocumentDrawer } = useDocumentDrawerContext ( )
61
-
62
- const { reportUpdate } = useDocumentEvents ( )
63
- const { dispatchFields, isValid, setBackgroundProcessing, setIsValid } = useForm ( )
55
+ const { isValid, setBackgroundProcessing, submit } = useForm ( )
64
56
65
57
const [ formState ] = useAllFormFields ( )
66
58
const modified = useFormModified ( )
@@ -151,118 +143,38 @@ export const Autosave: React.FC<Props> = ({ id, collection, global: globalDoc })
151
143
method = 'POST'
152
144
}
153
145
154
- if ( url ) {
155
- if ( modifiedRef . current ) {
156
- const { data, valid } = reduceFieldsToValuesWithValidation ( formStateRef . current , true )
157
-
158
- data . _status = 'draft'
159
-
160
- const skipSubmission =
161
- submitted && ! valid && versionsConfig ?. drafts && versionsConfig ?. drafts ?. validate
162
-
163
- if ( ! skipSubmission ) {
164
- let res
165
-
166
- try {
167
- res = await fetch ( url , {
168
- body : JSON . stringify ( data ) ,
169
- credentials : 'include' ,
170
- headers : {
171
- 'Accept-Language' : i18n . language ,
172
- 'Content-Type' : 'application/json' ,
173
- } ,
174
- method,
175
- } )
176
- } catch ( _err ) {
177
- // Swallow Error
178
- }
179
-
180
- const newDate = new Date ( )
181
- // We need to log the time in order to figure out if we need to trigger the state off later
182
- endTimestamp = newDate . getTime ( )
183
-
184
- const json = await res . json ( )
185
-
186
- if ( res . status === 200 ) {
187
- setLastUpdateTime ( newDate . getTime ( ) )
188
-
189
- reportUpdate ( {
190
- id,
191
- entitySlug,
192
- updatedAt : newDate . toISOString ( ) ,
193
- } )
194
-
195
- // if onSaveFromDocumentDrawer is defined, call it
196
- if ( typeof onSaveFromDocumentDrawer === 'function' ) {
197
- void onSaveFromDocumentDrawer ( {
198
- ...json ,
199
- operation : 'update' ,
200
- } )
201
- }
202
-
203
- if ( ! mostRecentVersionIsAutosaved ) {
204
- incrementVersionCount ( )
205
- setMostRecentVersionIsAutosaved ( true )
206
- setUnpublishedVersionCount ( ( prev ) => prev + 1 )
207
- }
208
- }
209
-
210
- if ( versionsConfig ?. drafts && versionsConfig ?. drafts ?. validate && json ?. errors ) {
211
- if ( Array . isArray ( json . errors ) ) {
212
- const [ fieldErrors , nonFieldErrors ] = json . errors . reduce (
213
- ( [ fieldErrs , nonFieldErrs ] , err ) => {
214
- const newFieldErrs = [ ]
215
- const newNonFieldErrs = [ ]
216
-
217
- if ( err ?. message ) {
218
- newNonFieldErrs . push ( err )
219
- }
220
-
221
- if ( Array . isArray ( err ?. data ) ) {
222
- err . data . forEach ( ( dataError ) => {
223
- if ( dataError ?. field ) {
224
- newFieldErrs . push ( dataError )
225
- } else {
226
- newNonFieldErrs . push ( dataError )
227
- }
228
- } )
229
- }
230
-
231
- return [
232
- [ ...fieldErrs , ...newFieldErrs ] ,
233
- [ ...nonFieldErrs , ...newNonFieldErrs ] ,
234
- ]
235
- } ,
236
- [ [ ] , [ ] ] ,
237
- )
238
-
239
- dispatchFields ( {
240
- type : 'ADD_SERVER_ERRORS' ,
241
- errors : fieldErrors ,
242
- } )
243
-
244
- nonFieldErrors . forEach ( ( err ) => {
245
- toast . error ( err . message || i18n . t ( 'error:unknown' ) )
246
- } )
247
-
248
- setIsValid ( false )
249
- hideIndicator ( )
250
- return
251
- }
252
- } else {
253
- // If it's not an error then we can update the document data inside the context
254
- const document = json ?. doc || json ?. result
255
-
256
- // Manually update the data since this function doesn't fire the `submit` function from useForm
257
- if ( document ) {
258
- setIsValid ( true )
259
- updateSavedDocumentData ( document )
260
- }
261
- }
262
-
263
- hideIndicator ( )
264
- }
146
+ const { valid } = reduceFieldsToValuesWithValidation ( formStateRef . current , true )
147
+
148
+ const skipSubmission =
149
+ submitted && ! valid && versionsConfig ?. drafts && versionsConfig ?. drafts ?. validate
150
+
151
+ if ( ! skipSubmission && modifiedRef . current && url ) {
152
+ const result = await submit ( {
153
+ action : url ,
154
+ context : {
155
+ incrementVersionCount : false ,
156
+ } ,
157
+ disableFormWhileProcessing : false ,
158
+ disableSuccessStatus : true ,
159
+ method,
160
+ overrides : {
161
+ _status : 'draft' ,
162
+ } ,
163
+ skipValidation : versionsConfig ?. drafts && ! versionsConfig ?. drafts ?. validate ,
164
+ } )
165
+
166
+ if ( result && result ?. res ?. ok && ! mostRecentVersionIsAutosaved ) {
167
+ incrementVersionCount ( )
168
+ setMostRecentVersionIsAutosaved ( true )
169
+ setUnpublishedVersionCount ( ( prev ) => prev + 1 )
265
170
}
171
+
172
+ const newDate = new Date ( )
173
+
174
+ // We need to log the time in order to figure out if we need to trigger the state off later
175
+ endTimestamp = newDate . getTime ( )
176
+
177
+ hideIndicator ( )
266
178
}
267
179
}
268
180
} ,
0 commit comments