-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTopicsTable.tsx
More file actions
156 lines (147 loc) · 4.12 KB
/
Copy pathTopicsTable.tsx
File metadata and controls
156 lines (147 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { DataTableColumn } from 'mantine-datatable'
import { DataTable } from 'mantine-datatable'
import { formatDate } from '../../utils/format'
import { useTopicsContext } from '../../providers/TopicsProvider/hooks'
import type { ITopicOverview } from '../../requests/responses/topic'
import { TopicState } from '../../requests/responses/topic'
import { Link, useNavigate } from 'react-router'
import { Badge, Box, Center, Stack, Text } from '@mantine/core'
import AvatarUserList from '../AvatarUserList/AvatarUserList'
import ThesisTypeBadge from '../../pages/LandingPage/components/ThesisTypBadge/ThesisTypBadge'
type TopicColumn =
| 'title'
| 'types'
| 'supervisor'
| 'examiner'
| 'researchGroup'
| 'state'
| 'createdAt'
| string
interface ITopicOverviewsTableProps {
columns?: TopicColumn[]
extraColumns?: Record<string, DataTableColumn<ITopicOverview>>
noBorder?: boolean
}
const TopicsTable = (props: ITopicOverviewsTableProps) => {
const {
extraColumns = {},
columns = ['title', 'types', 'examiner', 'supervisor'],
noBorder = false,
} = props
const navigate = useNavigate()
const { topics, page, setPage, limit, isLoading } = useTopicsContext()
const getTopicColor = (state: TopicState) => {
switch (state) {
case TopicState.OPEN:
return 'green'
case TopicState.CLOSED:
return 'red'
case TopicState.DRAFT:
return 'yellow'
case TopicState.EXPIRED:
return 'orange'
default:
return 'gray'
}
}
const columnConfig: Record<TopicColumn, DataTableColumn<ITopicOverview>> = {
state: {
accessor: 'state',
title: 'State',
textAlign: 'center',
width: 100,
render: (topic) => (
<Center>
<Badge color={getTopicColor(topic.state)} radius='sm'>
{topic.state}
</Badge>
</Center>
),
},
title: {
accessor: 'title',
title: 'Title',
cellsStyle: () => ({ minWidth: 200 }),
render: (record) => (
<Box
component={Link}
to={`/topics/${record.topicId}`}
style={{ textDecoration: 'none', color: 'inherit' }}
>
<Box w={'100%'}>{record.title}</Box>
</Box>
),
},
types: {
accessor: 'thesisTypes',
title: 'Thesis Types',
width: 180,
ellipsis: true,
render: (topic) => (
<Stack gap={2}>
{topic.thesisTypes ? (
topic.thesisTypes.map((type) => <ThesisTypeBadge type={type} key={type} />)
) : (
<ThesisTypeBadge type='Any' key='any' />
)}
</Stack>
),
},
examiner: {
accessor: 'examiner',
title: 'Examiner',
width: 180,
ellipsis: true,
render: (topic) => <AvatarUserList users={topic.examiners ?? []} />,
},
supervisor: {
accessor: 'supervisor',
title: 'Supervisor(s)',
width: 180,
ellipsis: true,
render: (topic) => <AvatarUserList users={topic.supervisors ?? []} />,
},
researchGroup: {
accessor: 'researchGroup.name',
title: 'Research Group',
width: 180,
ellipsis: true,
render: (topic) => (
<Text size='sm' style={{ whiteSpace: 'normal', wordBreak: 'break-word' }}>
{topic.researchGroup?.name ?? ''}
</Text>
),
},
createdAt: {
accessor: 'createdAt',
title: 'Created At',
width: 150,
ellipsis: true,
render: (record) => formatDate(record.createdAt),
},
...extraColumns,
}
return (
<DataTable
fetching={isLoading}
withTableBorder={!noBorder}
minHeight={200}
noRecordsText='No topics to show'
borderRadius='sm'
verticalSpacing='md'
striped
highlightOnHover
totalRecords={topics?.totalElements ?? 0}
recordsPerPage={limit}
page={page + 1}
onPageChange={(x) => setPage(x - 1)}
records={topics?.content}
idAccessor='topicId'
columns={columns.map((column) => columnConfig[column])}
onRowClick={({ record }) => {
void navigate(`/topics/${record.topicId}`)
}}
/>
)
}
export default TopicsTable