Skip to content

Commit

Permalink
ui: connect filters
Browse files Browse the repository at this point in the history
  • Loading branch information
karolina-siemieniuk-morawska committed Aug 15, 2024
1 parent d11c040 commit c0596c1
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 387 deletions.
10 changes: 8 additions & 2 deletions ui/src/actions/holdingpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ function resetQuery() {
};
}

type QueryParams = { page: number; size: number; [key: string]: any };
type QueryParams = {
page: number;
size?: number;
ordering?: string;
[key: string]: any;
};
export function searchQueryUpdate(
query: QueryParams
): (dispatch: ActionCreator<Action>) => Promise<void> {
Expand All @@ -208,7 +213,8 @@ export function fetchSearchResults(): (
const currentQuery = getState()?.holdingpen?.get('query')?.toJS() || {};
const resolveQuery = `${BACKOFFICE_SEARCH_API}/?${
Object.entries(currentQuery)
?.map(([key, value]: [string, any]) => `${key}=${value}`)
.filter(([_, value]) => value != null)
.map(([key, value]: [string, any]) => `${key}=${value}`)
.join('&') || ''
}`;

Expand Down
4 changes: 2 additions & 2 deletions ui/src/common/components/Logo/LogoHoldingpen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React from 'react';
import { Link } from 'react-router-dom';

import './Logo.less';
import { HOLDINGPEN } from '../../routes';
import { HOLDINGPEN_NEW } from '../../routes';
import { ReactComponent as LogoSvg } from './logo-holdingpen.svg';

const LogoHoldingpen = () => (
<Link className="__Logo__" to={HOLDINGPEN}>
<Link className="__Logo__" to={HOLDINGPEN_NEW}>
<LogoSvg className="logo" />
</Link>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exports[`Holdingpen renders initial state 1`] = `
class="ant-breadcrumb-link"
>
<a
href="/holdingpen-new/dashboard"
href="/holdingpen-new/"
>
Dashboard
</a>
Expand Down Expand Up @@ -328,7 +328,7 @@ exports[`Holdingpen renders initial state 1`] = `
</p>
<a
class="normal f6"
href="/holdingpen-new/search/?workflow_name=AUTHOR_CREATE"
href="/holdingpen-new/search"
>
View all
</a>
Expand Down Expand Up @@ -364,7 +364,7 @@ exports[`Holdingpen renders initial state 1`] = `
</p>
<a
class="normal f6"
href="/holdingpen-new/search/?workflow_name=AUTHOR_UPDATE"
href="/holdingpen-new/search"
>
View all
</a>
Expand Down Expand Up @@ -400,7 +400,7 @@ exports[`Holdingpen renders initial state 1`] = `
</p>
<a
class="normal f6"
href="/holdingpen-new/search/?workflow_name=HEP_CREATE"
href="/holdingpen-new/search"
>
View all
</a>
Expand Down
182 changes: 182 additions & 0 deletions ui/src/holdingpen-new/components/SearchFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import React, { useState } from 'react';
import { Col, Card, Select, Row, Checkbox } from 'antd';
import { connect, RootStateOrAny } from 'react-redux';
import { Action, ActionCreator } from 'redux';
import { Map } from 'immutable';
import classNames from 'classnames';

import LoadingOrChildren from '../../common/components/LoadingOrChildren';
import UnclickableTag from '../../common/components/UnclickableTag';
import { searchQueryReset, searchQueryUpdate } from '../../actions/holdingpen';
import { COLLECTIONS, getIcon } from '../utils/utils';
import LinkLikeButton from '../../common/components/LinkLikeButton/LinkLikeButton';

interface SearchFiltersProps {
dispatch: ActionCreator<Action>;
loading: boolean;
query: Map<string, any>;
facets: any;
count: any;
}

const FilterOption: React.FC<{
label: string;
options: Map<string, any>;
selectedKey: string | null;
onSelect: (key: string | null) => void;
renderLabel: (key: string) => React.ReactNode;
}> = ({ label, options, selectedKey, onSelect, renderLabel }) => {
if (!options?.size) return null;

return (
<>
<Row>
<p className="facet-category mt3">{label}</p>
</Row>
{options.map((option: Map<string, any>) => {
const key = option.get('key');
const isChecked = selectedKey === key;
return (
<Row className="mb2" justify="space-between" key={key}>
<Col>
<Checkbox
checked={isChecked}
onChange={() => onSelect(isChecked ? null : key)}
>
{renderLabel(key)}
</Checkbox>
</Col>
<Col>
<UnclickableTag>{option.get('doc_count')}</UnclickableTag>
</Col>
</Row>
);
})}
</>
);
};

const SearchFilters: React.FC<SearchFiltersProps> = ({
dispatch,
loading,
query,
facets,
count,
}) => {
const [selectedFilters, setSelectedFilters] = useState({
status: query?.get('status'),
workflow_type: query?.get('workflow_type'),
});

const updateFilters = (key: string, value: string | null) => {
setSelectedFilters((prevFilters) => ({ ...prevFilters, [key]: value }));
dispatch(
searchQueryUpdate({
...query.toJS(),
page: 1,
[key]: value,
})
);
};

return (
<Col xs={0} lg={5}>
<LoadingOrChildren loading={loading}>
<Card>
<p className="facet-category">Results per page</p>
<Select
defaultValue="10"
value={query.get('size')}
style={{ width: '100%', marginBottom: '1rem' }}
options={[
{ value: 10, label: '10' },
{ value: 25, label: '25' },
{ value: 50, label: '50' },
{ value: 100, label: '100' },
]}
onChange={(value: number) =>
dispatch(
searchQueryUpdate({
page: 1,
size: value,
ordering: query.get('ordering'),
})
)
}
/>

<p className="facet-category mt3">Sort by</p>
<Select
defaultValue="-_updated_at"
value={query.get('ordering')}
style={{ width: '100%', marginBottom: '1rem' }}
options={[
{ value: '-_updated_at', label: 'Most recent' },
{ value: '_updated_at', label: 'Least recent' },
]}
onChange={(value: string) =>
dispatch(
searchQueryUpdate({
page: 1,
size: query.get('size'),
ordering: value,
})
)
}
/>

{count > 0 && (
<>
<FilterOption
label="Filter by collection"
options={facets
?.getIn(['_filter_workflow_type', 'workflow_type'])
?.get('buckets')}
selectedKey={selectedFilters.workflow_type}
onSelect={(key) => updateFilters('workflow_type', key)}
renderLabel={(key) => (
<span className="ttc">{COLLECTIONS[key]}</span>
)}
/>

<FilterOption
label="Filter by status"
options={facets
?.getIn(['_filter_status', 'status'])
?.get('buckets')}
selectedKey={selectedFilters.status}
onSelect={(key) => updateFilters('status', key)}
renderLabel={(key) => (
<span className={classNames(key?.toLowerCase(), 'ttc')}>
{getIcon(key)}
{key}
</span>
)}
/>
</>
)}

<Row justify="center" className="mt4">
<LinkLikeButton
onClick={() => {
setSelectedFilters({ status: null, workflow_type: null });
dispatch(searchQueryReset());
}}
>
Reset filters
</LinkLikeButton>
</Row>
</Card>
</LoadingOrChildren>
</Col>
);
};

const mapStateToProps = (state: RootStateOrAny) => ({
loading: state.holdingpen.get('loading'),
query: state.holdingpen.get('query'),
facets: state.holdingpen.get('facets'),
count: state.holdingpen.get('totalResults'),
});

export default connect(mapStateToProps)(SearchFilters);
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
background-color: #f5f5f5;
font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;
}
.bg-running {
background-color: #c2c5c7;
}

.font-white {
color: white !important;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/* eslint-disable react-hooks/exhaustive-deps */
import React, { useEffect } from 'react';
import classNames from 'classnames';
import {
WarningOutlined,
CheckOutlined,
HourglassOutlined,
LoadingOutlined,
} from '@ant-design/icons';
import { Card, Input, Select, Tabs } from 'antd';
import { Link } from 'react-router-dom';
import { push } from 'connected-react-router';
Expand All @@ -24,6 +18,7 @@ import {
} from '../../../actions/holdingpen';
import EmptyOrChildren from '../../../common/components/EmptyOrChildren';
import LoadingOrChildren from '../../../common/components/LoadingOrChildren';
import { COLLECTIONS, getIcon } from '../../utils/utils';

interface DashboardPageContainerProps {
dispatch: ActionCreator<Action>;
Expand All @@ -35,12 +30,6 @@ const TEXT_CENTER: Record<string | number, string & {}> = {
textAlign: 'center',
};

const COLLECTIONS: Record<string, string> = {
AUTHOR_CREATE: 'new authors',
AUTHOR_UPDATE: 'author updates',
HEP_CREATE: 'new literature submissions',
};

const { Option } = Select;

const { Search } = Input;
Expand All @@ -60,21 +49,6 @@ const selectBefore = (
</Select>
);

const getIcon = (status: string) => {
switch (status?.toLowerCase()) {
case 'approval':
return <HourglassOutlined className="mr2" />;
case 'error':
return <WarningOutlined className="mr2" />;
case 'completed':
return <CheckOutlined className="mr2" />;
case 'running':
return <LoadingOutlined className="mr2" />;
default:
return null;
}
};

const DashboardPageContainer: React.FC<DashboardPageContainerProps> = ({
dispatch,
facets,
Expand Down Expand Up @@ -117,16 +91,14 @@ const DashboardPageContainer: React.FC<DashboardPageContainerProps> = ({
{type?.get('doc_count')}
</p>
<Link
to={`${HOLDINGPEN_SEARCH_NEW}/?workflow_name=${type?.get(
'key'
)}`}
to={HOLDINGPEN_SEARCH_NEW}
className="normal f6"
onClick={() => {
dispatch(
searchQueryUpdate({
page: 1,
size: 10,
workflow_name: type?.get('key'),
workflow_type: type?.get('key'),
})
);
}}
Expand All @@ -144,16 +116,14 @@ const DashboardPageContainer: React.FC<DashboardPageContainerProps> = ({
{(type?.getIn(['status', 'buckets']) as List<any>)?.map(
(status: any) => (
<a
href={`${HOLDINGPEN_SEARCH_NEW}/?workflow_name=${type?.get(
'key'
)}&status=${status?.get('key')}`}
href={HOLDINGPEN_SEARCH_NEW}
key={status?.get('key')}
onClick={() => {
dispatch(
searchQueryUpdate({
page: 1,
size: 10,
workflow_name: type?.get('key'),
workflow_type: type?.get('key'),
status: status?.get('key'),
})
);
Expand Down Expand Up @@ -188,7 +158,7 @@ const DashboardPageContainer: React.FC<DashboardPageContainerProps> = ({
className="__DashboardPageContainer__"
data-testid="holdingpen-dashboard-page"
>
<Breadcrumbs title1="Dashboard" href1="dashboard" dashboardPage />
<Breadcrumbs title1="Dashboard" href1="" dashboardPage />
<div className="inner-container mt4">
<h2 className="f2 center">Search Holdingpen</h2>
<div className="search-container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@

.facet-category {
font-size: 1rem;
margin-top: 1rem;
font-weight: 600;
}

.ant-tag {
margin-right: 0;
}
}

.waiting,
.running {
color: #7b898a;
}
.error {
color: #e74c3c;
}
.approval {
color: #cfa90e;
}
.completed {
color: #16a085;
}
Loading

0 comments on commit c0596c1

Please sign in to comment.