Skip to content

Commit ac8f93b

Browse files
Amy ChenAmy Chen
authored andcommitted
fix launch code
1 parent c01de6d commit ac8f93b

File tree

2 files changed

+69
-59
lines changed

2 files changed

+69
-59
lines changed

src/components/Launch.js

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,73 @@
1-
import React from 'react';
2-
import FHIR from 'fhirclient';
3-
import Box from '@mui/material/Box';
4-
import CircularProgress from '@mui/material/CircularProgress';
5-
import Error from './Error';
6-
import {queryPatientIdKey} from '../util/util.js';
7-
import '../style/App.scss';
1+
import React from "react";
2+
import FHIR from "fhirclient";
3+
import Stack from "@mui/material/Stack";
4+
import CircularProgress from "@mui/material/CircularProgress";
5+
import Error from "./Error";
6+
import { getEnv, queryPatientIdKey } from "../util/util.js";
7+
import "../style/App.scss";
88

99
export default function Launch() {
10+
const [error, setError] = React.useState("");
1011

11-
const [error, setError] = React.useState('');
12+
React.useEffect(() => {
13+
let authURL = "launch-context.json";
14+
const backendURL = getEnv("REACT_APP_BACKEND_URL");
15+
if (backendURL) {
16+
authURL = `${backendURL}/auth/auth-info`;
17+
}
18+
const urlParams = new URLSearchParams(window.location.search);
19+
//retrieve patient id from URL querystring if any
20+
let patientId = urlParams.get("patient");
21+
console.log("patient id from url query string: ", patientId);
22+
console.log("Auth url ", authURL);
1223

13-
React.useEffect(() => {
14-
let authURL = 'launch-context.json';
15-
if (process.env.REACT_APP_BACKEND_URL) {
16-
authURL = `${process.env.REACT_APP_BACKEND_URL}/auth/auth-info`;
24+
fetch(authURL, {
25+
// include cookies in request
26+
credentials: "include",
27+
})
28+
.then((result) => {
29+
if (!result.ok) {
30+
throw Error(result.status);
1731
}
18-
const urlParams = new URLSearchParams(window.location.search);
19-
//retrieve patient id from URL querystring if any
20-
let patientId = urlParams.get('patient');
21-
console.log("patient id from url query string: ", patientId);
22-
console.log("authURL: ", authURL);
23-
24-
fetch(authURL, {
25-
// include cookies in request
26-
credentials: 'include'
27-
})
28-
.then(result => {
29-
if (!result.ok) {
30-
throw Error(result.status);
31-
}
32-
return result.json();
33-
})
34-
.catch(e => setError(e))
35-
.then(json => {
36-
if (patientId) {
37-
//only do this IF patient id comes from url queryString
38-
json.patientId = patientId;
39-
sessionStorage.setItem(queryPatientIdKey, patientId);
40-
}
41-
//allow auth scopes to be updated via environment variable
42-
//see https://build.fhir.org/ig/HL7/smart-app-launch/scopes-and-launch-context.html
43-
const envAuthScopes = process.env.REACT_APP_AUTH_SCOPES;
44-
if (envAuthScopes) json.scope = envAuthScopes;
45-
46-
console.log("launch context json ", json);
47-
FHIR.oauth2.authorize(json).catch((e) => {
48-
setError(e);
49-
});
32+
return result.json();
33+
})
34+
.catch((e) => setError(e))
35+
.then((json) => {
36+
if (patientId) {
37+
//only do this IF patient id comes from url queryString
38+
json.patientId = patientId;
39+
sessionStorage.setItem(queryPatientIdKey, patientId);
40+
}
41+
//allow auth scopes to be updated via environment variable
42+
//see https://build.fhir.org/ig/HL7/smart-app-launch/scopes-and-launch-context.html
43+
const envAuthScopes = getEnv("REACT_APP_AUTH_SCOPES");
44+
if (envAuthScopes) json.scope = envAuthScopes;
5045

51-
})
52-
.catch(e => {
53-
setError(e);
54-
console.log('launch error ', e);
46+
console.log("launch context json ", json);
47+
FHIR.oauth2.authorize(json).catch((e) => {
48+
setError(e);
5549
});
56-
}, []);
50+
})
51+
.catch((e) => {
52+
setError(e);
53+
console.log("launch error ", e);
54+
});
55+
}, []);
5756

58-
return (
59-
<React.Fragment>
60-
{error && <Error message={error.message}></Error>}
61-
{!error && <Box style={{ padding: "1rem" }}>
62-
<CircularProgress></CircularProgress>
63-
<span>Launching ...</span>
64-
</Box>}
65-
</React.Fragment>
66-
);
57+
return (
58+
<React.Fragment>
59+
{error && <Error message={error.message}></Error>}
60+
{!error && (
61+
<Stack
62+
spacing={2}
63+
direction="row"
64+
style={{ padding: "24px" }}
65+
alignItems="center"
66+
>
67+
<CircularProgress></CircularProgress>
68+
<div>Launching ...</div>
69+
</Stack>
70+
)}
71+
</React.Fragment>
72+
);
6773
}
68-

src/util/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ export function getFHIRResourcePaths(patientId) {
2323
});
2424
}
2525

26+
export const getEnv = (key) => {
27+
if (!process || !process.env) return "";
28+
return process.env[key];
29+
};
30+
2631
export const queryPatientIdKey = 'launch_queryPatientId';

0 commit comments

Comments
 (0)