Skip to content

Commit

Permalink
refactor: Extracted dialoges and effects from SchemaGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoelle committed May 16, 2021
1 parent b4dc115 commit 081ecc9
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 245 deletions.
62 changes: 62 additions & 0 deletions app/dialogs/OverwriteSchemaDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { initializeSheet } from '../model/SchemaSlice';
import { save } from '../utils/FileAccess';
import { getTopLevelTasks } from '../utils/TaskUtil';
import ConfirmationDialog from './ConfirmationDialog';
import StartCorrectionDialog from './StartCorrectionDialog';

export function onInitializeSheet(
dispatch,
showModal,
autosave,
selectedSheet,
tasks,
tasksEntity,
ratingsEntity,
commentsEntity
) {
dispatch(
initializeSheet(
selectedSheet.id,
tasksEntity,
ratingsEntity,
commentsEntity,
getTopLevelTasks(tasks).map((t) => t.id)
)
);

if (autosave) {
dispatch(save());
}

showModal(ConfirmationDialog, StartCorrectionDialog(selectedSheet));
}

const OverwriteSchemaDialog = (
showModal,
autosave,
selectedSheet,
tasks,
tasksEntity,
ratingsEntity,
commentsEntity
) => {
return {
title: 'Overwrite schema?',
text: `Are you sure you want to overwrite the existing schema of sheet "${selectedSheet?.name}"?
All correction progress will be lost!`,
onConfirm: (dispatch) => {
onInitializeSheet(
dispatch,
showModal,
autosave,
selectedSheet,
tasks,
tasksEntity,
ratingsEntity,
commentsEntity
);
},
};
};

export default OverwriteSchemaDialog;
66 changes: 66 additions & 0 deletions app/dialogs/PasteFromClipboardDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable no-empty */
import * as YAML from 'yaml';
import RateableTask from '../model/RateableTask';
import {
schemaSetClipboard,
schemaSetEntities,
schemaSetSelectedSheet,
} from '../model/SchemaSlice';
import SingleChoiceTask from '../model/SingleChoiceTask';
import TaskEntity from '../model/TaskEntity';
import {
isParentTaskEntity,
isRateableTask,
isSingleChoiceTask,
} from '../utils/TaskUtil';

const PasteFromClipboardDialog = (clipboard, sheets) => {
function onPasteFromClipboard(dispatch) {
const text = clipboard.readText();
try {
const newEntities = YAML.parse(text);
if (newEntities.tasks && newEntities.ratings && newEntities.comments) {
const max = Object.entries<TaskEntity>(newEntities.tasks)
.map(([, v]) => {
if (isParentTaskEntity(v)) {
return 0;
}
if (isRateableTask(v)) {
return (v as RateableTask).max;
}
if (isSingleChoiceTask(v)) {
return (v as SingleChoiceTask).answer.value;
}
return 0;
})
.reduce((acc, v) => acc + v, 0);
const suitableSheet = sheets.find(
(s) => (!s.tasks || s.tasks.length === 0) && s.maxValue === max
);
if (suitableSheet) {
dispatch(schemaSetSelectedSheet(suitableSheet.id));
}
dispatch(schemaSetEntities(newEntities));
dispatch(schemaSetClipboard(text));
}
} catch (error) {}
}

function clearClipborad(dispatch) {
const text = clipboard.readText();
dispatch(schemaSetClipboard(text));
}

return {
title: 'Paste from Clipboard?',
text: `Do you want to paste the correction schema from you clipboard?`,
onConfirm: (dispatch) => {
onPasteFromClipboard(dispatch);
},
onReject: (dispatch) => {
clearClipborad(dispatch);
},
};
};

export default PasteFromClipboardDialog;
22 changes: 22 additions & 0 deletions app/dialogs/StartCorrectionDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { correctionPageSetSheetId } from '../model/CorrectionPageSlice';
import { setTabIndex } from '../model/HomeSlice';
import SheetEntity from '../model/SheetEntity';

const StartCorrectionDialog = (selectedSheet: SheetEntity | undefined) => {
const onStartCorrection = (dispatch) => {
if (selectedSheet?.id !== undefined) {
dispatch(correctionPageSetSheetId(selectedSheet?.id));
dispatch(setTabIndex(3));
}
};

return {
title: 'Start correcting right away?',
text: `Do you want to start correcting the sheet "${selectedSheet?.name}" now?`,
onConfirm: (dispatch) => {
onStartCorrection(dispatch);
},
};
};

export default StartCorrectionDialog;
55 changes: 55 additions & 0 deletions app/effects/CheckClipboardEffect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-empty */
import * as YAML from 'yaml';
import { schemaSetClipboard } from '../model/SchemaSlice';
import ConfirmationDialog from '../dialogs/ConfirmationDialog';
import PasteFromClipboardDialog from '../dialogs/PasteFromClipboardDialog';

const CheckClipboardEffect = (
dispatch,
showModal,
clipboard,
sheets,
clipboardOld,
skipCheck,
setSkipCheck,
entities
) => {
function checkClipboard() {
const text = clipboard.readText();

if (text.trim() === YAML.stringify(entities).trim()) {
dispatch(schemaSetClipboard(text));
}

if (
text.trim().length === 0 ||
text === clipboardOld ||
text.trim() === YAML.stringify(entities).trim()
) {
return;
}
try {
const newEntities = YAML.parse(text);
if (newEntities.tasks && newEntities.ratings && newEntities.comments) {
dispatch(schemaSetClipboard(text));
if (!skipCheck) {
showModal(
ConfirmationDialog,
PasteFromClipboardDialog(clipboard, sheets)
);
} else {
setSkipCheck(false);
}
}
} catch (error) {}
}

return () => {
const id = setInterval(() => checkClipboard(), 1000);
return () => {
clearInterval(id);
};
};
};

export default CheckClipboardEffect;
Loading

0 comments on commit 081ecc9

Please sign in to comment.