Skip to content

Commit

Permalink
Rename loginUrl to userinfoUrl
Browse files Browse the repository at this point in the history
In order to reflect what the purpose of the url actually is.
Also use useEffect which is recommended best practise when fetching data
in React components.
  • Loading branch information
spaceo committed Oct 6, 2023
1 parent 3ec563a commit 7a69b06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/apps/create-patron-user-info/CreatePatron.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
'[\n {\n "branchId":"DK-775120",\n "title":"Højbjerg"\n },\n {\n "branchId":"DK-775122",\n "title":"Beder-Malling"\n },\n {\n "branchId":"DK-775144",\n "title":"Gellerup"\n },\n {\n "branchId":"DK-775167",\n "title":"Lystrup"\n },\n {\n "branchId":"DK-775146",\n "title":"Harlev"\n },\n {\n "branchId":"DK-775168",\n "title":"Skødstrup"\n },\n {\n "branchId":"FBS-751010",\n "title":"Arresten"\n },\n {\n "branchId":"DK-775147",\n "title":"Hasle"\n },\n {\n "branchId":"FBS-751032",\n "title":"Må ikke benyttes"\n },\n {\n "branchId":"FBS-751031",\n "title":"Fjernlager 1"\n },\n {\n "branchId":"DK-775126",\n "title":"Solbjerg"\n },\n {\n "branchId":"FBS-751030",\n "title":"ITK"\n },\n {\n "branchId":"DK-775149",\n "title":"Sabro"\n },\n {\n "branchId":"DK-775127",\n "title":"Tranbjerg"\n },\n {\n "branchId":"DK-775160",\n "title":"Risskov"\n },\n {\n "branchId":"DK-775162",\n "title":"Hjortshøj"\n },\n {\n "branchId":"DK-775140",\n "title":"Åby"\n },\n {\n "branchId":"FBS-751009",\n "title":"Fjernlager 2"\n },\n {\n "branchId":"FBS-751029",\n "title":"Stadsarkivet"\n },\n {\n "branchId":"FBS-751027",\n "title":"Intern"\n },\n {\n "branchId":"FBS-751026",\n "title":"Fælles undervejs"\n },\n {\n "branchId":"FBS-751025",\n "title":"Fællessekretariatet"\n },\n {\n "branchId":"DK-775133",\n "title":"Bavnehøj"\n },\n {\n "branchId":"FBS-751024",\n "title":"Fjernlånte materialer"\n },\n {\n "branchId":"DK-775100",\n "title":"Hovedbiblioteket"\n },\n {\n "branchId":"DK-775170",\n "title":"Trige"\n },\n {\n "branchId":"DK-775150",\n "title":"Tilst"\n },\n {\n "branchId":"DK-775130",\n "title":"Viby"\n },\n {\n "branchId":"DK-775164",\n "title":"Egå"\n }\n]',
control: { type: "text" }
},
loginUrl: {
userinfoUrl: {
defaultValue: "https://login.bib.dk/userinfo",
control: { type: "text" }
},
Expand Down
30 changes: 18 additions & 12 deletions src/apps/create-patron-user-info/CreatePatron.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, FC } from "react";
import React, { useState, FC, useEffect } from "react";
import UserInfo from "./UserInfo";
import { useUrls } from "../../core/utils/url";

Expand All @@ -8,18 +8,24 @@ interface CreatePatronProps {

const CreatePatron: FC<CreatePatronProps> = ({ userToken }) => {
const [cpr, setCpr] = useState<string | null>(null);
const { loginUrl } = useUrls();
const { userinfoUrl } = useUrls();

fetch(String(loginUrl), {
method: "get",
headers: { Authorization: `Bearer ${userToken}` }
})
.then((response) => response.json())
.then((data) => {
if (data?.attributes?.cpr) {
setCpr(data.attributes.cpr);
}
});
if (!userinfoUrl) {
throw new Error("userinfoUrl is not defined");
}

useEffect(() => {
fetch(String(userinfoUrl), {
method: "get",
headers: { Authorization: `Bearer ${userToken}` }
})
.then((response) => response.json())
.then((data) => {
if (data?.attributes?.cpr) {
setCpr(data.attributes.cpr);
}
});
}, [userToken, userinfoUrl]);

if (cpr === null) return null;

Expand Down

0 comments on commit 7a69b06

Please sign in to comment.