Skip to content

Commit

Permalink
fix: always show latest version on search page
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerer authored and JonasKellerer committed Dec 4, 2023
1 parent 1b64518 commit aff7cd1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
30 changes: 5 additions & 25 deletions website/src/components/SearchPage/SearchForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ vi.mock('../../config', () => ({

const queryClient = new QueryClient();

const defaultMetadataSettings = [
const defaultSearchFormFilters = [
{ name: 'field1', type: 'string' as const, label: 'Field 1', autocomplete: false, filterValue: '' },
{ name: 'field2', type: 'date' as const, autocomplete: false, filterValue: '' },
{ name: 'field3', type: 'pango_lineage' as const, label: 'Field 3', autocomplete: true, filterValue: '' },
];

function renderSearchForm(
metadataSettings: Filter[] = [...defaultMetadataSettings],
searchFormFilters: Filter[] = [...defaultSearchFormFilters],
clientConfig: ClientConfig = testConfig.public,
) {
render(
<QueryClientProvider client={queryClient}>
<SearchForm organism={testOrganism} metadataSettings={metadataSettings} clientConfig={clientConfig} />
<SearchForm organism={testOrganism} filters={searchFormFilters} clientConfig={clientConfig} />
</QueryClientProvider>,
);
}
Expand Down Expand Up @@ -60,13 +60,13 @@ describe('SearchForm', () => {
await userEvent.click(searchButton);

expect(window.location.href).toBe(
routes.searchPage(testOrganism, [{ ...defaultMetadataSettings[0], filterValue }]),
routes.searchPage(testOrganism, [{ ...defaultSearchFormFilters[0], filterValue }]),
);
});

test('should not render the form with fields with flag notSearchable', async () => {
renderSearchForm([
...defaultMetadataSettings,
...defaultSearchFormFilters,
{
name: 'NotSearchable',
type: 'string' as const,
Expand All @@ -79,24 +79,4 @@ describe('SearchForm', () => {
expect(screen.getByPlaceholderText('Field 1')).toBeDefined();
expect(screen.queryByPlaceholderText('NotSearchable')).not.toBeInTheDocument();
});

test('should add default values for isLatestVersion fields to search', async () => {
renderSearchForm([
...defaultMetadataSettings,
{
name: 'isLatestVersion',
type: 'string' as const,
autocomplete: false,
filterValue: '',
notSearchable: true,
},
]);

await userEvent.type(screen.getByPlaceholderText('Field 1'), 'test');

const searchButton = screen.getByRole('button', { name: 'Search' });
await userEvent.click(searchButton);

expect(window.location.href).toContain(`isLatestVersion=true`);
});
});
15 changes: 4 additions & 11 deletions website/src/components/SearchPage/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@ const queryClient = new QueryClient();

interface SearchFormProps {
organism: string;
metadataSettings: Filter[];
filters: Filter[];
clientConfig: ClientConfig;
}

const clientLogger = getClientLogger('SearchForm');

const defaultFilters = [
{ name: 'isLatestVersion', value: 'true' },
{ name: 'isRevocation', value: 'false' },
];

export const SearchForm: FC<SearchFormProps> = ({ organism, metadataSettings, clientConfig }) => {
export const SearchForm: FC<SearchFormProps> = ({ organism, filters, clientConfig }) => {
const [fieldValues, setFieldValues] = useState<(Filter & { label: string })[]>(
metadataSettings.map((filter) => ({
filters.map((filter) => ({
...filter,
filterValue:
defaultFilters.find((defaultFilter) => filter.name === defaultFilter.name && filter.notSearchable)
?.value ?? '',
filterValue: '',
label: filter.label ?? sentenceCase(filter.name),
})),
);
Expand Down
8 changes: 4 additions & 4 deletions website/src/pages/[organism]/search/index.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { getData, getMetadataSettings } from './search';
import { getData, getSearchFormFilters } from './search';
import { Pagination } from '../../../components/SearchPage/Pagination';
import { SearchForm } from '../../../components/SearchPage/SearchForm';
import { Table } from '../../../components/SearchPage/Table';
Expand All @@ -16,13 +16,13 @@ const getSearchParams = (field: string): string => {
return Astro.url.searchParams.get(field) ?? '';
};
const metadataSettings = await getMetadataSettings(getSearchParams, organism);
const searchFormFilter = getSearchFormFilters(getSearchParams, organism);
const pageParam = Astro.url.searchParams.get('page');
const page = pageParam !== null ? Number.parseInt(pageParam, 10) : 1;
const offset = (page - 1) * pageSize;
const data = await getData(organism, metadataSettings, offset, pageSize);
const data = await getData(organism, searchFormFilter, offset, pageSize);
---

<BaseLayout title='Search'>
Expand All @@ -31,7 +31,7 @@ const data = await getData(organism, metadataSettings, offset, pageSize);
<div class='md:w-72'>
<SearchForm
organism={organism}
metadataSettings={metadataSettings}
filters={searchFormFilter}
clientConfig={clientConfig}
client:only='react'
/>
Expand Down
23 changes: 17 additions & 6 deletions website/src/pages/[organism]/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ import { Result } from 'neverthrow';
import type { TableSequenceData } from '../../../components/SearchPage/Table.tsx';
import { getSchema } from '../../../config.ts';
import { LapisClient } from '../../../services/lapisClient.ts';
import { hiddenDefaultSearchFilters } from '../../../settings.ts';
import type { ProblemDetail } from '../../../types/backend.ts';
import type { Filter } from '../../../types/config.ts';

export type SearchResponse = {
data: TableSequenceData[];
totalCount: number;
};

function addHiddenFilters(searchFormFilter: Filter[], hiddenFilters: Filter[]) {
const searchFormFilterNames = searchFormFilter.map((filter) => filter.name);
const hiddenFiltersToAdd = hiddenFilters.filter((filter) => !searchFormFilterNames.includes(filter.name));
return [...searchFormFilter, ...hiddenFiltersToAdd];
}

export const getData = async (
organism: string,
metadataFilter: Filter[],
searchFormFilter: Filter[],
offset: number,
limit: number,
hiddenDefaultFilters: Filter[] = hiddenDefaultSearchFilters,
): Promise<Result<SearchResponse, ProblemDetail>> => {
const searchFilters = metadataFilter
const filters = addHiddenFilters(searchFormFilter, hiddenDefaultFilters);

const searchFilters = filters
.filter((metadata) => metadata.filterValue !== '')
.reduce((acc: Record<string, string>, metadata) => {
acc[metadata.name] = metadata.filterValue;
Expand All @@ -43,12 +54,12 @@ export const getData = async (
});
};

export const getMetadataSettings = async (
getSearchParams: (param: string) => string,
organism: string,
): Promise<Filter[]> => {
export const getSearchFormFilters = (getSearchParams: (param: string) => string, organism: string): Filter[] => {
const schema = getSchema(organism);
return schema.metadata.flatMap((metadata) => {
if (metadata.notSearchable === true) {
return [];
}
if (metadata.type === 'date') {
const metadataFrom = {
...metadata,
Expand Down
5 changes: 5 additions & 0 deletions website/src/settings.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export const pageSize = 100;

export const hiddenDefaultSearchFilters = [
{ name: 'versionStatus', filterValue: 'LATEST_VERSION', type: 'string' as const },
{ name: 'isRevocation', filterValue: 'false', type: 'string' as const },
];
1 change: 0 additions & 1 deletion website/tests/pages/search/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test.describe('The search page', () => {
await searchPage.page.waitForURL(
`${baseUrl}${routes.searchPage(dummyOrganism.key, [
{ name: 'accessionVersion', type: 'string', filterValue: testSequence.name },
{ name: 'isRevocation', type: 'string', filterValue: 'false' },
])}`,
);
await expect(searchPage.page.getByText(testSequence.name, { exact: true })).toBeVisible();
Expand Down

0 comments on commit aff7cd1

Please sign in to comment.