-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extracted dialoges and effects from SchemaGenerator
- Loading branch information
1 parent
b4dc115
commit 081ecc9
Showing
6 changed files
with
337 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.