Skip to content

Commit

Permalink
feat: console.debug when auto saves are skipped. (#105)
Browse files Browse the repository at this point in the history
It can be really frustrating to wonder "wtf is this not working", and
previously the fix was to "know where to go in the form-state source to
set the break point an examine the form". :-|
  • Loading branch information
stephenh authored Dec 15, 2023
1 parent f56404d commit dc822f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/useFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ export function useFormState<T, I>(opts: UseFormStateOpts<T, I>): ObjectState<T>
}

// Don't use canSave() because we don't want to set touched for all the fields
if (autoSaveRef.current && form.dirty && form.valid && !isAutoSaving) {
if (autoSaveRef.current && form.dirty && !isAutoSaving) {
// It's very frustrating to not know why the form is savings, to go ahead and log these
if (!form.valid) {
console.debug("Skipping auto-save b/c form is invalid: ", form.errors);
return;
}
isAutoSaving = "queued";
let maybeError: undefined | string;
// We use setTimeout as a cheap way to wait until the end of the current event listener
Expand Down
9 changes: 7 additions & 2 deletions src/useFormStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ export function useFormStates<T, I = T>(opts: UseFormStatesOpts<T, I>): UseFormS
let form = existing?.[0];

async function maybeAutoSave(form: ObjectState<T>) {
// Don't use canSave() because we don't want to set touched for all the fields
if (autoSaveRef.current && form.dirty && form.valid) {
// Don't use form.canSave() because we don't want to set touched for all the fields
if (autoSaveRef.current && form.dirty) {
// It's very frustrating to not know why the form is savings, to go ahead and log these
if (!form.valid) {
console.debug("Skipping auto-save b/c form is invalid: ", form.errors);
return;
}
const { current: pending } = pendingAutoSaves;
if (isAutoSaving) {
pending.add(form);
Expand Down

0 comments on commit dc822f7

Please sign in to comment.