forked from inertiajs/inertia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseForm.js
215 lines (192 loc) · 5.69 KB
/
useForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { router } from '@inertiajs/core'
import isEqual from 'lodash.isequal'
import cloneDeep from 'lodash.clonedeep'
import { writable } from 'svelte/store'
import { getContext } from 'svelte'
function useForm(...args) {
const rememberKey = typeof args[0] === 'string' ? args[0] : null
const data = (typeof args[0] === 'string' ? args[1] : args[0]) || {}
const restored = rememberKey ? router.restore(rememberKey) : null
const frameId = getContext('inertia:frame-id')
const frameSrc = getContext('inertia:frame-src')
let defaults = cloneDeep(data)
let cancelToken = null
let recentlySuccessfulTimeoutId = null
let transform = (data) => data
const store = writable({
...(restored ? restored.data : data),
isDirty: false,
errors: restored ? restored.errors : {},
hasErrors: false,
progress: null,
wasSuccessful: false,
recentlySuccessful: false,
processing: false,
setStore(key, value) {
store.update((store) => {
return Object.assign(store, typeof key === 'string' ? { [key]: value } : key)
})
},
data() {
return Object.keys(data).reduce((carry, key) => {
carry[key] = this[key]
return carry
}, {})
},
transform(callback) {
transform = callback
return this
},
defaults(key, value) {
if (typeof key === 'undefined') {
defaults = Object.assign(defaults, cloneDeep(this.data()))
return this
}
defaults = Object.assign(defaults, cloneDeep(value ? { [key]: value } : key))
return this
},
reset(...fields) {
let clonedDefaults = cloneDeep(defaults)
if (fields.length === 0) {
this.setStore(clonedDefaults)
} else {
this.setStore(
Object.keys(clonedDefaults)
.filter((key) => fields.includes(key))
.reduce((carry, key) => {
carry[key] = clonedDefaults[key]
return carry
}, {}),
)
}
return this
},
setError(key, value) {
this.setStore('errors', {
...this.errors,
...(value ? { [key]: value } : key),
})
return this
},
clearErrors(...fields) {
this.setStore(
'errors',
Object.keys(this.errors).reduce(
(carry, field) => ({
...carry,
...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}),
}),
{},
),
)
return this
},
submit(method, url, options = {}) {
const data = transform(this.data())
if (frameSrc) data.frameSrc = frameSrc
const _options = {
...options,
target: typeof(options.target) !== 'undefined' ? options.target : frameId,
onCancelToken: (token) => {
cancelToken = token
if (options.onCancelToken) {
return options.onCancelToken(token)
}
},
onBefore: (visit) => {
this.setStore('wasSuccessful', false)
this.setStore('recentlySuccessful', false)
clearTimeout(recentlySuccessfulTimeoutId)
if (options.onBefore) {
return options.onBefore(visit)
}
},
onStart: (visit) => {
this.setStore('processing', true)
if (options.onStart) {
return options.onStart(visit)
}
},
onProgress: (event) => {
this.setStore('progress', event)
if (options.onProgress) {
return options.onProgress(event)
}
},
onSuccess: async (page) => {
this.setStore('processing', false)
this.setStore('progress', null)
this.clearErrors()
this.setStore('wasSuccessful', true)
this.setStore('recentlySuccessful', true)
recentlySuccessfulTimeoutId = setTimeout(() => this.setStore('recentlySuccessful', false), 2000)
if (options.onSuccess) {
return options.onSuccess(page)
}
},
onError: (errors) => {
this.setStore('processing', false)
this.setStore('progress', null)
this.clearErrors().setError(errors)
if (options.onError) {
return options.onError(errors)
}
},
onCancel: () => {
this.setStore('processing', false)
this.setStore('progress', null)
if (options.onCancel) {
return options.onCancel()
}
},
onFinish: () => {
this.setStore('processing', false)
this.setStore('progress', null)
cancelToken = null
if (options.onFinish) {
return options.onFinish()
}
},
}
if (method === 'delete') {
router.delete(url, { ..._options, data })
} else {
router[method](url, data, _options)
}
},
get(url, options) {
this.submit('get', url, options)
},
post(url, options) {
this.submit('post', url, options)
},
put(url, options) {
this.submit('put', url, options)
},
patch(url, options) {
this.submit('patch', url, options)
},
delete(url, options) {
this.submit('delete', url, options)
},
cancel() {
if (cancelToken) {
cancelToken.cancel()
}
},
})
store.subscribe((form) => {
if (form.isDirty === isEqual(form.data(), defaults)) {
form.setStore('isDirty', !form.isDirty)
}
const hasErrors = Object.keys(form.errors).length > 0
if (form.hasErrors !== hasErrors) {
form.setStore('hasErrors', !form.hasErrors)
}
if (rememberKey) {
router.remember({ data: form.data(), errors: form.errors }, rememberKey)
}
})
return store
}
export default useForm