Skip to content

Commit

Permalink
feat(anni): wip interface server data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Jun 21, 2022
1 parent 7dba73c commit 9b12a58
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 14 deletions.
4 changes: 3 additions & 1 deletion annotation-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"@headlessui/react": "^1.6.4",
"@heroicons/react": "^1.0.6",
"axios": "^0.27.2",
"dayjs": "^1.11.3",
"mongoose": "^6.3.5",
"next": "12.1.6",
"react": "^18.1.0",
"react-dom": "^18.1.0"
"react-dom": "^18.1.0",
"swr": "^1.3.0"
},
"devDependencies": {
"@types/node": "^17.0.31",
Expand Down
8 changes: 2 additions & 6 deletions annotation-interface/pages/api/bricks/[id].ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { NextApiHandler } from 'next';

import dbConnect from '../../../database/connect';
import TextBrick from '../../../database/models/TextBrick';

const brickApi = async (
req: NextApiRequest,
res: NextApiResponse,
): Promise<void> => {
const brickApi: NextApiHandler = async (req, res) => {
const {
query: { id },
method,
Expand Down Expand Up @@ -39,7 +36,6 @@ const brickApi = async (

default:
res.status(400).json({ success: false });
break;
}
};

Expand Down
8 changes: 2 additions & 6 deletions annotation-interface/pages/api/bricks/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { NextApiHandler } from 'next';

import dbConnect from '../../../database/connect';
import TextBrick from '../../../database/models/TextBrick';

const brickApi = async (
req: NextApiRequest,
res: NextApiResponse,
): Promise<void> => {
const brickApi: NextApiHandler = async (req, res) => {
const { method } = req;
await dbConnect();

Expand All @@ -21,7 +18,6 @@ const brickApi = async (
break;
default:
res.status(400).json({ success: false });
break;
}
};

Expand Down
39 changes: 39 additions & 0 deletions annotation-interface/pages/api/server-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import axios from 'axios';
import { NextApiHandler } from 'next';

export type FetchDate = Date | null;
export interface LastUpdatesDto {
medications: FetchDate;
guidelines: FetchDate;
}

const serverUpdateApi: NextApiHandler = async (req, res) => {
const { method } = req;
try {
switch (method) {
case 'GET':
console.log('fetching dates');
const [medicationsRes, guidelinesRes] = await Promise.all([
axios.get<FetchDate>(
`http://${process.env.AS_API}/medications/last_update`,
),
axios.get<FetchDate>(
`http://${process.env.AS_API}/guidelines/last_update`,
),
]);
const lastUpdates: LastUpdatesDto = {
medications: medicationsRes.data,
guidelines: guidelinesRes.data,
};
console.log(lastUpdates);
res.status(200).json(lastUpdates);
return;
default:
throw new Error();
}
} catch {
res.status(400).json({ success: false });
}
};

export default serverUpdateApi;
75 changes: 74 additions & 1 deletion annotation-interface/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
import { CloudDownloadIcon, ExclamationIcon } from '@heroicons/react/solid';
import axios from 'axios';
import dayjs from 'dayjs';
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
import RelativeTime from 'dayjs/plugin/relativeTime';
import useSWR from 'swr';

import PageHeading from '../components/common/PageHeading';
import WithIcon from '../components/common/WithIcon';
import { LastUpdatesDto } from './api/server-update';

dayjs.extend(RelativeTime);
dayjs.extend(LocalizedFormat);

const fetchLastUpdateDates = async (url: string) =>
await axios.get<LastUpdatesDto>(url);

const dateDisplay = (
data: Awaited<ReturnType<typeof fetchLastUpdateDates>> | undefined,
key: keyof LastUpdatesDto,
) => {
if (!data) return null;
const date = data.data[key];
if (!date) return <WithIcon icon={ExclamationIcon}>missing</WithIcon>;
return (
<p
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title={dayjs(date).format('lll')}
>
Last updated {dayjs(date).fromNow()}
</p>
);
};

const Home = () => {
return <div>TODO</div>;
const { data } = useSWR('/api/server-update', fetchLastUpdateDates);
return (
<>
<PageHeading title="PharMe's Annotation Interface">
Welcome to the curator&apos;s interface to the{' '}
<span className="italic">Annotation Server</span>: PharMe&apos;s
hub for all user-agnostic data. PharMe uses{' '}
<span className="italic">external data</span> from{' '}
<a className="underline" href="https://go.drugbank.com">
DrugBank
</a>{' '}
and{' '}
<a className="underline" href="https://cpicpgx.org">
CPIC
</a>{' '}
as well as <span className="italic">internal data</span> defined
by{' '}
<a className="underline" href="https://cpicpgx.org">
Annotations
</a>
.
</PageHeading>

<div className="pb-2 border-b border-black border-opacity-10 flex justify-between">
<h2 className="font-bold">External data status</h2>
<WithIcon as="button" icon={CloudDownloadIcon} reverse={true}>
Fetch new data
</WithIcon>
</div>
<div className="p-2 flex justify-between">
<p>DrugBank: drug names, synonyms, descriptions</p>
{dateDisplay(data, 'medications')}
</div>
<div className="p-2 flex justify-between">
<p>CPIC: PGx guidelines</p>
{dateDisplay(data, 'guidelines')}
</div>
</>
);
};

export default Home;
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,11 @@ date-fns@^2.28.0:
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.28.0.tgz#9570d656f5fc13143e50c975a3b6bbeb46cd08b2"
integrity sha512-8d35hViGYx/QH0icHYCeLmsLmMUheMmTyV9Fcm6gvNwdw31yXXH+O85sOBJ+OLnLQMKZowvpKb6FgMIQjcpvQw==

dayjs@^1.11.3:
version "1.11.3"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.3.tgz#4754eb694a624057b9ad2224b67b15d552589258"
integrity sha512-xxwlswWOlGhzgQ4TKzASQkUhqERI3egRNqgV4ScR8wlANA/A9tZ7miXa44vTTKEq5l7vWoL5G57bG3zA+Kow0A==

debug@2.6.9, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -7419,6 +7424,11 @@ swagger-ui-express@^4.2.0:
dependencies:
swagger-ui-dist ">=4.11.0"

swr@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/swr/-/swr-1.3.0.tgz#c6531866a35b4db37b38b72c45a63171faf9f4e8"
integrity sha512-dkghQrOl2ORX9HYrMDtPa7LTVHJjCTeZoB1dqTbnnEDlSvN8JEKpYIYurDfvbQFUUS8Cg8PceFVZNkW0KNNYPw==

symbol-observable@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-4.0.0.tgz#5b425f192279e87f2f9b937ac8540d1984b39205"
Expand Down

0 comments on commit 9b12a58

Please sign in to comment.