From 76b61bb31ad9cceceace23f165c258212239b9cc Mon Sep 17 00:00:00 2001 From: neopostmodern Date: Sun, 30 Apr 2023 04:52:14 +0200 Subject: [PATCH] feat(client): only save forms if the value was changed --- client/src/renderer/components/LinkForm.tsx | 7 +++++-- client/src/renderer/components/TagForm.tsx | 6 ++++-- client/src/renderer/components/TextForm.tsx | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/client/src/renderer/components/LinkForm.tsx b/client/src/renderer/components/LinkForm.tsx index 22896c7..235515c 100644 --- a/client/src/renderer/components/LinkForm.tsx +++ b/client/src/renderer/components/LinkForm.tsx @@ -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'; @@ -48,7 +48,10 @@ const LinkForm: React.FC = ({ 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 }; diff --git a/client/src/renderer/components/TagForm.tsx b/client/src/renderer/components/TagForm.tsx index 1f344b0..ac563a9 100644 --- a/client/src/renderer/components/TagForm.tsx +++ b/client/src/renderer/components/TagForm.tsx @@ -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'; @@ -53,7 +53,9 @@ const TagForm: React.FC = ({ tag, onSubmit }) => { mode: 'onBlur', resolver: (formValues) => { // todo: validate - onSubmit(formValues); + if (!isEqual(formValues, defaultValues)) { + onSubmit(formValues); + } return { values: formValues, errors: {} }; }, }); diff --git a/client/src/renderer/components/TextForm.tsx b/client/src/renderer/components/TextForm.tsx index d29958c..f778913 100644 --- a/client/src/renderer/components/TextForm.tsx +++ b/client/src/renderer/components/TextForm.tsx @@ -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'; @@ -36,7 +36,9 @@ const TextForm: React.FC = ({ defaultValues, mode: 'onBlur', resolver: (formValues) => { - onSubmit(formValues); + if (!isEqual(formValues, defaultValues)) { + onSubmit(formValues); + } return { values: formValues, errors: {} }; }, });