Skip to content

Commit

Permalink
Feat: Use React Query to fetch GitLogs in files
Browse files Browse the repository at this point in the history
Ref: #27
  • Loading branch information
mpetojevic committed Apr 17, 2024
1 parent cb3ca77 commit 14cd7fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
15 changes: 2 additions & 13 deletions src/components/coursemanage/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import { openBrowser, openTerminal } from '../overview/util';
import { PageConfig } from '@jupyterlab/coreutils';
import PublishRoundedIcon from '@mui/icons-material/PublishRounded';
import {
IGitLogObject,
getGitLog,
getRemoteStatus,
lectureBasePath
} from '../../../services/file.service';
Expand Down Expand Up @@ -73,14 +71,7 @@ export const Files = (props: IFilesProps) => {
const [assignment, setAssignment] = React.useState(props.assignment);
const [lecture, setLecture] = React.useState(props.lecture);
const [selectedDir, setSelectedDir] = React.useState('source');
const [gitLogs, setGitLog] = React.useState([] as IGitLogObject[]);
const [assignmentState, setAssignmentState] = React.useState(assignment);

const updateGitLog = () => {
getGitLog(lecture, assignment, RepoType.SOURCE, 10).then(logs =>
setGitLog(logs)
);
};
const updateRemoteStatus = async () => {
const status = await getRemoteStatus(
props.lecture,
Expand All @@ -92,9 +83,7 @@ export const Files = (props: IFilesProps) => {
status as 'up_to_date' | 'pull_needed' | 'push_needed' | 'divergent'
);
};
React.useEffect(() => {
updateGitLog();
}, [assignmentState]);


openBrowser(
`${lectureBasePath}${lecture.code}/${selectedDir}/${assignment.id}`
Expand Down Expand Up @@ -409,7 +398,7 @@ export const Files = (props: IFilesProps) => {
Add new
</Button>
</Tooltip>
<GitLogModal gitLogs={gitLogs} />
<GitLogModal lecture={lecture} assignment={assignment}/>
<Tooltip title={'Show in File-Browser'}>
<IconButton
sx={{ mt: -1, pt: 0, pb: 0 }}
Expand Down
26 changes: 19 additions & 7 deletions src/components/coursemanage/files/git-log.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ import {
} from '@mui/material';

import * as React from 'react';
import { IGitLogObject } from '../../../services/file.service';
import { IGitLogObject, getGitLog } from '../../../services/file.service';
import { utcToLocalFormat } from '../../../services/datetime.service';
import GitHubIcon from '@mui/icons-material/GitHub';
import { RepoType } from '../../util/repo-type';
import { QueryClient, useMutation, useQuery } from '@tanstack/react-query';
import { Lecture } from '../../../model/lecture';
import { Assignment } from '../../../model/assignment';

const queryClient = new QueryClient();

interface IGitLogProps {
gitLogs: IGitLogObject[];
lecture: Lecture,
assignment: Assignment
}

const style = {
Expand Down Expand Up @@ -58,15 +65,20 @@ const getTimelineItem = (logItem: IGitLogObject) => {
};

export const GitLogModal = (props: IGitLogProps) => {
const [gitLogs, setGitLogs] = React.useState(props.gitLogs);
React.useEffect(() => {
setGitLogs(props.gitLogs);
}, [props.gitLogs]);

const { data: gitLogs = [] as IGitLogObject[], refetch: refetchGitLogs } = useQuery({
queryKey: ['gitLogs', props.lecture, props.assignment],
queryFn: () => getGitLog(props.lecture, props.assignment, RepoType.SOURCE, 10)
});


const [open, setOpen] = React.useState(false);
const handleOpen = () => {

const handleOpen = async () => {
setOpen(true);
await refetchGitLogs();
};

const handleClose = () => {
setOpen(false);
};
Expand Down

0 comments on commit 14cd7fc

Please sign in to comment.