@@ -41,7 +41,13 @@ import {
4141 formatExportData , columnVisibilityLocalStorageKey
4242} from './constants' ;
4343import { THREE_HALF_HOURS , THREE_HOURS } from '../../../../config/TimeUnits' ;
44- import { ActivitySearchRequest , ActivitySearchValidation , Sorting } from './definitions' ;
44+ import {
45+ ActivitySearchRequest ,
46+ ActivitySearchValidation ,
47+ Sorting ,
48+ ExportMutationVariables ,
49+ VisibilityConfig
50+ } from './definitions' ;
4551import './styles.scss' ;
4652
4753const csvConfig = mkConfig ( {
@@ -114,64 +120,69 @@ const TestSearch = () => {
114120 pageSize : data . pageSize
115121 } ) ;
116122 } ,
117- onError : ( error ) => {
123+ onError : ( error : unknown ) => {
124+ let errorMessage = 'Search failed.' ;
125+ if ( error instanceof Error && error . message ) {
126+ errorMessage = `Search failed with the error: ${ error . message } ` ;
127+ } else if ( typeof error === 'string' ) {
128+ errorMessage = `Search failed with the error: ${ error } ` ;
129+ }
118130 setAlert ( {
119131 status : 'error' ,
120- message : `Search failed with the error: ${ error . message } `
132+ message : errorMessage
121133 } ) ;
122134 }
123135 } ) ;
124136
125137 const exportMutation = useMutation ( {
126- mutationFn : ( filter :
127- ActivitySearchRequest ) => searchTestingActivities ( filter , undefined , undefined , true , 0 , 0 ) ,
138+ mutationFn : ( {
139+ filter,
140+ sortBy,
141+ sortDirection
142+ } : ExportMutationVariables ) => searchTestingActivities (
143+ filter ,
144+ sortBy ,
145+ sortDirection ,
146+ true ,
147+ 0 ,
148+ 0
149+ ) ,
128150
129- onSuccess : ( data ) => {
130- const visibilityConfig = JSON . parse (
151+ onSuccess : ( data : PaginatedTestingSearchResponseType ) => {
152+ const visibilityConfig : VisibilityConfig = JSON . parse (
131153 localStorage . getItem ( columnVisibilityLocalStorageKey ) || '{}'
132154 ) ;
133155
134156 const isVisible = ( key : string ) => visibilityConfig [ key ] !== false ;
135157
136- type FilteredRow = Record <
137- keyof TestingSearchResponseType ,
138- TestingSearchResponseType [ keyof TestingSearchResponseType ] | undefined
139- > ;
140-
141- const filterItem = ( item : TestingSearchResponseType ) : FilteredRow => Object
142- . keys ( item ) . reduce ( ( acc , key ) => {
143- if ( ! isVisible ( key ) ) return acc ;
144- if ( ! Object . hasOwnProperty . call ( formatExportData , key ) ) return acc ;
145-
146- const k = key as keyof TestingSearchResponseType ;
147- acc [ k ] = item [ k ] ;
148- return acc ;
149- } , { } as FilteredRow ) ;
150-
151- const formatItem = ( item : FilteredRow ) => Object . entries ( item ) . reduce ( ( acc , [ key ] ) => {
152- const config = formatExportData [ key as keyof typeof formatExportData ] ;
153-
154- if ( config ) {
155- acc [ config . header ] = config . value ( item as TestingSearchResponseType ) ;
156- }
157-
158- if ( key === 'Result' ) {
159- acc . Result = formatExportData . Result . value ( item as TestingSearchResponseType ) ;
160- }
161-
162- return acc ;
163- } , { } as Record < string , any > ) ;
164-
165- const filteredContent : FilteredRow [ ] = data . content . map ( filterItem ) ;
166- const formattedContent = filteredContent . map ( formatItem ) ;
158+ const formattedContent = data . content . map (
159+ ( row : TestingSearchResponseType ) => {
160+ const out : Record < string , string | number | null > = { } ;
161+ (
162+ Object . entries ( formatExportData ) as Array <
163+ [
164+ keyof typeof formatExportData ,
165+ ( typeof formatExportData ) [ keyof typeof formatExportData ]
166+ ]
167+ >
168+ ) . forEach ( ( [ key , cfg ] ) => {
169+ if ( ! isVisible ( String ( key ) ) ) return ;
170+ out [ cfg . header ] = cfg . value ( row ) ;
171+ } ) ;
172+ return out ;
173+ }
174+ ) ;
167175
168- const csv = generateCsv ( csvConfig ) ( formattedContent ) ;
169- download ( csvConfig ) ( csv ) ;
176+ const csv = generateCsv ( csvConfig ) ( formattedContent ) ;
177+ download ( csvConfig ) ( csv ) ;
170178 } ,
171- onError : ( error ) => {
179+ onError : ( error : unknown ) => {
180+ const message = error instanceof Error && error . message
181+ ? error . message
182+ : 'Unknown error' ;
172183 setAlert ( {
173184 status : 'error' ,
174- message : `Failed to export data: ${ error ?. message || 'Unknown error' } `
185+ message : `Failed to export data: ${ message } `
175186 } ) ;
176187 }
177188 } ) ;
@@ -190,7 +201,12 @@ const TestSearch = () => {
190201 } ;
191202
192203 const handleExportData = ( ) => {
193- exportMutation . mutate ( searchParams ) ;
204+ const sort = sorting [ 0 ] ;
205+ exportMutation . mutate ( {
206+ filter : searchParams ,
207+ sortBy : sort ?. id ,
208+ sortDirection : sort ?. desc ? 'desc' : 'asc'
209+ } ) ;
194210 } ;
195211
196212 const testTypeQuery = useQuery ( {
@@ -221,7 +237,7 @@ const TestSearch = () => {
221237 sortDirection : sort ?. desc ? 'desc' : 'asc'
222238 } ,
223239 {
224- onSuccess : ( data ) => {
240+ onSuccess : ( data : PaginatedTestingSearchResponseType ) => {
225241 setSearchResults ( data . content ) ;
226242 setPaginationInfo ( {
227243 totalElements : data . totalElements ,
0 commit comments