Skip to content

Commit

Permalink
Allow api url to be passed into the components instead of hardcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
utdanningnobot committed Jan 10, 2023
1 parent aa39e67 commit 0207dec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Context2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ const Context2 = ({
missingDataText = "Missing data",
tooltipStyles,
tooltipCaretStyles,
api_url = 'https://api.utdanning.no'
}) => {

const [usedLimit, setUsedLimit] = useState(limit);
const item = useData(id, direction)
const item = useData(id, direction, api_url)
const { docs } = item ? (item.mapping || {}) : {}
const dataset = useMemo(() => prepareMore(docs, usedLimit), [docs, usedLimit])

Expand Down
3 changes: 2 additions & 1 deletion src/Context3.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const Context3 = ({
colors = defaultColors,
moreLabel,
missingDataText = "Missing data",
api_url = 'https://api.utdanning.no'
}) => {

const [usedLimit, setUsedLimit] = useState(limit);
const [total, setTotal] = useState();
const item = useData(id, direction)
const item = useData(id, direction, api_url)
const { docs } = item ? (item.mapping || {}) : {}
const dataset = useMemo(() => prepareMore(docs, usedLimit), [docs, usedLimit])

Expand Down
22 changes: 4 additions & 18 deletions src/useData.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@

import { useState, useEffect } from "react"

function getEndpoint(name) {
const host = window.location.host;
let domainBase = "";
if (host.indexOf("beta") === 0 || host.indexOf("dev") === 0 || host.indexOf("localhost") === 0 ) {
domainBase = "beta.";
} else if (host.indexOf("alfa") === 0) {
domainBase = "alfa.";
}
let server = "https://" + domainBase + "api.utdanning.no";
let path = "/sammenligning/" + (name ? name : "main");
return server + path;

}

export async function getData(id, direction) {
const mainEndpoint = getEndpoint();
const endpoint = getEndpoint("arbeidsmarked");
export async function getData(id, direction, api_url = 'https://api.utdanning.no') {
const mainEndpoint = api_url + '/sammenligning/main';
const endpoint = api_url + '/sammenligning/arbeidsmarked';

const idNames = {
"uno_id2nus": "uno_id",
Expand Down Expand Up @@ -56,7 +42,7 @@ export async function getData(id, direction) {
return { main: main ? main[id] : null, mapping: mappingData }
}

export function useData(unoId = "y_sykepleier", direction = "utdanning2yrke") {
export function useData(unoId = "y_sykepleier", direction = "utdanning2yrke", api_url) {
const [item, setItem] = useState()
useEffect(() => {
getData(unoId, direction).then(data => {
Expand Down

0 comments on commit 0207dec

Please sign in to comment.