|
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"; |
8 | 8 |
|
9 | 9 | export default function Launch() {
|
| 10 | + const [error, setError] = React.useState(""); |
10 | 11 |
|
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); |
12 | 23 |
|
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); |
17 | 31 | }
|
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; |
50 | 45 |
|
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); |
55 | 49 | });
|
56 |
| - }, []); |
| 50 | + }) |
| 51 | + .catch((e) => { |
| 52 | + setError(e); |
| 53 | + console.log("launch error ", e); |
| 54 | + }); |
| 55 | + }, []); |
57 | 56 |
|
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 | + ); |
67 | 73 | }
|
68 |
| - |
|
0 commit comments