Skip to content

Commit

Permalink
refactor(website): move logic to TS file, properly use neverthrow
Browse files Browse the repository at this point in the history
Refs: #1713
  • Loading branch information
fengelniederhammer committed Apr 30, 2024
1 parent f6400aa commit b846d6b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 75 deletions.
28 changes: 28 additions & 0 deletions website/src/pages/seq/[accessionVersion]/findOrganismAndData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { err, ok } from 'neverthrow';

import { getSequenceDetailsTableData, SequenceDetailsTableResultType } from './getSequenceDetailsTableData.ts';
import { getConfiguredOrganisms } from '../../../config.ts';

export async function findOrganismAndData(accessionVersion: string) {
const organisms = getConfiguredOrganisms();

const promises = organisms.map(async ({ key }) => {
return {
organism: key,
result: await getSequenceDetailsTableData(accessionVersion, key),
};
});

const queries = await Promise.all(promises);

for (const { organism, result } of queries) {
if (result.isOk()) {
return ok({
organism,
result: result.value,
});
}
}

return err({ type: SequenceDetailsTableResultType.ERROR });
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ export enum SequenceDetailsTableResultType {
ERROR = 'error',
}

type TableData = {
export type TableData = {
type: SequenceDetailsTableResultType.TABLE_DATA;
tableData: TableDataEntry[];
sequenceEntryHistory: SequenceEntryHistory;
dataUseTermsHistory: DataUseTermsHistoryEntry[];
};

type Redirect = {
export type Redirect = {
type: SequenceDetailsTableResultType.REDIRECT;
redirectUrl: string;
};

export type SequenceDetailsTableDataResult = Promise<Result<TableData | Redirect, ProblemDetail>>;

export const getSequenceDetailsTableData = async (
accessionVersion: string,
organism: string,
): Promise<Result<TableData | Redirect, ProblemDetail>> => {
): SequenceDetailsTableDataResult => {
const { accession, version } = parseAccessionVersionFromString(accessionVersion);

const lapisClient = LapisClient.createForOrganism(organism);
Expand Down
113 changes: 41 additions & 72 deletions website/src/pages/seq/[accessionVersion]/index.astro
Original file line number Diff line number Diff line change
@@ -1,106 +1,75 @@
---
import { getSequenceDetailsTableData, SequenceDetailsTableResultType } from './getSequenceDetailsTableData';
import { findOrganismAndData } from './findOrganismAndData';
import { SequenceDetailsTableResultType } from './getSequenceDetailsTableData';
import RevocationEntryDataTable from '../../../components/SequenceDetailsPage/RevocationEntryDataTable.astro';
import SequencesBanner from '../../../components/SequenceDetailsPage/SequencesBanner.astro';
import SequencesDataTableTitle from '../../../components/SequenceDetailsPage/SequencesDataTableTitle.astro';
import SequencesDataPage from '../../../components/SequenceDetailsPage/SequencesDetailsPage.astro';
import { isRevocationEntry } from '../../../components/SequenceDetailsPage/getTableData';
import ErrorBox from '../../../components/common/ErrorBox.astro';
import { getConfiguredOrganisms } from '../../../config';
import BaseLayout from '../../../layouts/BaseLayout.astro';
const accessionVersion = Astro.params.accessionVersion!;
const organisms = getConfiguredOrganisms();
let sequenceDetailsTableData;
let organism: string | undefined;
const promises = organisms.map(async ({ key }) => {
return {
organism: key,
result: await getSequenceDetailsTableData(accessionVersion, key),
};
});
const queries = await Promise.all(promises);
for (const query of queries) {
if (query.result.isOk()) {
sequenceDetailsTableData = query.result;
organism = query.organism;
break;
}
}
if (sequenceDetailsTableData === undefined) {
sequenceDetailsTableData = {
isOk: () => false,
type: SequenceDetailsTableResultType.ERROR,
};
}
const sequenceDetailsTableData = await findOrganismAndData(accessionVersion);
if (
sequenceDetailsTableData.isOk() &&
sequenceDetailsTableData.value!.type === SequenceDetailsTableResultType.REDIRECT
sequenceDetailsTableData.value.result.type === SequenceDetailsTableResultType.REDIRECT
) {
return Astro.redirect(sequenceDetailsTableData.value.redirectUrl);
return Astro.redirect(sequenceDetailsTableData.value.result.redirectUrl);
}
---

<BaseLayout
title={sequenceDetailsTableData.isOk() ? accessionVersion : 'Sequence not found'}
implicitOrganism={organism}
implicitOrganism={sequenceDetailsTableData.isOk() ? sequenceDetailsTableData.value.organism : undefined}
>
{
sequenceDetailsTableData.isOk() &&
sequenceDetailsTableData.value!.type === SequenceDetailsTableResultType.TABLE_DATA && (
sequenceDetailsTableData.value.result.type === SequenceDetailsTableResultType.TABLE_DATA && (
<div slot='banner'>
<SequencesBanner
sequenceEntryHistory={sequenceDetailsTableData.value!.sequenceEntryHistory}
sequenceEntryHistory={sequenceDetailsTableData.value.result.sequenceEntryHistory}
accessionVersion={accessionVersion}
/>
</div>
)
}
{
sequenceDetailsTableData.isOk() && (
<SequencesDataTableTitle
accessionVersion={accessionVersion}
organism={organism!}
sequenceEntryHistory={
sequenceDetailsTableData.isOk() &&
sequenceDetailsTableData.value!.type === SequenceDetailsTableResultType.TABLE_DATA
? sequenceDetailsTableData.value!.sequenceEntryHistory
: undefined
}
/>
)
}

{
sequenceDetailsTableData.isOk() &&
sequenceDetailsTableData.match!(
(data) =>
data.type === SequenceDetailsTableResultType.TABLE_DATA &&
(isRevocationEntry(data.tableData) ? (
<RevocationEntryDataTable
tableData={data.tableData}
dataUseTermsHistory={data.dataUseTermsHistory}
/>
) : (
<SequencesDataPage
tableData={data.tableData}
organism={organism!}
accessionVersion={accessionVersion}
dataUseTermsHistory={data.dataUseTermsHistory}
/>
)),
() => null,
)
}
{
!sequenceDetailsTableData.isOk() && (
<ErrorBox title='Sequence entry not found'>No data found for accession version {accessionVersion}</ErrorBox>
sequenceDetailsTableData.match(
({ organism, result }) => (
<>
<SequencesDataTableTitle
accessionVersion={accessionVersion}
organism={organism}
sequenceEntryHistory={
sequenceDetailsTableData.isOk() && result.type === SequenceDetailsTableResultType.TABLE_DATA
? result.sequenceEntryHistory
: undefined
}
/>
{result.type === SequenceDetailsTableResultType.TABLE_DATA &&
(isRevocationEntry(result.tableData) ? (
<RevocationEntryDataTable
tableData={result.tableData}
dataUseTermsHistory={result.dataUseTermsHistory}
/>
) : (
<SequencesDataPage
tableData={result.tableData}
organism={organism}
accessionVersion={accessionVersion}
dataUseTermsHistory={result.dataUseTermsHistory}
/>
))}
</>
),
() => (
<ErrorBox title='Sequence entry not found'>
No data found for accession version {accessionVersion}
</ErrorBox>
),
)
}
</BaseLayout>

0 comments on commit b846d6b

Please sign in to comment.