Skip to content

Commit

Permalink
Make useUserInfo aware of the userinfo url by it self
Browse files Browse the repository at this point in the history
Like we do with other query hooks.
Also renamed useGetUserInfo.ts to seUserInfo.ts - no need to use a
different filename for the hook.
  • Loading branch information
spaceo committed Nov 5, 2023
1 parent 3a62301 commit 4fc2459
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
9 changes: 3 additions & 6 deletions src/apps/create-patron-user-info/CreatePatron.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ 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<string | null>(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", {
transformer: "jsonParse"
});

// Fetch user info data.
const { data: userInfo, isLoading } = useUserInfo(String(userinfoUrl));
const { data: userInfo, isLoading } = useUserInfo();

useEffect(() => {
if (isLoading || !userInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -23,14 +24,6 @@ type UserInfoData = {
};
};

const getUserInfo = (url: string, signal?: AbortSignal) => {
return fetcher<UserInfoData>({
url,
method: "get",
signal
});
};

const getUserInfoQueryKey = (url: string) => {
const userToken = getToken(TOKEN_USER_KEY);
if (!userToken) {
Expand All @@ -40,27 +33,42 @@ const getUserInfoQueryKey = (url: string) => {
return `${url}:${userToken}`;
};

type UserInfoFunction = () => Promise<UserInfoData | undefined>;

const useUserInfo = <
TData = Awaited<ReturnType<typeof getUserInfo>>,
TData = Awaited<ReturnType<UserInfoFunction>>,
TError = ErrorType<void>
>(
url: string,
queryOptions?: UseQueryOptions<
Awaited<ReturnType<typeof getUserInfo>>,
Awaited<ReturnType<UserInfoFunction>>,
TError,
TData
>
): UseQueryResult<TData, TError> & { 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<Awaited<ReturnType<typeof getUserInfo>>> = () =>
const getUserInfo = (infoUrl: string, signal?: AbortSignal) => {
return fetcher<UserInfoData>({
url: infoUrl,
method: "get",
signal
});
};

const queryFn: QueryFunction<Awaited<ReturnType<UserInfoFunction>>> = () =>
getUserInfo(url);

const query = useQuery<
Awaited<ReturnType<typeof getUserInfo>>,
TError,
TData
>(queryKey, queryFn, queryOptions);
const query = useQuery<Awaited<ReturnType<UserInfoFunction>>, TError, TData>(
queryKey,
queryFn,
queryOptions
);

return {
queryKey,
Expand Down

0 comments on commit 4fc2459

Please sign in to comment.