From d9a8ab3864c7d3d96638b897515ca0ffd3734f30 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Thu, 24 Nov 2022 04:53:40 +0530 Subject: [PATCH] fix: disable update button when nothing changed (#5592) * fix: disable update button when nothing changed Signed-off-by: Udit Takkar * fix: invalidate after dsubmission Signed-off-by: Udit Takkar * Fix isDirty state for appearance settings Signed-off-by: Udit Takkar Co-authored-by: alannnc --- .../pages/settings/my-account/appearance.tsx | 247 ++++++++++-------- 1 file changed, 136 insertions(+), 111 deletions(-) diff --git a/apps/web/pages/settings/my-account/appearance.tsx b/apps/web/pages/settings/my-account/appearance.tsx index 5642b61c00..9da3eed0b2 100644 --- a/apps/web/pages/settings/my-account/appearance.tsx +++ b/apps/web/pages/settings/my-account/appearance.tsx @@ -41,9 +41,25 @@ const SkeletonLoader = () => { const AppearanceView = () => { const { t } = useLocale(); + const utils = trpc.useContext(); const { data: user, isLoading } = trpc.viewer.me.useQuery(); + + const formMethods = useForm({ + defaultValues: { + theme: user?.theme, + brandColor: user?.brandColor || "#292929", + darkBrandColor: user?.darkBrandColor || "#fafafa", + hideBranding: user?.hideBranding, + }, + }); + + const { + formState: { isSubmitting, isDirty }, + } = formMethods; + const mutation = trpc.viewer.updateProfile.useMutation({ - onSuccess: () => { + onSuccess: async () => { + await utils.viewer.me.invalidate(); showToast(t("settings_updated_successfully"), "success"); }, onError: () => { @@ -51,129 +67,138 @@ const AppearanceView = () => { }, }); - const formMethods = useForm(); - if (isLoading) return ; - if (user) - return ( -
{ - mutation.mutate({ - ...values, - // Radio values don't support null as values, therefore we convert an empty string - // back to null here. - theme: values.theme || null, - }); - }}> - -
-
-

{t("theme")}

-

{t("theme_applies_note")}

-
-
-
- - - -
+ if (!user) return null; -
-
-
-

{t("custom_brand_colors")}

-

{t("customize_your_brand_colors")}

-
-
+ const isDisabled = isSubmitting || !isDirty; -
- ( -
-

{t("light_brand_color")}

- formMethods.setValue("brandColor", value)} - /> -
- )} - /> - ( -
-

{t("dark_brand_color")}

- formMethods.setValue("darkBrandColor", value)} - /> -
- )} - /> + return ( + { + mutation.mutate({ + ...values, + // Radio values don't support null as values, therefore we convert an empty string + // back to null here. + theme: values.theme || null, + }); + }}> + +
+
+

{t("theme")}

+

{t("theme_applies_note")}

- {/* TODO future PR to preview brandColors */} - {/* */} -
- ( - <> -
-
-
-

{t("disable_cal_branding")}

{" "} - {t("pro")} -
-

{t("removes_cal_branding")}

-
-
- formMethods.setValue("hideBranding", checked)} - checked={value} - /> +
+ ( + <> +
+
+
+

{t("disable_cal_branding")}

+ {t("pro")}
+

{t("removes_cal_branding")}

- - )} - /> - - - ); +
+ + formMethods.setValue("hideBranding", checked, { shouldDirty: true }) + } + checked={value} + /> +
+
+ + )} + /> + + + ); }; AppearanceView.getLayout = getLayout;