Skip to content

Commit f70cfc4

Browse files
fix: removed useless state hooks and replaced them with tanqueries
1 parent d5dcdd4 commit f70cfc4

File tree

4 files changed

+31
-46
lines changed

4 files changed

+31
-46
lines changed

src/components/coursemanage/lecture.tsx

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { queryClient } from '../../widgets/assignmentmanage';
4141
interface IAssignmentTableProps {
4242
lecture: Lecture;
4343
rows: Assignment[];
44-
setAssignments: React.Dispatch<React.SetStateAction<Assignment[]>>;
44+
refreshAssignments: any;
4545
}
4646

4747
const AssignmentTable = (props: IAssignmentTableProps) => {
@@ -119,9 +119,7 @@ const AssignmentTable = (props: IAssignmentTableProps) => {
119119
variant: 'success'
120120
}
121121
);
122-
props.setAssignments(
123-
props.rows.filter(a => a.id !== row.id)
124-
);
122+
props.refreshAssignments();
125123
} catch (error: any) {
126124
enqueueSnackbar(error.message, {
127125
variant: 'error'
@@ -154,53 +152,29 @@ const AssignmentTable = (props: IAssignmentTableProps) => {
154152
};
155153

156154
export const LectureComponent = () => {
155+
const [isEditDialogOpen, setEditDialogOpen] = React.useState(false);
157156
const { lectureId } = extractIdsFromBreadcrumbs();
158157

159-
const { data: lecture, isLoading: isLoadingLecture } = useQuery<Lecture>({
158+
const { data: lecture } = useQuery<Lecture>({
160159
queryKey: ['lecture', lectureId],
161160
queryFn: () => getLecture(lectureId, true),
162-
enabled: !!lectureId
161+
enabled: true
163162
});
164163

165164
const {
166165
data: assignments = [],
167-
isLoading: isLoadingAssignments,
168-
refetch: refetchAssignments
166+
isPending: isPendingAssignments,
167+
refetch: refreshAssignments
169168
} = useQuery<AssignmentDetail[]>({
170-
queryKey: ['assignments', lecture, lectureId],
169+
queryKey: ['assignments', lectureId],
171170
queryFn: () => getAllAssignments(lectureId),
172-
enabled: !!lecture
171+
enabled: true
173172
});
174173

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-
199174
const handleUpdateLecture = updatedLecture => {
200175
updateLecture(updatedLecture).then(
201176
async response => {
202177
await updateMenus(true);
203-
setLecture(response);
204178
// Invalidate query key "lectures" and "completedLectures", so that we trigger refetch on lectures table and correct lecture name is shown in the table!
205179
await queryClient.invalidateQueries({ queryKey: ['lectures'] });
206180
await queryClient.invalidateQueries({
@@ -215,11 +189,21 @@ export const LectureComponent = () => {
215189
);
216190
};
217191

192+
if (isPendingAssignments) {
193+
return (
194+
<div>
195+
<Card>
196+
<LinearProgress />
197+
</Card>
198+
</div>
199+
);
200+
}
201+
218202
return (
219203
<Stack direction={'column'} sx={{ mt: 5, ml: 5, flex: 1 }}>
220204
<Typography variant={'h4'} sx={{ mr: 2 }}>
221-
{lectureState.name}
222-
{lectureState.complete ? (
205+
{lecture.name}
206+
{lecture.complete ? (
223207
<Typography
224208
sx={{
225209
display: 'inline-block',
@@ -250,7 +234,7 @@ export const LectureComponent = () => {
250234
textDecoration: 'underline',
251235
fontWeight: 'bold'
252236
}}
253-
onClick={handleOpenEditDialog}
237+
onClick={() => setEditDialogOpen(true)}
254238
>
255239
Rename Lecture.
256240
</span>
@@ -260,13 +244,13 @@ export const LectureComponent = () => {
260244

261245
<Stack direction="row" alignItems="center" spacing={2}>
262246
<CreateDialog
263-
lecture={lectureState}
247+
lecture={lecture}
264248
handleSubmit={async () => {
265-
await refetchAssignments();
249+
await refreshAssignments();
266250
}}
267251
/>
268252
<EditLectureDialog
269-
lecture={lectureState}
253+
lecture={lecture}
270254
handleSubmit={handleUpdateLecture}
271255
open={isEditDialogOpen}
272256
handleClose={() => setEditDialogOpen(false)}
@@ -278,9 +262,9 @@ export const LectureComponent = () => {
278262
<Typography variant={'h6'}>Assignments</Typography>
279263
</Stack>
280264
<AssignmentTable
281-
lecture={lectureState}
282-
rows={assignmentsState}
283-
setAssignments={setAssignments}
265+
lecture={lecture}
266+
rows={assignments}
267+
refreshAssignments={refreshAssignments}
284268
/>
285269
</Stack>
286270
);

src/services/assignments.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function getAllAssignments(
2525
reload = false,
2626
includeSubmissions = false
2727
): Promise<AssignmentDetail[]> {
28+
2829
let url = `/api/lectures/${lectureId}/assignments`;
2930
if (includeSubmissions) {
3031
const searchParams = new URLSearchParams({

src/widgets/assignmentmanage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class AssignmentManageView extends ReactWidget {
5151
render() {
5252
return (
5353
<QueryClientProvider client={queryClient}>
54-
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
54+
<ReactQueryDevtools initialIsOpen={false} />
5555
<ThemeProvider theme={createTheme({ palette: { mode: this.theme } })}>
5656
<CssBaseline />
5757
<SnackbarProvider

src/widgets/coursemanage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class CourseManageView extends ReactWidget {
4444
render() {
4545
return (
4646
<QueryClientProvider client={queryClient}>
47-
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
47+
<ReactQueryDevtools initialIsOpen={false} />
4848
<ThemeProvider theme={createTheme({ palette: { mode: this.theme } })}>
4949
<CssBaseline />
5050
<SnackbarProvider

0 commit comments

Comments
 (0)