Skip to content

Commit 77b8e82

Browse files
format
1 parent d57f9ed commit 77b8e82

File tree

3 files changed

+118
-62
lines changed

3 files changed

+118
-62
lines changed

src/client/components/Admin/UserSearch.tsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,18 @@ const UserTable = ({ users }: { users: User[] }) => {
108108
)
109109
}
110110

111-
112-
const ActionUserTable = ({ users, onSelect, actionText, actionButtonText }: { users: User[], onSelect: (user: User) => void, actionText: string, actionButtonText: string }) => {
113-
const { t } = useTranslation()
111+
const ActionUserTable = ({
112+
users,
113+
onSelect,
114+
actionText,
115+
actionButtonText,
116+
}: {
117+
users: User[]
118+
onSelect: (user: User) => void
119+
actionText: string
120+
actionButtonText: string
121+
}) => {
122+
const { t } = useTranslation()
114123

115124
if (!users || users.length === 0) return null
116125

@@ -145,13 +154,13 @@ const ActionUserTable = ({ users, onSelect, actionText, actionButtonText }: { us
145154
<b>{t('admin:email')}</b>
146155
</Typography>
147156
</TableCell>
148-
<TableCell />
149-
<TableCell align="left">
157+
<TableCell />
158+
<TableCell align="left">
150159
<Typography variant="h6">
151160
<b>{actionText}</b>
152161
</Typography>
153162
</TableCell>
154-
<TableCell />
163+
<TableCell />
155164
</TableRow>
156165
</TableHead>
157166
<TableBody>
@@ -172,11 +181,16 @@ const ActionUserTable = ({ users, onSelect, actionText, actionButtonText }: { us
172181
<TableCell align="left">
173182
<Typography variant="h6">{user.primaryEmail}</Typography>
174183
</TableCell>
175-
<TableCell>
176-
<Button variant="outlined" onClick={() => {onSelect(user)}}>
177-
{actionButtonText}
178-
</Button>
179-
</TableCell>
184+
<TableCell>
185+
<Button
186+
variant="outlined"
187+
onClick={() => {
188+
onSelect(user)
189+
}}
190+
>
191+
{actionButtonText}
192+
</Button>
193+
</TableCell>
180194
</TableRow>
181195
))}
182196
</TableBody>
@@ -208,7 +222,15 @@ const UserSearch = () => {
208222
)
209223
}
210224
//a component which allows users to be searched in order to perform some kind of action with the user
211-
export const ActionUserSearch = ({onSelect, actionText, actionButtonText}: {onSelect: (user: User) => void, actionText: string, actionButtonText: string}) => {
225+
export const ActionUserSearch = ({
226+
onSelect,
227+
actionText,
228+
actionButtonText,
229+
}: {
230+
onSelect: (user: User) => void
231+
actionText: string
232+
actionButtonText: string
233+
}) => {
212234
const [search, setSearch] = useState('')
213235
const { users, isLoading, refetch } = useUserSearch(search)
214236
const { t } = useTranslation()
@@ -226,7 +248,7 @@ export const ActionUserSearch = ({onSelect, actionText, actionButtonText}: {onSe
226248
{search.length > 2 && search.length < 5 && <div>{t('admin:typeMore')}</div>}
227249

228250
{isLoading && <div>Loading...</div>}
229-
{users && <ActionUserTable users={users} onSelect={onSelect} actionText={actionText} actionButtonText={actionButtonText}/>}
251+
{users && <ActionUserTable users={users} onSelect={onSelect} actionText={actionText} actionButtonText={actionButtonText} />}
230252
</Box>
231253
)
232254
}

src/client/components/Courses/Course/index.tsx

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import { Edit, OpenInNew } from '@mui/icons-material'
2-
import { Alert, Box, Button, Checkbox, Container, FormControlLabel, Input, Modal, Paper, Skeleton, Stack, Tab, TextField, Tooltip, Typography } from '@mui/material'
2+
import {
3+
Alert,
4+
Box,
5+
Button,
6+
Checkbox,
7+
Container,
8+
FormControlLabel,
9+
Input,
10+
Modal,
11+
Paper,
12+
Skeleton,
13+
Stack,
14+
Tab,
15+
TextField,
16+
Tooltip,
17+
Typography,
18+
} from '@mui/material'
319
import { enqueueSnackbar } from 'notistack'
420
import { useEffect, useState } from 'react'
521
import { useTranslation } from 'react-i18next'
@@ -43,7 +59,7 @@ const Course = () => {
4359
return <ApiErrorView error={error} />
4460
}
4561
useEffect(() => {
46-
if(isCourseSuccess){
62+
if (isCourseSuccess) {
4763
setResponsibilities(course?.responsibilities)
4864
}
4965
}, [isCourseSuccess])
@@ -109,16 +125,15 @@ const Course = () => {
109125
const handleAddResponsible = async (user: User) => {
110126
const username = user.username
111127
const result = await apiClient.post(`/courses/${course.id}/responsibilities/assign`, { username: username })
112-
if(result.status === 200){
128+
if (result.status === 200) {
113129
const responsibility = result.data
114130
setResponsibilities([...responsibilities, responsibility])
115131
refetchCourse()
116132
}
117-
118133
}
119134
const handleRemoveResponsibility = async (responsibility) => {
120135
const result = await apiClient.post(`/courses/${course.id}/responsibilities/remove`, { username: responsibility.user?.username })
121-
if(result.status === 200){
136+
if (result.status === 200) {
122137
const filteredResponsibilities = responsibilities.filter((r) => r.id !== responsibility.id)
123138
setResponsibilities(filteredResponsibilities)
124139
}
@@ -208,12 +223,25 @@ const Course = () => {
208223
</Button>
209224
{showTeachers && (
210225
<Box>
211-
<Button onClick = {() => {setAddTeacherViewOpen(true)}}>{t('course:add')}</Button>
212-
<Stack sx={{margin: 1, padding: 1, borderColor: 'gray', borderWidth: 1, borderStyle: 'solid'}}>
226+
<Button
227+
onClick={() => {
228+
setAddTeacherViewOpen(true)
229+
}}
230+
>
231+
{t('course:add')}
232+
</Button>
233+
<Stack sx={{ margin: 1, padding: 1, borderColor: 'gray', borderWidth: 1, borderStyle: 'solid' }}>
213234
{responsibilities.map((responsibility) => (
214-
<Box key={responsibility.id} sx={{display: 'flex', alignItems: 'center', padding: 1}}>
215-
<Typography>{responsibility.user.last_name} {responsibility.user.first_names}</Typography>
216-
<AssignedResponsibilityManagement handleRemove={() => {handleRemoveResponsibility(responsibility)}} responsibility={responsibility}/>
235+
<Box key={responsibility.id} sx={{ display: 'flex', alignItems: 'center', padding: 1 }}>
236+
<Typography>
237+
{responsibility.user.last_name} {responsibility.user.first_names}
238+
</Typography>
239+
<AssignedResponsibilityManagement
240+
handleRemove={() => {
241+
handleRemoveResponsibility(responsibility)
242+
}}
243+
responsibility={responsibility}
244+
/>
217245
</Box>
218246
))}
219247
</Stack>
@@ -224,20 +252,30 @@ const Course = () => {
224252
</Paper>
225253
</Box>
226254

227-
<Modal sx={{
228-
display: 'flex',
229-
justifyContent: 'center',
230-
alignItems: 'center'
231-
}} open={addTeacherViewOpen} onClose={() => setAddTeacherViewOpen(false)}>
232-
<Box sx={{
233-
width: '80vw',
234-
height: '80vh',
235-
background: 'white',
236-
padding: '2rem'
237-
}}>
238-
239-
<ActionUserSearch onSelect = {(user: User) => {handleAddResponsible(user)}} actionText={t('course:add')} actionButtonText={t('course:add')}/>
240-
255+
<Modal
256+
sx={{
257+
display: 'flex',
258+
justifyContent: 'center',
259+
alignItems: 'center',
260+
}}
261+
open={addTeacherViewOpen}
262+
onClose={() => setAddTeacherViewOpen(false)}
263+
>
264+
<Box
265+
sx={{
266+
width: '80vw',
267+
height: '80vh',
268+
background: 'white',
269+
padding: '2rem',
270+
}}
271+
>
272+
<ActionUserSearch
273+
onSelect={(user: User) => {
274+
handleAddResponsible(user)
275+
}}
276+
actionText={t('course:add')}
277+
actionButtonText={t('course:add')}
278+
/>
241279
</Box>
242280
</Modal>
243281

@@ -264,15 +302,13 @@ const Course = () => {
264302
)
265303
}
266304

267-
const AssignedResponsibilityManagement = ({responsibility, handleRemove}) => {
305+
const AssignedResponsibilityManagement = ({ responsibility, handleRemove }) => {
268306
const { t, i18n } = useTranslation()
269-
if(!responsibility.createdByUserId){
270-
return (
271-
<Stack direction={'row'} sx={{marginLeft: 'auto', alignItems: 'center', height: '1rem'}} >
272-
</Stack>
273-
)}
307+
if (!responsibility.createdByUserId) {
308+
return <Stack direction={'row'} sx={{ marginLeft: 'auto', alignItems: 'center', height: '1rem' }}></Stack>
309+
}
274310
return (
275-
<Stack direction={'row'} sx={{marginLeft: 'auto', alignItems: 'center', height: '1rem'}} >
311+
<Stack direction={'row'} sx={{ marginLeft: 'auto', alignItems: 'center', height: '1rem' }}>
276312
<Typography>{t('course:customResponsibility')}</Typography>
277313
<Button onClick={handleRemove}>{t('course:remove')}</Button>
278314
</Stack>

src/server/routes/course.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ const userAssignedAsResponsible = (userId, chatInstance) => {
299299
return isResponsible
300300
}
301301

302-
const getUser = async (id: string):Promise<User | null> => {
302+
const getUser = async (id: string): Promise<User | null> => {
303303
const user = await User.findByPk(id)
304304
if (!user) {
305305
throw ApplicationError.NotFound('User not found')
@@ -308,7 +308,7 @@ const getUser = async (id: string):Promise<User | null> => {
308308
return user
309309
}
310310

311-
const getUserByUsername = async (username: string):Promise<User | null> => {
311+
const getUserByUsername = async (username: string): Promise<User | null> => {
312312
const user = await User.findOne({
313313
where: {
314314
username: username,
@@ -340,7 +340,7 @@ courseRouter.post('/:id/responsibilities/assign', async (req, res) => {
340340
const { user } = request
341341
const chatInstance = await getChatInstance(chatInstanceId)
342342
const hasPermission = await enforceUserHasFullAccess(user, chatInstanceId)
343-
if(!hasPermission){
343+
if (!hasPermission) {
344344
res.status(401).send('Unauthorized')
345345
return
346346
}
@@ -371,10 +371,9 @@ courseRouter.post('/:id/responsibilities/assign', async (req, res) => {
371371
id: userToAssign.id,
372372
first_names: userToAssign.firstNames,
373373
last_name: userToAssign.lastName,
374-
username: userToAssign.username
375-
}
374+
username: userToAssign.username,
375+
},
376376
}
377-
378377

379378
res.json(responsibilityToReturn)
380379
})
@@ -403,7 +402,7 @@ courseRouter.post('/:id/responsibilities/remove', async (req, res) => {
403402
const { user } = request
404403
const chatInstance = await getChatInstance(chatInstanceId)
405404
const hasPermission = await enforceUserHasFullAccess(user, chatInstanceId)
406-
if(!hasPermission){
405+
if (!hasPermission) {
407406
res.status(401).send('Unauthorized')
408407
return
409408
}
@@ -414,29 +413,28 @@ courseRouter.post('/:id/responsibilities/remove', async (req, res) => {
414413
return
415414
}
416415

417-
const assignedUserId:string = userToRemove.id
418-
const userAssignedAlready:boolean = await userAssignedAsResponsible(assignedUserId, chatInstance)
416+
const assignedUserId: string = userToRemove.id
417+
const userAssignedAlready: boolean = await userAssignedAsResponsible(assignedUserId, chatInstance)
419418
if (!userAssignedAlready) {
420419
res.status(400).send('User to remove not found')
421420
return
422421
}
423422

424423
const responsibilityToRemove = await Responsibility.findOne({
425-
where: {
426-
userId: assignedUserId,
427-
chatInstanceId: chatInstanceId
428-
},
424+
where: {
425+
userId: assignedUserId,
426+
chatInstanceId: chatInstanceId,
427+
},
429428
})
430-
if(!responsibilityToRemove?.createdByUserId){
429+
if (!responsibilityToRemove?.createdByUserId) {
431430
res.status(400).send('Can only remove user that has been manually added')
432431
return
433432
}
434433

435-
try{
434+
try {
436435
await responsibilityToRemove?.destroy()
437-
res.json({result: "success"})
438-
}
439-
catch{
436+
res.json({ result: 'success' })
437+
} catch {
440438
res.status(500).send('Unknown error occurred')
441439
}
442440
})

0 commit comments

Comments
 (0)