Skip to content

Commit

Permalink
Feat: Use react query for fetching files into the FilesList
Browse files Browse the repository at this point in the history
Ref: #27
  • Loading branch information
mpetojevic committed Mar 21, 2024
1 parent 9200e1b commit a960782
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/components/assignment/assignment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import { openBrowser } from '../coursemanage/overview/util';
import OpenInBrowserIcon from '@mui/icons-material/OpenInBrowser';
import { Scope, UserPermissions } from '../../services/permission.service';
import { GradeBook } from '../../services/gradebook';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

const calculateActiveStep = (submissions: Submission[]) => {
const hasFeedback = submissions.reduce(
Expand Down Expand Up @@ -285,6 +288,7 @@ export const AssignmentComponent = () => {
};

return (
<QueryClientProvider client={queryClient}>
<Box sx={{ flex: 1, overflow: 'auto' }}>
<Box>
<Box sx={{ mt: 6 }}>
Expand Down Expand Up @@ -433,5 +437,6 @@ export const AssignmentComponent = () => {
/>
</Box>
</Box>
</QueryClientProvider>
);
};
5 changes: 5 additions & 0 deletions src/components/coursemanage/files/file-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { useRouteLoaderData } from 'react-router-dom';
import { Lecture } from '../../../model/lecture';
import { Assignment } from '../../../model/assignment';
import { Submission } from '../../../model/submission';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

export const FileView = () => {
const { lecture, assignments, users } = useRouteLoaderData('lecture') as {
Expand All @@ -26,10 +29,12 @@ export const FileView = () => {
};

return (
<QueryClientProvider client={queryClient}>
<Files
lecture={lecture}
assignment={assignmentState}
onAssignmentChange={onAssignmentChange}
/>
</QueryClientProvider>
);
};
31 changes: 6 additions & 25 deletions src/components/coursemanage/grading/create-submission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import {
} from '../../../services/submissions.service';
import { enqueueSnackbar } from 'notistack';
import { GraderLoadingButton } from '../../util/loading-button';
import { QueryClient, QueryClientProvider, useMutation } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';

const queryClient = new QueryClient();
import { useMutation } from '@tanstack/react-query';

export const CreateSubmission = () => {
const { assignment, rows, setRows } = useOutletContext() as {
Expand Down Expand Up @@ -102,12 +99,6 @@ export const CreateSubmission = () => {
createSubmissionMutation.mutate();
};

const [reloadFilesToggle, setReloadFiles] = React.useState(false);

const reloadFiles = () => {
setReloadFiles(!reloadFilesToggle);
};

return (
<Box sx={{ overflow: 'auto' }}>
<Stack direction={'column'} sx={{ flex: '1 1 100%' }}>
Expand Down Expand Up @@ -147,21 +138,11 @@ export const CreateSubmission = () => {
/>
)}
/>
<Stack
direction={'row'}
justifyContent={'flex-start'}
alignItems={'center'}
spacing={2}
sx={{ ml: 2 }}
>
<Typography>Submission Files</Typography>
<Tooltip title="Reload Files">
<IconButton aria-label="reload" onClick={() => reloadFiles()}>
<ReplayIcon />
</IconButton>
</Tooltip>
</Stack>

<Typography
sx={{ ml: 2 }}
>
Submission Files
</Typography>
<FilesList path={path} sx={{ m: 2 }} />
<Stack direction={'row'} sx={{ ml: 2 }} spacing={2}>
<GraderLoadingButton
Expand Down
2 changes: 1 addition & 1 deletion src/components/coursemanage/grading/grading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ export const GradingComponent = () => {
}}
/>
</Stack>
<ReactQueryDevtools initialIsOpen={false} />
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
</QueryClientProvider>
);
};
13 changes: 11 additions & 2 deletions src/components/util/file-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import FileItem from './file-item';
import FolderItem from './folder-item';
import { Assignment } from '../../model/assignment';
import { Lecture } from '../../model/lecture';
import { useQuery } from '@tanstack/react-query';

interface IFileListProps {
path: string;
Expand All @@ -38,10 +39,16 @@ interface IFileListProps {
}

export const FilesList = (props: IFileListProps) => {
const [files, setFiles] = React.useState([]);
const { data: files = [], refetch } = useQuery({
queryKey: ['files', props.path],
queryFn: () => getFiles(props.path),
// Disable automatic refetching, since we want to subscribe directyly on property changes.
refetchOnMount: false,
refetchInterval: false,
});

React.useEffect(() => {
getFiles(props.path).then(files => setFiles(files));
refetch();
}, [props]);

const inContained = (file: string) => {
Expand All @@ -51,6 +58,8 @@ export const FilesList = (props: IFileListProps) => {
return true;
};



const generateItems = (files: File[]) => {
const filePaths = files.flatMap(file =>
extractRelativePathsAssignment(file)
Expand Down

0 comments on commit a960782

Please sign in to comment.