Skip to content

Commit

Permalink
enhance: Prevent axios bundle when using useForm() with SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
iksaku committed May 25, 2023
1 parent 804d890 commit d3b536c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cloneDeep from 'lodash.clonedeep'
import isEqual from 'lodash.isequal'
import { createEffect, createMemo, createSignal } from 'solid-js'
import { createStore, reconcile, SetStoreFunction, Store, unwrap } from 'solid-js/store'
import { isServer } from 'solid-js/web'

type FormState = Record<string, unknown>
type InertiaRestoredState<TForm extends FormState> = { data: TForm; errors: Record<keyof TForm, string> }
Expand Down Expand Up @@ -32,7 +33,10 @@ interface InertiaFormProps<TForm extends FormState> {
cancel(): void
}

export type InertiaForm<TForm extends FormState> = [get: Store<InertiaFormProps<TForm>>, set: SetStoreFunction<TForm>]
export type InertiaForm<TForm extends FormState> = [
get: Store<InertiaFormProps<TForm> & TForm>,
set: SetStoreFunction<TForm>,
]

export default function useForm<TForm extends FormState>(initialValues?: TForm): InertiaForm<TForm>
export default function useForm<TForm extends FormState>(rememberKey: string, initialValues?: TForm): InertiaForm<TForm>
Expand All @@ -42,9 +46,8 @@ export default function useForm<TForm extends FormState>(
): InertiaForm<TForm> {
const rememberKey: string | null = typeof rememberKeyOrInitialValues === 'string' ? rememberKeyOrInitialValues : null
const data: TForm = typeof rememberKeyOrInitialValues === 'string' ? maybeInitialValues : rememberKeyOrInitialValues
const restored: InertiaRestoredState<TForm> | null = rememberKey
? (router.restore(rememberKey) as InertiaRestoredState<TForm>)
: null
const restored: InertiaRestoredState<TForm> | null =
!isServer && rememberKey ? (router.restore(rememberKey) as InertiaRestoredState<TForm>) : null

const [defaults, setDefaults] = createSignal(data)
let cancelToken = null
Expand Down Expand Up @@ -180,6 +183,8 @@ export default function useForm<TForm extends FormState>(
hasErrorsMemo = createMemo(() => Object.keys(unwrap(deeplyTrackedFormErrors())).length > 0)

submit = (method: Method, url: string, options: Partial<VisitOptions> = {}) => {
if (isServer) return

const data = transform(form.data)
const _options = {
...options,
Expand All @@ -191,8 +196,10 @@ export default function useForm<TForm extends FormState>(
}
},
onBefore(visit) {
setForm('wasSuccessful', true)
setForm('recentlySuccessful', false)
setForm({
wasSuccessful: true,
recentlySuccessful: false,
})
clearTimeout(recentlySuccessfulTimeoutId)

if (options.onBefore) {
Expand Down Expand Up @@ -272,7 +279,7 @@ export default function useForm<TForm extends FormState>(
}
}

if (rememberKey) {
if (!isServer && rememberKey) {
createEffect(() => {
router.remember({ data: unwrap(form.data), errors: unwrap(deeplyTrackedFormErrors()) }, rememberKey)
})
Expand Down

0 comments on commit d3b536c

Please sign in to comment.