Skip to content

Commit

Permalink
chore: use URL from local environment variables; display record count…
Browse files Browse the repository at this point in the history
… in content page
  • Loading branch information
michaelwenk committed Jan 17, 2024
1 parent 3ba5d9e commit 56d2653
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 18 deletions.
8 changes: 6 additions & 2 deletions Dockerfile-frontend
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ COPY ./web-frontend/ .
RUN npm install

# for production mode
RUN node ./node_modules/rimraf dist && node ./node_modules/typescript/bin/tsc && node ./node_modules/vite/bin/vite.js build
RUN npm run build

ARG MB3_API_URL
RUN touch .env
RUN echo "VITE_MB3_API_URL=${MB3_API_URL}" > .env

EXPOSE 3000
CMD npm run preview
CMD npm run start
7 changes: 3 additions & 4 deletions docker/docker-compose-deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: '3.1'
version: "3.1"

services:

mongodb:
extends:
file: docker-compose-common.yaml
Expand All @@ -28,8 +27,8 @@ services:
build:
context: ..
dockerfile: Dockerfile-frontend
environment:
MB3_API_URL: "http://${MB3_API_HOST}:${MB3_API_PORT}"
args:
MB3_API_URL: "http://${MB3_API_HOST}:${MB3_API_PORT}"
ports:
- "${MB3_FRONTEND_PORT}:3000"

Expand Down
2 changes: 1 addition & 1 deletion docker/env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Server configuration
# --------------------

# The port where the API is exposed for frontend and applikation usage
# The port where the API is exposed for frontend and application usage
MB3_API_PORT=8081

# The host where the frontend is running
Expand Down
6 changes: 5 additions & 1 deletion web-frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
node_modules
npm-debug.log
dist
data
data
.env.local
.env.development.local
.env.test.local
.env.production.local
5 changes: 3 additions & 2 deletions web-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "web-frontend",
"version": "1.0.0",
"scripts": {
"start": "vite --host localhost --port 3000 --strictPort --open",
"build": "rimraf dist && tsc && vite build",
"dev": "vite --mode development --host localhost --port 3000 --strictPort --open",
"build": "rimraf dist && tsc && vite build --mode production",
"start": "vite --mode production --host 0.0.0.0 --port 3000 --strictPort",
"preview": "vite preview --host 0.0.0.0 --port 3000 -d --strictPort",
"compile": "tsc",
"clean-install": "rm package-lock.json && rm -rf node_modules && npm i",
Expand Down
12 changes: 5 additions & 7 deletions web-frontend/src/elements/routes/pages/accession/Accession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
} from 'react-router-dom';
import routes from '../../../../constants/routes';

const base = 'http://localhost:8081';

function Accession() {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
Expand All @@ -25,8 +23,8 @@ function Accession() {
const [requestedAccession, setRequestedAccession] = useState<string>('');
const [record, setRecord] = useState<Record | undefined>();

async function getRecord(base: string, id: string) {
const url = base + '/v1/records/' + id;
async function getRecord(id: string) {
const url = import.meta.env.VITE_MB3_API_URL + '/v1/records/' + id;

const resp = await fetch(url);
if (resp.ok) {
Expand All @@ -42,11 +40,11 @@ function Accession() {
}
}

const handleOnSearch = useCallback(async (base: string, id: string) => {
const handleOnSearch = useCallback(async (id: string) => {
setIsRequesting(true);
setRequestedAccession(id);

const rec: Record | undefined = await getRecord(base, id);
const rec: Record | undefined = await getRecord(id);
if (rec) {
rec.peak.peak.values = rec.peak.peak.values.map((p) => {
const _p = p;
Expand Down Expand Up @@ -82,7 +80,7 @@ function Accession() {
const id = searchParams.get('id');
if (id) {
setAccession(id);
handleOnSearch(base, id);
handleOnSearch(id);
}
}, [handleOnSearch, searchParams]);

Expand Down
40 changes: 39 additions & 1 deletion web-frontend/src/elements/routes/pages/content/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
import { useCallback, useEffect, useState } from 'react';
import Spinner from '../../../basic/Spinner';

function Content() {
const [isRequesting, setIsRequesting] = useState<boolean>(false);
const [recordCount, setRecordCount] = useState<number | undefined>();

async function getRecordCount() {
const url = import.meta.env.VITE_MB3_API_URL + '/v1/records/count';

const resp = await fetch(url);
if (resp.ok) {
return await resp.json();
} else {
return undefined;
}
}

const handleOnFetch = useCallback(async () => {
setIsRequesting(true);

const count: number | undefined = await getRecordCount();

setRecordCount(count);
setIsRequesting(false);
}, []);

useEffect(() => {
handleOnFetch();
}, [handleOnFetch]);

return (
<div>
<h2>Content View</h2>
{isRequesting ? (
<Spinner buttonDisabled buttonStyle={{ display: 'none' }} />
) : (
<h2>
{recordCount
? `MassBank has ${recordCount} entries!`
: 'Could not fetch record count!'}
</h2>
)}
</div>
);
}
Expand Down

0 comments on commit 56d2653

Please sign in to comment.