@inertiajs/vue3 1.0.0-beta.2 → 1.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/useForm.js DELETED
@@ -1,205 +0,0 @@
1
- import { router } from '@inertiajs/core'
2
- import cloneDeep from 'lodash.clonedeep'
3
- import isEqual from 'lodash.isequal'
4
- import { reactive, watch } from 'vue'
5
-
6
- export default function useForm(...args) {
7
- const rememberKey = typeof args[0] === 'string' ? args[0] : null
8
- const data = (typeof args[0] === 'string' ? args[1] : args[0]) || {}
9
- const restored = rememberKey ? router.restore(rememberKey) : null
10
- let defaults = cloneDeep(data)
11
- let cancelToken = null
12
- let recentlySuccessfulTimeoutId = null
13
- let transform = (data) => data
14
-
15
- let form = reactive({
16
- ...(restored ? restored.data : data),
17
- isDirty: false,
18
- errors: restored ? restored.errors : {},
19
- hasErrors: false,
20
- processing: false,
21
- progress: null,
22
- wasSuccessful: false,
23
- recentlySuccessful: false,
24
- data() {
25
- return Object.keys(data).reduce((carry, key) => {
26
- carry[key] = this[key]
27
- return carry
28
- }, {})
29
- },
30
- transform(callback) {
31
- transform = callback
32
-
33
- return this
34
- },
35
- defaults(key, value) {
36
- if (typeof key === 'undefined') {
37
- defaults = this.data()
38
- } else {
39
- defaults = Object.assign({}, cloneDeep(defaults), value ? { [key]: value } : key)
40
- }
41
-
42
- return this
43
- },
44
- reset(...fields) {
45
- let clonedDefaults = cloneDeep(defaults)
46
- if (fields.length === 0) {
47
- Object.assign(this, clonedDefaults)
48
- } else {
49
- Object.assign(
50
- this,
51
- Object.keys(clonedDefaults)
52
- .filter((key) => fields.includes(key))
53
- .reduce((carry, key) => {
54
- carry[key] = clonedDefaults[key]
55
- return carry
56
- }, {}),
57
- )
58
- }
59
-
60
- return this
61
- },
62
- setError(key, value) {
63
- Object.assign(this.errors, value ? { [key]: value } : key)
64
-
65
- this.hasErrors = Object.keys(this.errors).length > 0
66
-
67
- return this
68
- },
69
- clearErrors(...fields) {
70
- this.errors = Object.keys(this.errors).reduce(
71
- (carry, field) => ({
72
- ...carry,
73
- ...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}),
74
- }),
75
- {},
76
- )
77
-
78
- this.hasErrors = Object.keys(this.errors).length > 0
79
-
80
- return this
81
- },
82
- submit(method, url, options = {}) {
83
- const data = transform(this.data())
84
- const _options = {
85
- ...options,
86
- onCancelToken: (token) => {
87
- cancelToken = token
88
-
89
- if (options.onCancelToken) {
90
- return options.onCancelToken(token)
91
- }
92
- },
93
- onBefore: (visit) => {
94
- this.wasSuccessful = false
95
- this.recentlySuccessful = false
96
- clearTimeout(recentlySuccessfulTimeoutId)
97
-
98
- if (options.onBefore) {
99
- return options.onBefore(visit)
100
- }
101
- },
102
- onStart: (visit) => {
103
- this.processing = true
104
-
105
- if (options.onStart) {
106
- return options.onStart(visit)
107
- }
108
- },
109
- onProgress: (event) => {
110
- this.progress = event
111
-
112
- if (options.onProgress) {
113
- return options.onProgress(event)
114
- }
115
- },
116
- onSuccess: async (page) => {
117
- this.processing = false
118
- this.progress = null
119
- this.clearErrors()
120
- this.wasSuccessful = true
121
- this.recentlySuccessful = true
122
- recentlySuccessfulTimeoutId = setTimeout(() => (this.recentlySuccessful = false), 2000)
123
-
124
- const onSuccess = options.onSuccess ? await options.onSuccess(page) : null
125
- defaults = cloneDeep(this.data())
126
- this.isDirty = false
127
- return onSuccess
128
- },
129
- onError: (errors) => {
130
- this.processing = false
131
- this.progress = null
132
- this.clearErrors().setError(errors)
133
-
134
- if (options.onError) {
135
- return options.onError(errors)
136
- }
137
- },
138
- onCancel: () => {
139
- this.processing = false
140
- this.progress = null
141
-
142
- if (options.onCancel) {
143
- return options.onCancel()
144
- }
145
- },
146
- onFinish: () => {
147
- this.processing = false
148
- this.progress = null
149
- cancelToken = null
150
-
151
- if (options.onFinish) {
152
- return options.onFinish()
153
- }
154
- },
155
- }
156
-
157
- if (method === 'delete') {
158
- router.delete(url, { ..._options, data })
159
- } else {
160
- router[method](url, data, _options)
161
- }
162
- },
163
- get(url, options) {
164
- this.submit('get', url, options)
165
- },
166
- post(url, options) {
167
- this.submit('post', url, options)
168
- },
169
- put(url, options) {
170
- this.submit('put', url, options)
171
- },
172
- patch(url, options) {
173
- this.submit('patch', url, options)
174
- },
175
- delete(url, options) {
176
- this.submit('delete', url, options)
177
- },
178
- cancel() {
179
- if (cancelToken) {
180
- cancelToken.cancel()
181
- }
182
- },
183
- __rememberable: rememberKey === null,
184
- __remember() {
185
- return { data: this.data(), errors: this.errors }
186
- },
187
- __restore(restored) {
188
- Object.assign(this, restored.data)
189
- this.setError(restored.errors)
190
- },
191
- })
192
-
193
- watch(
194
- form,
195
- (newValue) => {
196
- form.isDirty = !isEqual(form.data(), defaults)
197
- if (rememberKey) {
198
- router.remember(cloneDeep(newValue.__remember()), rememberKey)
199
- }
200
- },
201
- { immediate: true, deep: true },
202
- )
203
-
204
- return form
205
- }
@@ -1,24 +0,0 @@
1
- import { router } from '@inertiajs/core'
2
- import cloneDeep from 'lodash.clonedeep'
3
- import { isReactive, reactive, ref, watch } from 'vue'
4
-
5
- export default function useRemember(data, key) {
6
- if (typeof data === 'object' && data !== null && data.__rememberable === false) {
7
- return data
8
- }
9
-
10
- const restored = router.restore(key)
11
- const type = isReactive(data) ? reactive : ref
12
- const hasCallbacks = typeof data.__remember === 'function' && typeof data.__restore === 'function'
13
- const remembered = restored === undefined ? data : type(hasCallbacks ? data.__restore(restored) : restored)
14
-
15
- watch(
16
- remembered,
17
- (newValue) => {
18
- router.remember(cloneDeep(hasCallbacks ? data.__remember() : newValue), key)
19
- },
20
- { immediate: true, deep: true },
21
- )
22
-
23
- return remembered
24
- }