fix(auth): sync navbar user after self admin edit
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
Keep the logged-in user context and cookie in sync when an admin updates their own profile so the navbar reflects name changes immediately. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { API_ENDPOINTS, userService } from "@/lib/api";
|
||||
import { useUser } from "@/contexts";
|
||||
import { API_ENDPOINTS, ICreateAdminCredentials, userService } from "@/lib/api";
|
||||
import {
|
||||
useInfiniteQuery,
|
||||
useMutation,
|
||||
@@ -9,6 +10,11 @@ import { useRouter } from "next/navigation";
|
||||
import { useMemo } from "react";
|
||||
import { toast } from "react-toastify";
|
||||
|
||||
const toCurrentUserUpdates = ({
|
||||
password: _password,
|
||||
...profile
|
||||
}: ICreateAdminCredentials) => profile;
|
||||
|
||||
export const useDeleteAdmin = (onSuccessCallback: () => void) => {
|
||||
const queryClient = useQueryClient();
|
||||
const mutationKey = useMemo(() => [API_ENDPOINTS.USER.ADMINS, "delete"], []);
|
||||
@@ -45,6 +51,7 @@ export const useCreateAdmin = () => {
|
||||
export const useUpdateAdmin = (id: string) => {
|
||||
const queryClient = useQueryClient();
|
||||
const router = useRouter();
|
||||
const { user, syncCurrentUser } = useUser();
|
||||
const mutationKey = useMemo(
|
||||
() => [API_ENDPOINTS.USER.ADMINS, "update", id],
|
||||
[id],
|
||||
@@ -52,7 +59,11 @@ export const useUpdateAdmin = (id: string) => {
|
||||
return useMutation({
|
||||
mutationKey,
|
||||
mutationFn: userService.updateAdmin,
|
||||
onSuccess: (response) => {
|
||||
onSuccess: (response, { id: adminId, credentials }) => {
|
||||
if (user?.id === adminId) {
|
||||
syncCurrentUser(toCurrentUserUpdates(credentials));
|
||||
}
|
||||
|
||||
toast.success(response);
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [API_ENDPOINTS.USER.ADMINS],
|
||||
|
||||
@@ -14,7 +14,7 @@ export const useProfileQuery = () => {
|
||||
});
|
||||
};
|
||||
export const useUpdateProfileQuery = () => {
|
||||
const { setUser } = useUser();
|
||||
const { syncCurrentUser } = useUser();
|
||||
const queryClient = useQueryClient();
|
||||
const mutationKey = useMemo(
|
||||
() => [API_ENDPOINTS.PROFILE.UPDATE, "UPDATE_USER_PROFILE"],
|
||||
@@ -28,7 +28,7 @@ export const useUpdateProfileQuery = () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [API_ENDPOINTS.PROFILE.READ, "CURRENT_USER_PROFILE"],
|
||||
});
|
||||
setUser(response);
|
||||
syncCurrentUser(response);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user