Skip to content

Commit

Permalink
feat(client): only save forms if the value was changed
Browse files Browse the repository at this point in the history
  • Loading branch information
neopostmodern committed Apr 30, 2023
1 parent a6db8ef commit 76b61bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions client/src/renderer/components/LinkForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from 'lodash';
import { isEqual, pick } from 'lodash';
import React from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { LinkQuery } from '../generated/graphql';
Expand Down Expand Up @@ -48,7 +48,10 @@ const LinkForm: React.FC<LinkFormProps> = ({
errors.url =
'Not a valid URL – did you forget the protocol? (e.g. https://)';
}
if (Object.keys(errors).length === 0) {
if (
Object.keys(errors).length === 0 &&
!isEqual(formValues, defaultValues)
) {
onSubmit(formValues);
}
return { values: formValues, errors };
Expand Down
6 changes: 4 additions & 2 deletions client/src/renderer/components/TagForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from 'lodash';
import { isEqual, pick } from 'lodash';
import React from 'react';
import { useForm } from 'react-hook-form';
import styled from 'styled-components';
Expand Down Expand Up @@ -53,7 +53,9 @@ const TagForm: React.FC<TagFormProps> = ({ tag, onSubmit }) => {
mode: 'onBlur',
resolver: (formValues) => {
// todo: validate
onSubmit(formValues);
if (!isEqual(formValues, defaultValues)) {
onSubmit(formValues);
}
return { values: formValues, errors: {} };
},
});
Expand Down
6 changes: 4 additions & 2 deletions client/src/renderer/components/TextForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from 'lodash';
import { isEqual, pick } from 'lodash';
import React from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { NotesForListQuery } from '../generated/graphql';
Expand Down Expand Up @@ -36,7 +36,9 @@ const TextForm: React.FC<TextFormProps> = ({
defaultValues,
mode: 'onBlur',
resolver: (formValues) => {
onSubmit(formValues);
if (!isEqual(formValues, defaultValues)) {
onSubmit(formValues);
}
return { values: formValues, errors: {} };
},
});
Expand Down

0 comments on commit 76b61bb

Please sign in to comment.