11import React , { useState } from 'react' ;
22
33import { toBasicISOString } from '@douglasneuroinformatics/libjs' ;
4- import { ActionDropdown , ClientTable , Dialog , Heading , SearchBar } from '@douglasneuroinformatics/libui/components' ;
4+ import {
5+ ActionDropdown ,
6+ Checkbox ,
7+ ClientTable ,
8+ Dialog ,
9+ Heading ,
10+ SearchBar
11+ } from '@douglasneuroinformatics/libui/components' ;
512import { useDownload , useNotificationsStore , useTranslation } from '@douglasneuroinformatics/libui/hooks' ;
613import type { InstrumentRecordsExport } from '@opendatacapture/schemas/instrument-records' ;
714import type { Subject } from '@opendatacapture/schemas/subject' ;
@@ -71,6 +78,8 @@ const RouteComponent = () => {
7178 const navigate = useNavigate ( ) ;
7279
7380 const { data } = useSubjectsQuery ( { params : { groupId : currentGroup ?. id } } ) ;
81+ const [ tableData , setTableData ] = useState < Subject [ ] > ( data ) ;
82+ const [ isLookUpSearch , setLookUpSearch ] = useState ( true ) ;
7483
7584 const getExportRecords = async ( ) => {
7685 const response = await axios . get < InstrumentRecordsExport > ( '/v1/instrument-records/export' , {
@@ -92,15 +101,21 @@ const RouteComponent = () => {
92101 } ) ;
93102 getExportRecords ( )
94103 . then ( ( data ) : any => {
104+ const listedSubjects = tableData . map ( ( record ) => {
105+ return record . id . replace ( 'root$' , '' ) ;
106+ } ) ;
107+
108+ const filteredData = data . filter ( ( dataEntry ) => listedSubjects . includes ( dataEntry . subjectId ) ) ;
109+
95110 switch ( option ) {
96111 case 'CSV' :
97112 void download ( 'README.txt' , t ( 'datahub.index.table.exportHelpText' ) ) ;
98- void download ( `${ baseFilename } .csv` , unparse ( data ) ) ;
113+ void download ( `${ baseFilename } .csv` , unparse ( filteredData ) ) ;
99114 break ;
100115 case 'Excel' :
101- return downloadExcel ( `${ baseFilename } .xlsx` , data ) ;
116+ return downloadExcel ( `${ baseFilename } .xlsx` , filteredData ) ;
102117 case 'JSON' :
103- return download ( `${ baseFilename } .json` , JSON . stringify ( data , null , 2 ) ) ;
118+ return download ( `${ baseFilename } .json` , JSON . stringify ( filteredData , null , 2 ) ) ;
104119 }
105120 } )
106121 . then ( ( ) => {
@@ -119,6 +134,11 @@ const RouteComponent = () => {
119134 } ;
120135
121136 const lookupSubject = async ( { id } : { id : string } ) => {
137+ if ( ! isLookUpSearch ) {
138+ setSubjectTable ( { id : id } ) ;
139+ return ;
140+ }
141+
122142 const response = await axios . get < Subject > ( `/v1/subjects/${ id } ` , {
123143 validateStatus : ( status ) => status === 200 || status === 404
124144 } ) ;
@@ -131,6 +151,24 @@ const RouteComponent = () => {
131151 }
132152 } ;
133153
154+ const setSubjectTable = ( { id } : { id : string } ) => {
155+ const newSubjects = data . map ( ( record ) => {
156+ if ( record . id . includes ( id ) ) {
157+ return record ;
158+ }
159+ return ;
160+ } ) ;
161+
162+ const filteredSubjects = newSubjects . filter ( ( record ) => record !== undefined ) ;
163+
164+ if ( newSubjects . length < 1 ) {
165+ addNotification ( { message : t ( 'core.notFound' ) , type : 'warning' } ) ;
166+ } else {
167+ addNotification ( { type : 'success' } ) ;
168+ setTableData ( filteredSubjects ) ;
169+ }
170+ } ;
171+
134172 return (
135173 < React . Fragment >
136174 < PageHeader >
@@ -170,9 +208,14 @@ const RouteComponent = () => {
170208 onSelection = { handleExportSelection }
171209 />
172210 </ div >
211+ < div className = "flex min-w-60 gap-2 lg:shrink" >
212+ < Checkbox id = "Datahub table search mode" onCheckedChange = { ( ) => setLookUpSearch ( ! isLookUpSearch ) } >
213+ Table Search Mode
214+ </ Checkbox >
215+ </ div >
173216 </ div >
174217 < MasterDataTable
175- data = { data }
218+ data = { tableData }
176219 onSelect = { ( subject ) => {
177220 void navigate ( { to : `./${ subject . id } /table` } ) ;
178221 } }
0 commit comments