11import { computed , toRef , unref } from 'vue'
2+ import { getSessionStatus } from '@/shared/utils/sessionsUtils'
3+
4+ const toSortableTimestamp = ( value ) => {
5+ if ( ! value ) return 0
6+ if ( typeof value ?. toMillis === 'function' ) return value . toMillis ( )
7+
8+ const timestamp = new Date ( value ) . getTime ( )
9+ return Number . isNaN ( timestamp ) ? 0 : timestamp
10+ }
11+
12+ const getSessionItemValue = ( item ) =>
13+ [ item . id , item . userDocId ?? item . email , item . email , item . testDate ]
14+ . map ( ( value ) => encodeURIComponent ( String ( value ?? '' ) ) )
15+ . join ( ':' )
16+
17+ const getSortableTitle = ( item ) =>
18+ item . header ?. templateTitle ?? item . testTitle ?? item . title ?? item . email ?? ''
19+
20+ const getSortableOwner = ( item , type ) => {
21+ if ( type === 'myTemplates' ) return ''
22+ if ( type === 'publicTemplates' ) {
23+ return item . header ?. templateAuthor ?. userEmail ?? ''
24+ }
25+
26+ return item . testAdmin ?. email ?? item . testAuthorEmail ?? ''
27+ }
28+
29+ const getSortableParticipantCount = ( item ) =>
30+ item . numberColaborators ?? item . cooperators ?. length ?? 0
31+
32+ const getSortableCreationDate = ( item , type ) => {
33+ if ( type === 'myTemplates' || type === 'publicTemplates' ) {
34+ return item . header ?. creationDate ?? 0
35+ }
36+
37+ return item . creationDate ?? item . updateDate ?? 0
38+ }
239
340export function useDataTableConfig ( type , t , options = { } ) {
441 const typeRef = toRef ( type )
542
43+ const itemValue = ( item ) =>
44+ typeRef . value === 'sessions' ? getSessionItemValue ( item ) : item . id
45+
646 const headers = computed ( ( ) => {
47+ const currentType = typeRef . value
48+
749 const baseHeaders = [
850 {
951 title : t ( 'common.table.type' ) ,
@@ -15,8 +57,7 @@ export function useDataTableConfig(type, t, options = {}) {
1557 title : t ( 'common.table.name' ) ,
1658 key : 'name' ,
1759 sortable : true ,
18- value : ( item ) =>
19- item . header ?. templateTitle ?? item . testTitle ?? item . email ,
60+ value : getSortableTitle ,
2061 } ,
2162 {
2263 title : t ( 'common.table.tags' ) ,
@@ -28,45 +69,50 @@ export function useDataTableConfig(type, t, options = {}) {
2869 title : t ( 'common.table.owner' ) ,
2970 key : 'owner' ,
3071 sortable : true ,
72+ value : ( item ) => getSortableOwner ( item , currentType ) ,
3173 } ,
3274 ]
3375
34- if ( typeRef . value === 'sessions' ) {
76+ if ( currentType === 'sessions' ) {
3577 baseHeaders . push ( {
3678 title : t ( 'common.table.evaluator' ) ,
3779 key : 'evaluator' ,
3880 sortable : true ,
81+ value : ( item ) => item . email ?? '' ,
3982 } )
4083 baseHeaders . push ( {
4184 title : t ( 'common.table.status' ) ,
4285 key : 'status' ,
4386 sortable : true ,
87+ value : ( item ) => getSessionStatus ( item . testDate ) . status ,
4488 } )
4589 baseHeaders . push ( {
4690 title : t ( 'common.table.sessionDate' ) ,
4791 key : 'testDate' ,
4892 sortable : true ,
93+ value : ( item ) => toSortableTimestamp ( item . testDate ) ,
4994 } )
5095 }
5196
5297 if (
53- typeRef . value !== 'sessions' &&
54- typeRef . value !== 'myTemplates' &&
55- typeRef . value !== 'publicTemplates'
98+ currentType !== 'sessions' &&
99+ currentType !== 'myTemplates' &&
100+ currentType !== 'publicTemplates'
56101 ) {
57102 baseHeaders . push ( {
58103 title : t ( 'common.table.participants' ) ,
59104 key : 'participants' ,
60105 sortable : true ,
61106 align : 'center' ,
62- value : ( item ) => item . numberColaborators ?? 0 ,
107+ value : getSortableParticipantCount ,
63108 } )
64109 }
65110
66111 baseHeaders . push ( {
67112 title : t ( 'common.table.created' ) ,
68113 key : 'creationDate' ,
69114 sortable : true ,
115+ value : ( item ) => getSortableCreationDate ( item , currentType ) ,
70116 } )
71117
72118 if ( unref ( options . showActions ) ) {
@@ -99,5 +145,6 @@ export function useDataTableConfig(type, t, options = {}) {
99145 return {
100146 headers,
101147 getEmptyStateMessage,
148+ itemValue,
102149 }
103150}
0 commit comments