refactor: add useQuery on whoami endpoint request (#3125)
* 📝 (authContext.tsx): Add useGetUserData hook to fetch user data and update user state in AuthProvider component 📝 (use-get-user.ts): Refactor useGetUserData hook to use mutation function for fetching user data and handling success and error cases in a more structured way * 📝 (API/index.ts): Remove getProfilePictures function as it is no longer used 🔧 (use-get-profile-pictures.ts): Remove unused code related to fetching profile pictures 🔧 (use-preload-images.tsx): Refactor usePreloadImages hook to accept profilePictures as an argument 🔧 (profilePictureChooserComponent/index.tsx): Update usage of usePreloadImages hook to match new signature 🔧 (ProfilePictureForm/index.tsx): Refactor to use useGetProfilePicturesQuery hook instead of handleGetProfilePictures function
This commit is contained in:
parent
df502f7b9c
commit
d94bb70d95
2 changed files with 32 additions and 21 deletions
|
|
@ -3,6 +3,7 @@ import {
|
||||||
LANGFLOW_API_TOKEN,
|
LANGFLOW_API_TOKEN,
|
||||||
LANGFLOW_AUTO_LOGIN_OPTION,
|
LANGFLOW_AUTO_LOGIN_OPTION,
|
||||||
} from "@/constants/constants";
|
} from "@/constants/constants";
|
||||||
|
import { useGetUserData } from "@/controllers/API/queries/auth";
|
||||||
import useAuthStore from "@/stores/authStore";
|
import useAuthStore from "@/stores/authStore";
|
||||||
import useFlowsManagerStore from "@/stores/flowsManagerStore";
|
import useFlowsManagerStore from "@/stores/flowsManagerStore";
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
|
|
@ -52,6 +53,8 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const setIsLoading = useFlowsManagerStore((state) => state.setIsLoading);
|
const setIsLoading = useFlowsManagerStore((state) => state.setIsLoading);
|
||||||
const autoLogin = useAuthStore((state) => state.autoLogin);
|
const autoLogin = useAuthStore((state) => state.autoLogin);
|
||||||
|
|
||||||
|
const { mutate: mutateLoggedUser } = useGetUserData();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const storedAccessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
const storedAccessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
||||||
if (storedAccessToken) {
|
if (storedAccessToken) {
|
||||||
|
|
@ -67,19 +70,23 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
function getUser() {
|
function getUser() {
|
||||||
getLoggedUser()
|
mutateLoggedUser(
|
||||||
.then(async (user) => {
|
{},
|
||||||
setUserData(user);
|
{
|
||||||
const isSuperUser = user!.is_superuser;
|
onSuccess: async (user) => {
|
||||||
useAuthStore.getState().setIsAdmin(isSuperUser);
|
setUserData(user);
|
||||||
getFoldersApi(true, true);
|
const isSuperUser = user!.is_superuser;
|
||||||
|
useAuthStore.getState().setIsAdmin(isSuperUser);
|
||||||
checkHasStore();
|
getFoldersApi(true, true);
|
||||||
fetchApiData();
|
checkHasStore();
|
||||||
})
|
fetchApiData();
|
||||||
.catch((error) => {
|
},
|
||||||
setLoading(false);
|
onError: () => {
|
||||||
});
|
setUserData(null);
|
||||||
|
setLoading(false);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(newAccessToken: string, autoLogin: string) {
|
function login(newAccessToken: string, autoLogin: string) {
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,24 @@
|
||||||
import { keepPreviousData } from "@tanstack/react-query";
|
import { UseMutationResult } from "@tanstack/react-query";
|
||||||
import { Users, useQueryFunctionType } from "../../../../types/api";
|
import { useMutationFunctionType, Users } from "../../../../types/api";
|
||||||
import { api } from "../../api";
|
import { api } from "../../api";
|
||||||
import { getURL } from "../../helpers/constants";
|
import { getURL } from "../../helpers/constants";
|
||||||
import { UseRequestProcessor } from "../../services/request-processor";
|
import { UseRequestProcessor } from "../../services/request-processor";
|
||||||
|
|
||||||
export const useGetUserData: useQueryFunctionType<undefined, Users> = () => {
|
export const useGetUserData: useMutationFunctionType<undefined, any> = (
|
||||||
const { query } = UseRequestProcessor();
|
options?,
|
||||||
|
) => {
|
||||||
|
const { mutate } = UseRequestProcessor();
|
||||||
|
|
||||||
const getUserData = async () => {
|
const getUserData = async () => {
|
||||||
const response = await api.get<Users>(`${getURL("USERS")}/whoami`);
|
const response = await api.get<Users>(`${getURL("USERS")}/whoami`);
|
||||||
return response["data"];
|
return response["data"];
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryResult = query(["useGetUserData"], getUserData, {
|
const mutation: UseMutationResult = mutate(
|
||||||
placeholderData: keepPreviousData,
|
["useGetUserData"],
|
||||||
});
|
getUserData,
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
|
||||||
return queryResult;
|
return mutation;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue