Skip to content

Commit

Permalink
feat: Added delete button to sheet card menu
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoelle committed Oct 18, 2020
1 parent 3be465b commit 7f80c1b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
49 changes: 42 additions & 7 deletions app/features/sheet-overview/SheetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {
CardActions,
CardContent,
CardHeader,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
IconButton,
LinearProgress,
ListItem,
Expand All @@ -14,16 +19,23 @@ import {
import React from 'react';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import { remote } from 'electron';
import { exportCorrections, getUniqueSheets } from '../../utils/FileAccess';
import {
deleteSheet,
exportCorrections,
getUniqueSheets,
} from '../../utils/FileAccess';
import { resolveLoader } from '../../../configs/webpack.config.eslint';

export default function SheetCard(props: any) {
const [anchorEl, setAnchorEl] = React.useState(null);
const [openConfirmDialog, setOpenConfirmDialog] = React.useState(false);
const {
sheet,
submissions,
setSheetToCorrect,
setSchemaSheet,
setTab,
reload,
} = props;

function onStartCorrection() {
Expand Down Expand Up @@ -62,10 +74,19 @@ export default function SheetCard(props: any) {
}
}

function onDeleteSheet() {
function onCloseConfirmDialog() {
setOpenConfirmDialog(false);
}

function onOpenConfirmDialog() {
onCloseMenu();
console.log('Delete!');
// TODO: deleting the task
setOpenConfirmDialog(true);
}

function onDeleteSheet() {
onCloseConfirmDialog();
deleteSheet(sheet);
reload();
}

function missingSchemas() {
Expand Down Expand Up @@ -93,9 +114,7 @@ export default function SheetCard(props: any) {
onClose={onCloseMenu}
>
<MenuItem onClick={onExport}>Export corrections</MenuItem>
<MenuItem onClick={onDeleteSheet} disabled>
Delete sheet
</MenuItem>
<MenuItem onClick={onOpenConfirmDialog}>Delete sheet</MenuItem>
</Menu>
</>
// eslint-disable-next-line prettier/prettier
Expand Down Expand Up @@ -136,6 +155,22 @@ export default function SheetCard(props: any) {
}
/>
</Card>
<Dialog open={openConfirmDialog} onClose={onCloseConfirmDialog}>
<DialogTitle>Are you sure?</DialogTitle>
<DialogContent>
<DialogContentText>
{`Are you sure you want to delete the sheet "${sheet.sheet.name}"?`}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={onDeleteSheet} color="primary" autoFocus>
Yes
</Button>
<Button onClick={onCloseConfirmDialog} color="primary">
No
</Button>
</DialogActions>
</Dialog>
</ListItem>
);
}
3 changes: 2 additions & 1 deletion app/features/sheet-overview/SheetCardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getSubmissionsOfSheet } from '../../utils/FileAccess';
import SheetCard from './SheetCard';

export default function SheetCardList(props: any) {
const { sheets, setSheetToCorrect, setSchemaSheet, setTab } = props;
const { sheets, setSheetToCorrect, setSchemaSheet, setTab, reload } = props;
if (sheets?.length > 0) {
return (
<List
Expand All @@ -28,6 +28,7 @@ export default function SheetCardList(props: any) {
setSheetToCorrect={setSheetToCorrect}
setSchemaSheet={setSchemaSheet}
setTab={setTab}
reload={reload}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions app/features/sheet-overview/SheetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default function SheetOverview(props: any) {
setSheetToCorrect={setSheetToCorrect}
setSchemaSheet={setSchemaSheet}
setTab={setTab}
reload={reload}
/>
</Box>
</div>
Expand Down
23 changes: 23 additions & 0 deletions app/utils/FileAccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,26 @@ export function getSubmissionsOfSheet(sheet: any) {
});
return subs.filter((s) => isSubmissionFromSheet(s, sheet));
}

function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
const curPath = Path.join(path, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}

export function deleteSubmission(s: any) {
const subDir = getSubmissionDir();
deleteFolderRecursive(Path.join(subDir, s.submission));
}

export function deleteSheet(sheet: any) {
getSubmissionsOfSheet(sheet).forEach((s) => deleteSubmission(s));
}

0 comments on commit 7f80c1b

Please sign in to comment.