Skip to content

Commit

Permalink
(fix) Improvements to the search experience (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen authored Mar 1, 2024
1 parent 99ec207 commit d65fe76
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 139 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "webpack --mode production",
"analyze": "webpack --mode=production --env.analyze=true",
"lint": "TIMING=1 eslint src --ext js,jsx,ts,tsx --max-warnings=0",
"prettier": "prettier --write \"src/**/*.{ts,tsx}\"",
"prettier": "prettier --write \"src/**/*.{ts,tsx}\" --list-different",
"typescript": "tsc",
"test": "jest --config jest.config.js",
"test-e2e": "playwright test",
Expand Down
67 changes: 37 additions & 30 deletions src/components/dashboard/dashboard.component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useCallback, useMemo, useState } from 'react';
import fuzzy from 'fuzzy';
import type { TFunction } from 'i18next';
import { useTranslation } from 'react-i18next';
import {
Expand Down Expand Up @@ -36,7 +35,6 @@ import {
useConfig,
useLayoutType,
usePagination,
useDebounce,
openmrsFetch,
restBaseUrl,
} from '@openmrs/esm-framework';
Expand Down Expand Up @@ -254,36 +252,27 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {
const isTablet = useLayoutType() === 'tablet';
const responsiveSize = isTablet ? 'lg' : 'sm';
const [filter, setFilter] = useState('');
const [searchTerm, setSearchTerm] = useState('');
const debouncedSearchTerm = useDebounce(searchTerm);
const [searchString, setSearchString] = useState('');

const filteredForms: Array<TypedForm> = useMemo(() => {
if (!debouncedSearchTerm) {
if (filter === 'Retired') {
return forms.filter((form) => form.retired);
}
const filteredRows = useMemo(() => {
if (!filter) {
return forms;
}

if (filter === 'Published') {
return forms.filter((form) => form.published);
}
if (filter === 'Published') {
return forms.filter((form) => form.published);
}

if (filter === 'Unpublished') {
return forms.filter((form) => !form.published);
}
if (filter === 'Unpublished') {
return forms.filter((form) => !form.published);
}

return forms;
if (filter === 'Retired') {
return forms.filter((form) => form.retired);
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return debouncedSearchTerm
? fuzzy
.filter(debouncedSearchTerm, forms, {
extract: (form: TypedForm) => `${form.name} ${form.version}`,
})
.sort((r1, r2) => r1.score - r2.score)
.map((result) => result.original as unknown as TypedForm)
: forms;
}, [filter, forms, debouncedSearchTerm]);
return forms;
}, [filter, forms]);

const tableHeaders = [
{
Expand All @@ -309,7 +298,16 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {
];

const editSchemaUrl = '${openmrsSpaBase}/form-builder/edit/${formUuid}';
const { paginated, goTo, results, currentPage } = usePagination(filteredForms, pageSize);

const searchResults = useMemo(() => {
if (searchString && searchString.trim() !== '') {
return filteredRows.filter((form) => form.name.toLowerCase().includes(searchString.toLowerCase()));
}

return filteredRows;
}, [searchString, filteredRows]);

const { paginated, goTo, results, currentPage } = usePagination(searchResults, pageSize);

const tableRows = results?.map((form: TypedForm) => ({
...form,
Expand All @@ -331,6 +329,14 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {

const handleFilter = ({ selectedItem }: { selectedItem: string }) => setFilter(selectedItem);

const handleSearch = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
goTo(1);
setSearchString(e.target.value);
},
[goTo, setSearchString],
);

return (
<>
{config.showSchemaSaveWarning && (
Expand Down Expand Up @@ -370,8 +376,9 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {
<TableToolbar className={styles.tableToolbar} size={responsiveSize}>
<TableToolbarContent className={styles.headerContainer}>
<TableToolbarSearch
expanded
className={styles.searchbox}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setSearchTerm(e.target.value)}
onChange={handleSearch}
placeholder={t('searchThisList', 'Search this list')}
/>
<Button
Expand Down Expand Up @@ -425,7 +432,7 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {
{paginated && (
<FormBuilderPagination
currentItems={results.length}
totalItems={filteredForms.length}
totalItems={searchResults.length}
onPageNumberChange={({ page }) => {
goTo(page);
}}
Expand All @@ -439,7 +446,7 @@ function FormsList({ forms, isValidating, mutate, t }: FormsListProps) {

const Dashboard: React.FC = () => {
const { t } = useTranslation();
const { error, forms, isLoading, isValidating, mutate } = useForms();
const { forms, error, isLoading, isValidating, mutate } = useForms();

return (
<main>
Expand Down
2 changes: 1 addition & 1 deletion src/components/dashboard/dashboard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
}

.filterDropdown {
margin-left: layout.$spacing-03;
margin-bottom: layout.$spacing-03;
}

.content {
Expand Down
6 changes: 3 additions & 3 deletions src/components/empty-state/empty-state.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
text-align: left;
text-transform: capitalize;
margin-bottom: layout.$spacing-05;

h4:after {
content: "";
display: block;
Expand All @@ -51,5 +51,5 @@

.tile {
text-align: center;
border: 1px solid colors.$gray-70;
}
border: 1px solid colors.$gray-20;
}
Loading

0 comments on commit d65fe76

Please sign in to comment.