Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update env #702

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @suzil
* @arunavabasu-03
1 change: 1 addition & 0 deletions frontend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_ENDPOINT = "your api endpoint"
5 changes: 3 additions & 2 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand All @@ -22,5 +23,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

# RADIS App logo type declaration
!./src/.radis.d.ts
# type declaration
!*.d.ts
237 changes: 118 additions & 119 deletions frontend/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,135 +136,134 @@ export const Form: React.FunctionComponent<FormProps> = ({
setLoading(true);
setError(undefined);

import(/* webpackIgnore: true */ "./config.js").then(async (module) => {
if (endpoint === "calculate-spectrum") {
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "calculate",
action: "click_calculate",
label: "calculate_spectrum",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
setProgress(30);
const apiEndpoint = import.meta.env.VITE_API_ENDPOINT;
if (endpoint === "calculate-spectrum") {
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "calculate",
action: "click_calculate",
label: "calculate_spectrum",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
setProgress(30);

const rawResponse = await axios({
url: module.apiEndpoint + `calculate-spectrum`,
method: "POST",
data: data,
headers: {
"Content-Type": "application/json",
},
});
if (
rawResponse.data.data === undefined &&
rawResponse.data.error === undefined
) {
handleBadResponse("Bad response from backend!");
const rawResponse = await axios({
url: apiEndpoint + `calculate-spectrum`,
method: "POST",
data: data,
headers: {
"Content-Type": "application/json",
},
});
if (
rawResponse.data.data === undefined &&
rawResponse.data.error === undefined
) {
handleBadResponse("Bad response from backend!");
setDisableDownloadButton(true);
} else {
const response = await rawResponse.data;
if (response.error) {
handleBadResponse(response.error);
setDisableDownloadButton(true);
} else {
const response = await rawResponse.data;
if (response.error) {
handleBadResponse(response.error);
setDisableDownloadButton(true);
} else {
setSpectra([
...(appendSpectrum ? spectra : []),
{
species: data.species.map((s) => ({ ...s })),
database: data.database,
tgas: data.tgas,
trot: data.trot,
tvib: data.tvib,
pressure: data.pressure,
pressure_units: data.pressure_units,
wavelength_units: data.wavelength_units,
...response.data,
},
]);
setDisableAddToPlotButton(false);
setPlotSettings({
mode: data.mode,
units: data.mode.startsWith("absorbance")
? "-ln(I/I0)"
: response.data.units,
});
setDisableDownloadButton(false);
}
setSpectra([
...(appendSpectrum ? spectra : []),
{
species: data.species.map((s) => ({ ...s })),
database: data.database,
tgas: data.tgas,
trot: data.trot,
tvib: data.tvib,
pressure: data.pressure,
pressure_units: data.pressure_units,
wavelength_units: data.wavelength_units,
...response.data,
},
]);
setDisableAddToPlotButton(false);
setPlotSettings({
mode: data.mode,
units: data.mode.startsWith("absorbance")
? "-ln(I/I0)"
: response.data.units,
});
setDisableDownloadButton(false);
}

setProgress(100);
setLoading(false);
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "calculate",
action: "click_calculate_successful",
label: "calculate_spectrum_successful",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
}

if (endpoint === "download-spectrum" || endpoint === "download-txt") {
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "file_download",
action: "click_download",
label: "download_spectrum_file",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
setProgress(30);
setLoading(false);
let serverFullUrl: string;
if (endpoint === "download-spectrum") {
serverFullUrl = module.apiEndpoint + `download-spectrum`;
} else {
serverFullUrl = module.apiEndpoint + `download-txt`;
}
const rawResponse = await axios({
url: serverFullUrl,
method: "POST",
responseType: "blob",
data: data,
headers: {
"Content-Type": "application/json",
},
});
setProgress(100);
setLoading(false);
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "calculate",
action: "click_calculate_successful",
label: "calculate_spectrum_successful",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
}

const url = window.URL.createObjectURL(new Blob([rawResponse.data]));
const link = document.createElement("a");
link.href = url;
if (endpoint === "download-spectrum") {
link.setAttribute(
"download",
`${data.database}_${molecules}_${data.min_wavenumber_range}_${data.max_wavenumber_range}cm-1_${data.tgas}K_${data.pressure}atm.spec`
);
}
if (endpoint === "download-txt") {
link.setAttribute(
"download",
`${data.database}_${molecules}_${data.min_wavenumber_range}_${data.max_wavenumber_range}cm-1_${data.tgas}K_${data.pressure}atm.csv`
);
}
if (endpoint === "download-spectrum" || endpoint === "download-txt") {
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "file_download",
action: "click_download",
label: "download_spectrum_file",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
setProgress(30);
setLoading(false);
let serverFullUrl: string;
if (endpoint === "download-spectrum") {
serverFullUrl = apiEndpoint + `download-spectrum`;
} else {
serverFullUrl = apiEndpoint + `download-txt`;
}
const rawResponse = await axios({
url: serverFullUrl,
method: "POST",
responseType: "blob",
data: data,
headers: {
"Content-Type": "application/json",
},
});

document.body.appendChild(link);
link.click();
setDisableDownloadButton(false);
const response = await rawResponse.data;
if (response.error) {
handleBadResponse(response.error);
} else {
setDisableDownloadButton(false);
}
const url = window.URL.createObjectURL(new Blob([rawResponse.data]));
const link = document.createElement("a");
link.href = url;
if (endpoint === "download-spectrum") {
link.setAttribute(
"download",
`${data.database}_${molecules}_${data.min_wavenumber_range}_${data.max_wavenumber_range}cm-1_${data.tgas}K_${data.pressure}atm.spec`
);
}
if (endpoint === "download-txt") {
link.setAttribute(
"download",
`${data.database}_${molecules}_${data.min_wavenumber_range}_${data.max_wavenumber_range}cm-1_${data.tgas}K_${data.pressure}atm.csv`
);
}

document.body.appendChild(link);
link.click();
setDisableDownloadButton(false);
const response = await rawResponse.data;
if (response.error) {
handleBadResponse(response.error);
} else {
setDisableDownloadButton(false);
setProgress(100);
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "file_download",
action: "click_download_successful",
label: "download_spectrum_file_successful",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
}
});
setDisableDownloadButton(false);
setProgress(100);
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
ReactGA.event({
category: "file_download",
action: "click_download_successful",
label: "download_spectrum_file_successful",
});
/*#########GOOGLE_ANALYTICS_EVENT_TRACKING###############*/
}
};

useEffect(() => {
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/components/config.js

This file was deleted.

9 changes: 9 additions & 0 deletions frontend/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_API_ENDPOINT: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}
3 changes: 2 additions & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"lib": [
"dom",
"dom.iterable",
"esnext"
"esnext",
"WebWorker"
],
"allowJs": true,
"skipLibCheck": true,
Expand Down