@@ -41,7 +41,7 @@ import { queryClient } from '../../widgets/assignmentmanage';
41
41
interface IAssignmentTableProps {
42
42
lecture : Lecture ;
43
43
rows : Assignment [ ] ;
44
- setAssignments : React . Dispatch < React . SetStateAction < Assignment [ ] > > ;
44
+ refreshAssignments : any ;
45
45
}
46
46
47
47
const AssignmentTable = ( props : IAssignmentTableProps ) => {
@@ -119,9 +119,7 @@ const AssignmentTable = (props: IAssignmentTableProps) => {
119
119
variant : 'success'
120
120
}
121
121
) ;
122
- props . setAssignments (
123
- props . rows . filter ( a => a . id !== row . id )
124
- ) ;
122
+ props . refreshAssignments ( ) ;
125
123
} catch ( error : any ) {
126
124
enqueueSnackbar ( error . message , {
127
125
variant : 'error'
@@ -154,53 +152,29 @@ const AssignmentTable = (props: IAssignmentTableProps) => {
154
152
} ;
155
153
156
154
export const LectureComponent = ( ) => {
155
+ const [ isEditDialogOpen , setEditDialogOpen ] = React . useState ( false ) ;
157
156
const { lectureId } = extractIdsFromBreadcrumbs ( ) ;
158
157
159
- const { data : lecture , isLoading : isLoadingLecture } = useQuery < Lecture > ( {
158
+ const { data : lecture } = useQuery < Lecture > ( {
160
159
queryKey : [ 'lecture' , lectureId ] ,
161
160
queryFn : ( ) => getLecture ( lectureId , true ) ,
162
- enabled : ! ! lectureId
161
+ enabled : true
163
162
} ) ;
164
163
165
164
const {
166
165
data : assignments = [ ] ,
167
- isLoading : isLoadingAssignments ,
168
- refetch : refetchAssignments
166
+ isPending : isPendingAssignments ,
167
+ refetch : refreshAssignments
169
168
} = useQuery < AssignmentDetail [ ] > ( {
170
- queryKey : [ 'assignments' , lecture , lectureId ] ,
169
+ queryKey : [ 'assignments' , lectureId ] ,
171
170
queryFn : ( ) => getAllAssignments ( lectureId ) ,
172
- enabled : ! ! lecture
171
+ enabled : true
173
172
} ) ;
174
173
175
- React . useEffect ( ( ) => {
176
- if ( assignments . length > 0 ) {
177
- setAssignments ( assignments ) ;
178
- }
179
- } , [ assignments ] ) ;
180
-
181
- const [ lectureState , setLecture ] = React . useState ( lecture ) ;
182
- const [ assignmentsState , setAssignments ] = React . useState < Assignment [ ] > ( [ ] ) ;
183
- const [ isEditDialogOpen , setEditDialogOpen ] = React . useState ( false ) ;
184
-
185
- if ( isLoadingLecture || isLoadingAssignments ) {
186
- return (
187
- < div >
188
- < Card >
189
- < LinearProgress />
190
- </ Card >
191
- </ div >
192
- ) ;
193
- }
194
-
195
- const handleOpenEditDialog = ( ) => {
196
- setEditDialogOpen ( true ) ;
197
- } ;
198
-
199
174
const handleUpdateLecture = updatedLecture => {
200
175
updateLecture ( updatedLecture ) . then (
201
176
async response => {
202
177
await updateMenus ( true ) ;
203
- setLecture ( response ) ;
204
178
// Invalidate query key "lectures" and "completedLectures", so that we trigger refetch on lectures table and correct lecture name is shown in the table!
205
179
await queryClient . invalidateQueries ( { queryKey : [ 'lectures' ] } ) ;
206
180
await queryClient . invalidateQueries ( {
@@ -215,11 +189,21 @@ export const LectureComponent = () => {
215
189
) ;
216
190
} ;
217
191
192
+ if ( isPendingAssignments ) {
193
+ return (
194
+ < div >
195
+ < Card >
196
+ < LinearProgress />
197
+ </ Card >
198
+ </ div >
199
+ ) ;
200
+ }
201
+
218
202
return (
219
203
< Stack direction = { 'column' } sx = { { mt : 5 , ml : 5 , flex : 1 } } >
220
204
< Typography variant = { 'h4' } sx = { { mr : 2 } } >
221
- { lectureState . name }
222
- { lectureState . complete ? (
205
+ { lecture . name }
206
+ { lecture . complete ? (
223
207
< Typography
224
208
sx = { {
225
209
display : 'inline-block' ,
@@ -250,7 +234,7 @@ export const LectureComponent = () => {
250
234
textDecoration : 'underline' ,
251
235
fontWeight : 'bold'
252
236
} }
253
- onClick = { handleOpenEditDialog }
237
+ onClick = { ( ) => setEditDialogOpen ( true ) }
254
238
>
255
239
Rename Lecture.
256
240
</ span >
@@ -260,13 +244,13 @@ export const LectureComponent = () => {
260
244
261
245
< Stack direction = "row" alignItems = "center" spacing = { 2 } >
262
246
< CreateDialog
263
- lecture = { lectureState }
247
+ lecture = { lecture }
264
248
handleSubmit = { async ( ) => {
265
- await refetchAssignments ( ) ;
249
+ await refreshAssignments ( ) ;
266
250
} }
267
251
/>
268
252
< EditLectureDialog
269
- lecture = { lectureState }
253
+ lecture = { lecture }
270
254
handleSubmit = { handleUpdateLecture }
271
255
open = { isEditDialogOpen }
272
256
handleClose = { ( ) => setEditDialogOpen ( false ) }
@@ -278,9 +262,9 @@ export const LectureComponent = () => {
278
262
< Typography variant = { 'h6' } > Assignments</ Typography >
279
263
</ Stack >
280
264
< AssignmentTable
281
- lecture = { lectureState }
282
- rows = { assignmentsState }
283
- setAssignments = { setAssignments }
265
+ lecture = { lecture }
266
+ rows = { assignments }
267
+ refreshAssignments = { refreshAssignments }
284
268
/>
285
269
</ Stack >
286
270
) ;
0 commit comments