@@ -10,11 +10,13 @@ import { UserContext } from "../utils/user";
10
10
import { Map } from "immutable" ;
11
11
import {
12
12
RoomInput ,
13
+ Room as GraphQLRoom ,
13
14
StaffRole ,
14
- UpdateRoomMutation ,
15
15
useAddRoomMutation ,
16
16
useDeleteRoomMutation ,
17
17
useUpdateRoomMutation ,
18
+ WeeklyEvent ,
19
+ useArchiveRoomMutation ,
18
20
} from "../generated/graphql" ;
19
21
import { Form , Formik } from "formik" ;
20
22
import {
@@ -38,12 +40,20 @@ import { RoomDeleteAlert } from "../components/room/RoomDeleteAlert";
38
40
39
41
type Props = { } ;
40
42
41
- const placeholderRoom : RoomInput = {
43
+ const placeholderRoom : Room = {
44
+ id : "" ,
42
45
name : "" ,
43
46
capacity : 0 ,
44
47
enforceCapacity : false ,
45
- manuallyDisabled : false ,
46
48
activeTimes : [ ] ,
49
+ archived : false ,
50
+ } ;
51
+
52
+ type Room = Pick <
53
+ GraphQLRoom ,
54
+ "id" | "name" | "capacity" | "enforceCapacity" | "archived"
55
+ > & {
56
+ activeTimes : Pick < WeeklyEvent , "startTime" | "endTime" | "day" > [ ] ;
47
57
} ;
48
58
49
59
export const RoomPageContainer : React . FC < Props > = ( ) => {
@@ -58,8 +68,17 @@ export const RoomPageContainer: React.FC<Props> = () => {
58
68
onOpen : openDeleteAlert ,
59
69
} = useDisclosure ( ) ;
60
70
const [ courses , setCourses ] = useState <
61
- Map < string , { [ key : string ] : RoomInput } >
71
+ Map < string , { [ key : string ] : Room } >
62
72
> ( Map ( ) ) ;
73
+ const isCoordinator = useMemo (
74
+ ( ) =>
75
+ user . getCourseStaff . some (
76
+ ( courseStaff ) =>
77
+ courseStaff . role === StaffRole . Coordinator &&
78
+ courseStaff . course . id === chosenCourse
79
+ ) ,
80
+ [ user . getCourseStaff , chosenCourse ]
81
+ ) ;
63
82
const toast = useToast ( ) ;
64
83
const [
65
84
updateRoomMutation ,
@@ -73,6 +92,10 @@ export const RoomPageContainer: React.FC<Props> = () => {
73
92
deleteRoomMutation ,
74
93
{ data : deleteRoomMutationData , loading : deleteRoomMutationLoading } ,
75
94
] = useMutationWithError ( useDeleteRoomMutation , { errorPolicy : "all" } ) ;
95
+ const [
96
+ archiveRoomMutation ,
97
+ { data : archiveRoomMutationData , loading : archiveRoomMutationLoading } ,
98
+ ] = useMutationWithError ( useArchiveRoomMutation , { errorPolicy : "all" } ) ;
76
99
useEffect ( ( ) => {
77
100
document . title = "Manage Rooms" ;
78
101
} , [ ] ) ;
@@ -82,15 +105,16 @@ export const RoomPageContainer: React.FC<Props> = () => {
82
105
setChosenRoom ( "" ) ;
83
106
} , [ chosenCourse ] ) ;
84
107
const updateRoom = useCallback (
85
- ( room : UpdateRoomMutation [ "updateRoom" ] ) => {
108
+ ( room : Room ) => {
86
109
setCourses ( ( prev ) =>
87
110
prev . set ( chosenCourse , {
88
111
...prev . get ( chosenCourse ) ,
89
112
[ room . id ] : {
113
+ id : room . id ,
90
114
name : room . name ,
91
115
capacity : room . capacity ,
92
116
enforceCapacity : room . enforceCapacity ,
93
- manuallyDisabled : room . manuallyDisabled ,
117
+ archived : room . archived ,
94
118
activeTimes : room . activeTimes ,
95
119
} ,
96
120
} )
@@ -127,6 +151,12 @@ export const RoomPageContainer: React.FC<Props> = () => {
127
151
const updatedRoom = updateRoomMutationData . updateRoom ;
128
152
updateRoom ( updatedRoom ) ;
129
153
} , [ updateRoomMutationData , updateRoom ] ) ;
154
+ useEffect ( ( ) => {
155
+ if ( ! archiveRoomMutationData ) {
156
+ return ;
157
+ }
158
+ updateRoom ( archiveRoomMutationData . archiveRoom ) ;
159
+ } , [ archiveRoomMutationData , updateRoom ] ) ;
130
160
useEffect ( ( ) => {
131
161
if ( ! addRoomMutationData ) {
132
162
return ;
@@ -150,16 +180,17 @@ export const RoomPageContainer: React.FC<Props> = () => {
150
180
prev . set (
151
181
courseStaff . course . id ,
152
182
courseStaff . course . rooms . reduce < {
153
- [ key : string ] : RoomInput ;
183
+ [ key : string ] : Room ;
154
184
} > (
155
185
( prevValue , currentRoom ) => ( {
156
186
...prevValue ,
157
187
[ currentRoom . id ] : {
188
+ id : currentRoom . id ,
158
189
name : currentRoom . name ,
159
190
capacity : currentRoom . capacity ,
160
191
enforceCapacity : currentRoom . enforceCapacity ,
161
- manuallyDisabled : currentRoom . manuallyDisabled ,
162
192
activeTimes : currentRoom . activeTimes ,
193
+ archived : currentRoom . archived ,
163
194
} ,
164
195
} ) ,
165
196
{ }
@@ -168,7 +199,7 @@ export const RoomPageContainer: React.FC<Props> = () => {
168
199
) ;
169
200
} ) ;
170
201
} , [ user ] ) ;
171
- const room = useMemo < RoomInput > ( ( ) => {
202
+ const room = useMemo < Room > ( ( ) => {
172
203
if ( isAdding ) {
173
204
return placeholderRoom ;
174
205
} else {
@@ -250,10 +281,6 @@ export const RoomPageContainer: React.FC<Props> = () => {
250
281
label = "Enforce Capacity:"
251
282
name = "enforceCapacity"
252
283
/>
253
- < FormikCheckbox
254
- label = "Disabled:"
255
- name = "manuallyDisabled"
256
- />
257
284
< FormikActiveTimeInput
258
285
name = "activeTimes"
259
286
label = "Weekly Active Times"
@@ -270,6 +297,35 @@ export const RoomPageContainer: React.FC<Props> = () => {
270
297
Save
271
298
</ Button >
272
299
{ ! isAdding && (
300
+ < Button
301
+ isLoading = {
302
+ archiveRoomMutationLoading
303
+ }
304
+ colorScheme = {
305
+ room . archived
306
+ ? "green"
307
+ : "orange"
308
+ }
309
+ variant = {
310
+ ! room . archived
311
+ ? "solid"
312
+ : "outline"
313
+ }
314
+ onClick = { ( ) =>
315
+ archiveRoomMutation ( {
316
+ variables : {
317
+ roomId : chosenRoom ,
318
+ archive : ! room . archived ,
319
+ } ,
320
+ } )
321
+ }
322
+ >
323
+ { room . archived
324
+ ? "Restore"
325
+ : "Archive" }
326
+ </ Button >
327
+ ) }
328
+ { ! isAdding && isCoordinator && (
273
329
< Button
274
330
colorScheme = "red"
275
331
isLoading = {
0 commit comments