From 4fc24591d3243b0e774ad8759d704a0bbfcac01b Mon Sep 17 00:00:00 2001 From: Mikkel Jakobsen Date: Sun, 5 Nov 2023 17:02:34 +0100 Subject: [PATCH] Make useUserInfo aware of the userinfo url by it self Like we do with other query hooks. Also renamed useGetUserInfo.ts to seUserInfo.ts - no need to use a different filename for the hook. --- .../create-patron-user-info/CreatePatron.tsx | 9 ++-- .../{useGetUserInfo.ts => useUserInfo.ts} | 42 +++++++++++-------- 2 files changed, 28 insertions(+), 23 deletions(-) rename src/core/adgangsplatformen/{useGetUserInfo.ts => useUserInfo.ts} (59%) diff --git a/src/apps/create-patron-user-info/CreatePatron.tsx b/src/apps/create-patron-user-info/CreatePatron.tsx index 72fe31fcef..89925ef4cb 100644 --- a/src/apps/create-patron-user-info/CreatePatron.tsx +++ b/src/apps/create-patron-user-info/CreatePatron.tsx @@ -4,17 +4,14 @@ import { useUrls } from "../../core/utils/url"; import { useText } from "../../core/utils/text"; import { redirectTo } from "../../core/utils/helpers/url"; import { useConfig } from "../../core/utils/config"; -import useUserInfo from "../../core/adgangsplatformen/useGetUserInfo"; +import useUserInfo from "../../core/adgangsplatformen/useUserInfo"; const CreatePatron: FC = () => { const [cpr, setCpr] = useState(null); const config = useConfig(); const t = useText(); - const { userinfoUrl, dashboardUrl } = useUrls(); + const { dashboardUrl } = useUrls(); - if (!userinfoUrl) { - throw new Error("userinfoUrl is not defined"); - } const { id: agencyId } = config<{ id: `${number}`; }>("agencyConfig", { @@ -22,7 +19,7 @@ const CreatePatron: FC = () => { }); // Fetch user info data. - const { data: userInfo, isLoading } = useUserInfo(String(userinfoUrl)); + const { data: userInfo, isLoading } = useUserInfo(); useEffect(() => { if (isLoading || !userInfo) { diff --git a/src/core/adgangsplatformen/useGetUserInfo.ts b/src/core/adgangsplatformen/useUserInfo.ts similarity index 59% rename from src/core/adgangsplatformen/useGetUserInfo.ts rename to src/core/adgangsplatformen/useUserInfo.ts index cc3221c621..1b09074e83 100644 --- a/src/core/adgangsplatformen/useGetUserInfo.ts +++ b/src/core/adgangsplatformen/useUserInfo.ts @@ -7,6 +7,7 @@ import { } from "react-query"; import { ErrorType, fetcher } from "./fetcher"; import { getToken, TOKEN_USER_KEY } from "../token"; +import { useUrls } from "../utils/url"; type UserInfoData = { attributes: { @@ -23,14 +24,6 @@ type UserInfoData = { }; }; -const getUserInfo = (url: string, signal?: AbortSignal) => { - return fetcher({ - url, - method: "get", - signal - }); -}; - const getUserInfoQueryKey = (url: string) => { const userToken = getToken(TOKEN_USER_KEY); if (!userToken) { @@ -40,27 +33,42 @@ const getUserInfoQueryKey = (url: string) => { return `${url}:${userToken}`; }; +type UserInfoFunction = () => Promise; + const useUserInfo = < - TData = Awaited>, + TData = Awaited>, TError = ErrorType >( - url: string, queryOptions?: UseQueryOptions< - Awaited>, + Awaited>, TError, TData > ): UseQueryResult & { queryKey: QueryKey } => { + const { userinfoUrl } = useUrls(); + if (!userinfoUrl) { + throw new Error("userinfoUrl is not defined"); + } + + const url = String(userinfoUrl); const queryKey = queryOptions?.queryKey ?? getUserInfoQueryKey(url); - const queryFn: QueryFunction>> = () => + const getUserInfo = (infoUrl: string, signal?: AbortSignal) => { + return fetcher({ + url: infoUrl, + method: "get", + signal + }); + }; + + const queryFn: QueryFunction>> = () => getUserInfo(url); - const query = useQuery< - Awaited>, - TError, - TData - >(queryKey, queryFn, queryOptions); + const query = useQuery>, TError, TData>( + queryKey, + queryFn, + queryOptions + ); return { queryKey,