Skip to content

Commit 75daabf

Browse files
committed
feat: implement a useEffect for checking searchString, remove group id from search
1 parent 1b8d8f2 commit 75daabf

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

apps/web/src/routes/_app/datahub/index.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import React, { useState } from 'react';
2-
1+
import React from 'react';
2+
import { useEffect, useState } from 'react';
33
import { toBasicISOString } from '@douglasneuroinformatics/libjs';
44
import {
55
ActionDropdown,
66
Button,
7-
Checkbox,
87
ClientTable,
98
Dialog,
109
Heading,
11-
Label,
1210
SearchBar
1311
} from '@douglasneuroinformatics/libui/components';
1412
import { useDownload, useNotificationsStore, useTranslation } from '@douglasneuroinformatics/libui/hooks';
@@ -81,7 +79,7 @@ const RouteComponent = () => {
8179

8280
const { data } = useSubjectsQuery({ params: { groupId: currentGroup?.id } });
8381
const [tableData, setTableData] = useState<Subject[]>(data);
84-
const [isLookUpSearch, setLookUpSearch] = useState(true);
82+
const [searchString, setSearchString] = useState('');
8583

8684
const getExportRecords = async () => {
8785
const response = await axios.get<InstrumentRecordsExport>('/v1/instrument-records/export', {
@@ -136,11 +134,6 @@ const RouteComponent = () => {
136134
};
137135

138136
const lookupSubject = async ({ id }: { id: string }) => {
139-
if (!isLookUpSearch) {
140-
setSubjectTable({ id: id });
141-
return;
142-
}
143-
144137
const response = await axios.get<Subject>(`/v1/subjects/${id}`, {
145138
validateStatus: (status) => status === 200 || status === 404
146139
});
@@ -153,23 +146,21 @@ const RouteComponent = () => {
153146
}
154147
};
155148

156-
const setSubjectTable = ({ id }: { id: string }) => {
157-
const newSubjects = data.map((record) => {
158-
if (record.id.includes(id)) {
159-
return record;
160-
}
149+
useEffect(() => {
150+
if (!searchString) {
151+
setTableData(data);
161152
return;
162-
});
153+
}
163154

164-
const filteredSubjects = newSubjects.filter((record) => record !== undefined);
155+
const filtered = data.filter((record) =>
156+
record.id
157+
.replace(/^.*?\$/, '')
158+
.toLowerCase()
159+
.includes(searchString.toLowerCase())
160+
);
165161

166-
if (newSubjects.length < 1) {
167-
addNotification({ message: t('core.notFound'), type: 'warning' });
168-
} else {
169-
addNotification({ type: 'success' });
170-
setTableData(filteredSubjects);
171-
}
172-
};
162+
setTableData(filtered);
163+
}, [searchString, data]);
173164

174165
return (
175166
<React.Fragment>
@@ -189,6 +180,8 @@ const RouteComponent = () => {
189180
fr: 'Cliquer pour rechercher'
190181
})}
191182
readOnly={false}
183+
value={searchString}
184+
onValueChange={(value: string) => setSearchString(value)}
192185
/>
193186
<Dialog open={isLookupOpen} onOpenChange={setIsLookupOpen}>
194187
<Dialog.Trigger>

0 commit comments

Comments
 (0)