Skip to content

Commit 4d1165d

Browse files
committed
Admin table: add option to show only active courses
1 parent 4f9267a commit 4d1165d

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/client/components/Admin/ChatInstances/ChatInstanceTable.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import TableCell from '@mui/material/TableCell'
77
import TableContainer from '@mui/material/TableContainer'
88
import TableHead from '@mui/material/TableHead'
99
import TablePagination from '@mui/material/TablePagination'
10-
import { TextField, Link, TableSortLabel } from '@mui/material'
10+
import {
11+
TextField,
12+
Link,
13+
TableSortLabel,
14+
Checkbox,
15+
InputLabel,
16+
} from '@mui/material'
1117
import TableRow from '@mui/material/TableRow'
1218
import { debounce } from 'lodash'
1319
import Paper from '@mui/material/Paper'
@@ -192,6 +198,7 @@ const ChatInstanceTable = () => {
192198
const [order, setOrder] = React.useState<Order>('asc')
193199
const [orderBy, setOrderBy] =
194200
React.useState<keyof ChatInstanceWithTokens>('name')
201+
const [showActiveCourses, setShowActiveCourses] = React.useState(false)
195202

196203
const [page, setPage] = React.useState(0)
197204
const [rowsPerPage, setRowsPerPage] = React.useState(10)
@@ -202,6 +209,7 @@ const ChatInstanceTable = () => {
202209
search: deferredSearch,
203210
order,
204211
orderBy,
212+
showActiveCourses,
205213
})
206214

207215
const handleChangePage = React.useCallback(
@@ -242,6 +250,16 @@ const ChatInstanceTable = () => {
242250
return (
243251
<Box sx={{ width: '100%' }}>
244252
<Paper sx={{ width: '100%', mb: 2 }}>
253+
<Box>
254+
<InputLabel>
255+
<Checkbox
256+
checked={showActiveCourses}
257+
onChange={() => setShowActiveCourses(!showActiveCourses)}
258+
inputProps={{ 'aria-label': 'controlled' }}
259+
/>
260+
Näytä aktiiviset kurssit
261+
</InputLabel>
262+
</Box>
245263
<Box sx={{ display: 'flex', alignItems: 'center' }}>
246264
<TextField
247265
label={t('admin:searchCourse')}

src/client/components/Admin/ChatInstances/useChatInstances.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ const useChatInstances = ({
1414
search = '',
1515
order = '',
1616
orderBy = '',
17+
showActiveCourses = false,
1718
}) => {
18-
const queryKey = ['chatInstances', { limit, offset, search, order, orderBy }]
19+
const queryKey = [
20+
'chatInstances',
21+
{ limit, offset, search, order, orderBy, showActiveCourses },
22+
]
1923

2024
const queryFn = async (): Promise<{
2125
chatInstances: ChatInstanceWithTokens[]
2226
count: number
2327
}> => {
2428
const res = await apiClient.get(
25-
`/chatinstances?limit=${limit}&offset=${offset}&search=${search}&orderBy=${orderBy}&order=${order}`
29+
`/chatinstances?limit=${limit}&offset=${offset}&search=${search}&orderBy=${orderBy}&order=${order}&showActiveCourses=${showActiveCourses}`
2630
)
2731

2832
const { data } = res

src/server/routes/chatInstance.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ chatInstanceRouter.get('/', async (req, res) => {
1515
search: searchRaw,
1616
order: orderRaw,
1717
orderBy: orderByRaw,
18+
showActiveCourses: showActiveCoursesRaw,
1819
} = req.query
1920
const limit = limitStr ? parseInt(limitStr as string, 10) : 100
2021
const offset = offsetStr ? parseInt(offsetStr as string, 10) : 0
2122
const search = String(searchRaw)
2223
const order = String(orderRaw)
2324
const orderBy = String(orderByRaw)
25+
const showActiveCourses = String(showActiveCoursesRaw)
2426
const hasSearch = search && search.length >= 4
2527

2628
const { rows: chatInstances, count } = await ChatInstance.findAndCountAll({
@@ -45,16 +47,27 @@ chatInstanceRouter.get('/', async (req, res) => {
4547
],
4648
exclude: ['updatedAt', 'createdAt'],
4749
},
48-
where: hasSearch
49-
? {
50-
[Op.or]: [
51-
{ courseId: { [Op.like]: `${search}%` } },
52-
{ 'name.en': { [Op.iLike]: `%${search}%` } },
53-
{ 'name.fi': { [Op.iLike]: `%${search}%` } },
54-
{ 'name.sv': { [Op.iLike]: `%${search}%` } },
55-
],
56-
}
57-
: undefined,
50+
where:
51+
// eslint-disable-next-line no-nested-ternary
52+
showActiveCourses === 'true'
53+
? {
54+
'activityPeriod.endDate': {
55+
[Op.gt]: new Date().toISOString(),
56+
},
57+
'activityPeriod.startDate': {
58+
[Op.lt]: new Date().toISOString(),
59+
},
60+
}
61+
: hasSearch
62+
? {
63+
[Op.or]: [
64+
{ courseId: { [Op.like]: `${search}%` } },
65+
{ 'name.en': { [Op.iLike]: `%${search}%` } },
66+
{ 'name.fi': { [Op.iLike]: `%${search}%` } },
67+
{ 'name.sv': { [Op.iLike]: `%${search}%` } },
68+
],
69+
}
70+
: undefined,
5871
limit,
5972
offset,
6073
order: [

0 commit comments

Comments
 (0)