From ddb94d7f1343ba5ae4ef0d0e67e89a379ddad6f5 Mon Sep 17 00:00:00 2001 From: David Arranz Date: Mon, 9 Sep 2024 18:48:50 +0200 Subject: [PATCH] . --- client/package.json | 2 +- client/src/app/settings/edit.tsx | 14 +++++------ client/src/app/settings/hooks/useSettings.tsx | 24 +++++++++++++------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/client/package.json b/client/package.json index e25355c..6c999f6 100644 --- a/client/package.json +++ b/client/package.json @@ -14,7 +14,7 @@ "dependencies": { "@dnd-kit/core": "^6.1.0", "@dnd-kit/sortable": "^8.0.0", - "@hookform/resolvers": "^3.5.0", + "@hookform/resolvers": "^3.9.0", "@radix-ui/react-accordion": "^1.1.2", "@radix-ui/react-alert-dialog": "^1.0.5", "@radix-ui/react-aspect-ratio": "^1.0.3", diff --git a/client/src/app/settings/edit.tsx b/client/src/app/settings/edit.tsx index b80f922..d6ed3b8 100644 --- a/client/src/app/settings/edit.tsx +++ b/client/src/app/settings/edit.tsx @@ -12,7 +12,6 @@ import { CardTitle, Form, } from "@/ui"; -import { joiResolver } from "@hookform/resolvers/joi"; import { t } from "i18next"; import { AlertCircleIcon } from "lucide-react"; @@ -22,9 +21,8 @@ import { Trans } from "react-i18next"; import { useUnsavedChangesNotifier } from "@/lib/hooks"; import { cn } from "@/lib/utils"; +import { useToast } from "@/ui/use-toast"; import { IUpdateProfile_Request_DTO } from "@shared/contexts"; -import Joi from "joi"; -import { toast } from "react-toastify"; import { useSettings } from "./hooks"; type SettingsDataForm = IUpdateProfile_Request_DTO; @@ -32,6 +30,7 @@ type SettingsDataForm = IUpdateProfile_Request_DTO; export const SettingsEditor = () => { const [activeSection, setActiveSection] = useState("profile"); const { useOne, useUpdate } = useSettings(); + const { toast } = useToast(); const { data, status, error: queryError } = useOne(); @@ -56,7 +55,7 @@ export const SettingsEditor = () => { mode: "onBlur", values: data?.dealer, defaultValues, - resolver: joiResolver( + /*resolver: joiResolver( Joi.object({ contact_information: Joi.string().optional().allow(null).allow("").default(""), default_payment_method: Joi.string().optional().allow(null).allow("").default(""), @@ -68,7 +67,7 @@ export const SettingsEditor = () => { scale: Joi.number(), }).required(), }) - ), + ),*/ }); const { formState, reset, getValues, handleSubmit } = form; @@ -79,16 +78,17 @@ export const SettingsEditor = () => { }); const onSubmit: SubmitHandler = async (data) => { + console.log("hola"); mutate(data, { onError: (error) => { console.debug(error); - toast.error(error.message); + toast({ title: "Error", description: error.message }); //alert(error.message); }, //onSettled: () => {}, onSuccess: () => { reset(getValues()); - toast.success("Ajustes guardados"); + toast({ description: "Ajustes guardados" }); }, }); }; diff --git a/client/src/app/settings/hooks/useSettings.tsx b/client/src/app/settings/hooks/useSettings.tsx index ac7ed10..7ef27bf 100644 --- a/client/src/app/settings/hooks/useSettings.tsx +++ b/client/src/app/settings/hooks/useSettings.tsx @@ -1,4 +1,4 @@ -import { useOne, useSave } from "@/lib/hooks/useDataSource"; +import { useOne } from "@/lib/hooks/useDataSource"; import { TDataSourceError } from "@/lib/hooks/useDataSource/types"; import { useDataSource } from "@/lib/hooks/useDataSource/useDataSource"; import { useQueryKey } from "@/lib/hooks/useQueryKey"; @@ -7,6 +7,7 @@ import { IUpdateProfile_Request_DTO, IUpdateProfileResponse_DTO, } from "@shared/contexts"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; export type UseSettingsGetParamsType = { enabled?: boolean; @@ -28,15 +29,24 @@ export const useSettings = (params?: UseSettingsGetParamsType) => { }), ...params, }), - useUpdate: () => - useSave({ + useUpdate: () => { + const queryClient = useQueryClient(); + + return useMutation({ mutationKey: keys().data().resource("settings").action("one").id("me").params().get(), - mutationFn: (data) => - dataSource.updateOne({ + mutationFn: (data) => { + return dataSource.updateOne({ resource: "profile", data, id: "", - }), - }), + }); + }, + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["data", "default", "settings"], + }); + }, + }); + }, }; };