|
| 1 | +import type { FormKitNode } from '@formkit/core'; |
| 2 | +import type { Method, VisitOptions, RequestPayload } from '@inertiajs/core'; |
| 3 | + |
| 4 | +import { createMessage } from '@formkit/core'; |
| 5 | +import { router } from '@inertiajs/core'; |
| 6 | +import { reactive, toRefs, watchEffect } from 'vue'; |
| 7 | +import { useEventsSystem } from './event'; |
| 8 | + |
| 9 | +export const useForm = <F extends RequestPayload>(initialFields?: F) => { |
| 10 | + const event = useEventsSystem<[node: FormKitNode]>(); |
| 11 | + |
| 12 | + let _recentlySuccessfulTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined; |
| 13 | + let _cancelToken: { |
| 14 | + cancel: () => void; |
| 15 | + } | undefined = undefined; |
| 16 | + |
| 17 | + const state = reactive<{ |
| 18 | + node: FormKitNode | null, |
| 19 | + dirty: boolean | null; |
| 20 | + errors: boolean | null; |
| 21 | + processing: boolean; |
| 22 | + progress: number; |
| 23 | + recentlySuccessful: boolean; |
| 24 | + valid: boolean | null; |
| 25 | + wasSuccessful: boolean; |
| 26 | + }>({ |
| 27 | + node: null, |
| 28 | + dirty: null, |
| 29 | + errors: null, |
| 30 | + processing: false, |
| 31 | + progress: 0, |
| 32 | + recentlySuccessful: false, |
| 33 | + valid: null, |
| 34 | + wasSuccessful: false |
| 35 | + }); |
| 36 | + |
| 37 | + event.combine((on) => { |
| 38 | + on('cancelToken', (token) => { |
| 39 | + _cancelToken = token; |
| 40 | + }); |
| 41 | + |
| 42 | + on('before', () => { |
| 43 | + state.progress = 0; |
| 44 | + state.recentlySuccessful = false; |
| 45 | + state.wasSuccessful = false; |
| 46 | + |
| 47 | + clearInterval(_recentlySuccessfulTimeoutId); |
| 48 | + }); |
| 49 | + |
| 50 | + on('start', (_, node) => { |
| 51 | + node.store.set(createMessage({ |
| 52 | + key: 'loading', |
| 53 | + visible: false, |
| 54 | + value: true |
| 55 | + })); |
| 56 | + |
| 57 | + if (node.props.submitBehavior !== 'live') node.props.disabled = true; |
| 58 | + |
| 59 | + state.processing = true; |
| 60 | + }); |
| 61 | + |
| 62 | + on('progress', (axiosProgress) => { |
| 63 | + state.progress = axiosProgress?.percentage || 0; |
| 64 | + }); |
| 65 | + |
| 66 | + on('success', () => { |
| 67 | + state.recentlySuccessful = true; |
| 68 | + state.wasSuccessful = true; |
| 69 | + |
| 70 | + _recentlySuccessfulTimeoutId = setTimeout(() => { |
| 71 | + state.recentlySuccessful = false; |
| 72 | + }, 2000); |
| 73 | + }); |
| 74 | + |
| 75 | + on('error', (errors, node) => { |
| 76 | + node.setErrors(node.name in errors ? errors[node.name] : [], errors); |
| 77 | + }); |
| 78 | + |
| 79 | + on('finish', (_, node) => { |
| 80 | + _cancelToken = undefined; |
| 81 | + |
| 82 | + node.store.remove('loading'); |
| 83 | + |
| 84 | + if (node.props.submitBehavior !== 'live') node.props.disabled = false; |
| 85 | + |
| 86 | + state.processing = false; |
| 87 | + state.progress = 0; |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + const plugin = (node: FormKitNode) => { |
| 92 | + if (node.props.type !== 'form') return; |
| 93 | + |
| 94 | + state.node = node; |
| 95 | + node.input(initialFields); |
| 96 | + |
| 97 | + node.on('created', () => { |
| 98 | + if (!node.context) return; |
| 99 | + |
| 100 | + watchEffect(() => { |
| 101 | + if (!node.context) return; |
| 102 | + |
| 103 | + state.dirty = node.context.state.dirty; |
| 104 | + state.valid = node.context.state.valid; |
| 105 | + state.errors = node.context.state.errors; |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + return false; |
| 110 | + }; |
| 111 | + |
| 112 | + const _createVisitHandler = (method: Method) => (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => { |
| 113 | + if (method === 'delete') { |
| 114 | + router.delete(url, { |
| 115 | + ...event.toVisitOptions(node), |
| 116 | + ...options, |
| 117 | + data |
| 118 | + }); |
| 119 | + } else { |
| 120 | + router[method](url, data, { |
| 121 | + ...event.toVisitOptions(node), |
| 122 | + ...options, |
| 123 | + }); |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + return { |
| 128 | + get: _createVisitHandler('get'), |
| 129 | + post: _createVisitHandler('post'), |
| 130 | + put: _createVisitHandler('put'), |
| 131 | + patch: _createVisitHandler('patch'), |
| 132 | + delete: _createVisitHandler('delete'), |
| 133 | + cancel: () => { |
| 134 | + if (_cancelToken) _cancelToken.cancel(); |
| 135 | + }, |
| 136 | + |
| 137 | + ...toRefs(state), |
| 138 | + |
| 139 | + on: event.on, |
| 140 | + combine: event.combine, |
| 141 | + |
| 142 | + plugin, |
| 143 | + } |
| 144 | +}; |
0 commit comments