udpate /me
This commit is contained in:
1 parent
666d522ee9
commit
422d13ef25
8 files changed
+70
-24
No files matched your search
@@ -4,16 +4,20 @@ import { ProfileForm } from "@/pages/profile/ProfileForm";
|
||||
import { useState, useEffect } from "react";
|
||||
import type { IUser } from "@/types/user";
|
||||
import type { ApiResponse } from "@/hooks/UseApi";
|
||||
import { useGet, usePatch } from "@/hooks/UseApi";
|
||||
import { useGet, usePatch, useGetOnDemand } from "@/hooks/UseApi";
|
||||
import { z } from "zod";
|
||||
import { profileSchema } from "@/validation/zod";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useAppDispatch } from "@/hooks/AppHooks";
|
||||
import { setUser as setUserGlobal } from "@/features/authSlice";
|
||||
type FormValues = z.infer<typeof profileSchema>;
|
||||
|
||||
const Profile = () => {
|
||||
const { t } = useTranslation();
|
||||
const [user, setUser] = useState<Partial<IUser> | null>(null);
|
||||
const { response, loading } = useGet<ApiResponse<any>>(`/profile`, {}, {});
|
||||
const { get: getOnDemand } = useGetOnDemand<IUser>();
|
||||
const dispatch = useAppDispatch();
|
||||
useEffect(() => {
|
||||
if (response?.data) setUser(response.data);
|
||||
}, [response]);
|
||||
@@ -23,7 +27,11 @@ const Profile = () => {
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
const res = await patch(`/profile`, data);
|
||||
if (res) {
|
||||
setUser(res.data);
|
||||
const me = await getOnDemand("/me");
|
||||
if (me?.data) {
|
||||
setUser(me.data);
|
||||
dispatch(setUserGlobal(me.data as IUser));
|
||||
}
|
||||
}
|
||||
};
|
||||
const initialData = {
|
||||
|
||||
Reference in new issue
Block a user