-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show confirmation dialog before leaving edit mode in some cases
- Loading branch information
1 parent
cbe5d26
commit 731b92c
Showing
12 changed files
with
223 additions
and
17 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,8 @@ | ||
import {CloseEditEntry} from '../../../../domain/useCases/entries/CloseEditEntry'; | ||
import {container} from '../../container'; | ||
|
||
const useCase: CloseEditEntry = container.get(CloseEditEntry); | ||
|
||
export function closeEditEntry(): Promise<void> { | ||
return useCase.execute(); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {SelectFile} from '../../../../domain/useCases/fileTree/SelectFile'; | ||
import {container} from '../../container'; | ||
import {confirmLeaveEditMode} from '../../helpers/confirmations/confirmLeaveEditMode'; | ||
|
||
const useCase: SelectFile = container.get(SelectFile); | ||
|
||
export function selectFile(filename: string): Promise<void> { | ||
return useCase.execute(filename); | ||
return confirmLeaveEditMode(() => { | ||
return useCase.execute(filename); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {SelectFileVariant} from '../../../../domain/useCases/fileTree/SelectFileVariant'; | ||
import {container} from '../../container'; | ||
import {confirmLeaveEditMode} from '../../helpers/confirmations/confirmLeaveEditMode'; | ||
|
||
const useCase: SelectFileVariant = container.get(SelectFileVariant); | ||
|
||
export function selectFileVariant(basename: string, variantId: string): Promise<void> { | ||
return useCase.execute(basename, variantId); | ||
return confirmLeaveEditMode(() => { | ||
return useCase.execute(basename, variantId); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {Reload} from '../../../../domain/useCases/project/Reload'; | ||
import {container} from '../../container'; | ||
import {confirmLeaveEditMode} from '../../helpers/confirmations/confirmLeaveEditMode'; | ||
|
||
const useCase: Reload = container.get(Reload); | ||
|
||
export function reload(): Promise<void> { | ||
return useCase.execute(); | ||
return confirmLeaveEditMode(() => { | ||
return useCase.execute(); | ||
}); | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
src/app/renderer/helpers/confirmations/confirmLeaveEditMode.ts
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,38 @@ | ||
import {remote} from 'electron'; | ||
import {closeCreateEntry} from '../../actions/entries/closeCreateEntry'; | ||
import {closeEditEntry} from '../../actions/entries/closeEditEntry'; | ||
import {closeCreateVariant} from '../../actions/variants/closeCreateVariant'; | ||
import {isEditing} from '../../queries/isEditing'; | ||
|
||
export function confirmLeaveEditMode<R = void>(onOk: Function): Promise<R | void> { | ||
return new Promise((resolve) => { | ||
if (!isEditing()) { | ||
resolve(onOk()); | ||
return; | ||
} | ||
|
||
remote.dialog.showMessageBox( | ||
{ | ||
type: 'warning', | ||
title: 'Leave Edit Mode?', | ||
message: 'You are currently in edit mode. If you proceed, your current form changes will be lost. Do you want to proceed?', | ||
buttons: ['Oh, wait!', 'Yes, leave edit mode'], | ||
cancelId: 0, | ||
defaultId: 1, | ||
}, | ||
(buttonId: number) => { | ||
if (buttonId == 1) { | ||
Promise.all([ | ||
closeCreateVariant(), | ||
closeCreateEntry(), | ||
closeEditEntry(), | ||
]).then(() => { | ||
resolve(onOk()); | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}, | ||
); | ||
}); | ||
} |
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,8 @@ | ||
import {IsEditing} from '../../../domain/stateQueries/IsEditing'; | ||
import {container} from '../container'; | ||
|
||
const query: IsEditing = container.get(IsEditing); | ||
|
||
export function isEditing(): boolean { | ||
return query.fetch(); | ||
} |
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,32 @@ | ||
import {injectable} from 'inversify'; | ||
import {EditorState} from '../states/EditorState'; | ||
import {ActiveFile} from '../states/objects/editor/ActiveFile'; | ||
import {DataEntry} from '../states/objects/editor/DataEntry'; | ||
|
||
@injectable() | ||
export class IsEditing { | ||
|
||
constructor( | ||
private editorState: EditorState, | ||
) { | ||
} | ||
|
||
fetch(): boolean { | ||
if (this.editorState.isAddVariantMode) { | ||
return true; | ||
} | ||
|
||
const currentFile: ActiveFile | null = this.editorState.currentFile; | ||
if (currentFile) { | ||
if (currentFile.isCreateMode) { | ||
return true; | ||
} | ||
|
||
if (currentFile.entries.all.some((entry: DataEntry) => entry.editMode)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,49 @@ | ||
import 'jest'; | ||
import 'reflect-metadata'; | ||
import {EditorState} from '../../states/EditorState'; | ||
import {ActiveFile} from '../../states/objects/editor/ActiveFile'; | ||
import {DataEntries} from '../../states/objects/editor/DataEntries'; | ||
import {DataEntry} from '../../states/objects/editor/DataEntry'; | ||
import {IsEditing} from '../IsEditing'; | ||
|
||
let query: IsEditing; | ||
let editorState: EditorState; | ||
let activeFile: ActiveFile; | ||
|
||
beforeEach(() => { | ||
editorState = new EditorState(); | ||
|
||
activeFile = new ActiveFile('myFile', '/temp/myFile.json'); | ||
editorState.open(activeFile); | ||
|
||
query = new IsEditing(editorState); | ||
}); | ||
|
||
describe('IsEditing', () => { | ||
it('returns false when not editing', () => { | ||
expect(query.fetch()).toEqual(false); | ||
}); | ||
|
||
it('returns true when in create mode', () => { | ||
activeFile.openCreateMode(); | ||
|
||
expect(query.fetch()).toEqual(true); | ||
}); | ||
|
||
it('returns true when in edit mode', () => { | ||
let entry1: DataEntry = new DataEntry(1, {id: 1}); | ||
let entry2: DataEntry = new DataEntry(2, {id: 2}); | ||
|
||
entry1.toggleEditMode(true); | ||
|
||
activeFile.setLoaded({schema: {}, uiSchema: {}}, new DataEntries([entry1, entry2])); | ||
|
||
expect(query.fetch()).toEqual(true); | ||
}); | ||
|
||
it('returns true when in create variant mode', () => { | ||
editorState.setAddVariantMode(true); | ||
|
||
expect(query.fetch()).toEqual(true); | ||
}); | ||
}); |
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,24 @@ | ||
import {injectable} from 'inversify'; | ||
import {EditorState} from '../../states/EditorState'; | ||
import {DataEntry} from '../../states/objects/editor/DataEntry'; | ||
|
||
@injectable() | ||
export class CloseEditEntry { | ||
|
||
constructor( | ||
private editorState: EditorState, | ||
) { | ||
} | ||
|
||
execute(): Promise<void> { | ||
if (!this.editorState.currentFile) { | ||
return Promise.resolve(); | ||
} | ||
|
||
this.editorState.currentFile.entries.all.forEach((entry: DataEntry) => { | ||
entry.toggleEditMode(false); | ||
}); | ||
|
||
return Promise.resolve(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/domain/useCases/entries/__tests__/CloseEditEntry.test.ts
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,35 @@ | ||
import 'jest'; | ||
import 'reflect-metadata'; | ||
import {EditorState} from '../../../states/EditorState'; | ||
import {ActiveFile} from '../../../states/objects/editor/ActiveFile'; | ||
import {DataEntries} from '../../../states/objects/editor/DataEntries'; | ||
import {DataEntry} from '../../../states/objects/editor/DataEntry'; | ||
import {CloseEditEntry} from '../CloseEditEntry'; | ||
|
||
let useCase: CloseEditEntry; | ||
let editorState: EditorState; | ||
|
||
beforeEach(() => { | ||
editorState = new EditorState(); | ||
|
||
useCase = new CloseEditEntry(editorState); | ||
}); | ||
|
||
describe('CloseEditEntry', () => { | ||
it('closes edit modes', () => { | ||
let entry1: DataEntry = new DataEntry(1, {id: 1}); | ||
let entry2: DataEntry = new DataEntry(2, {id: 2}); | ||
|
||
entry1.toggleEditMode(true); | ||
entry2.toggleEditMode(true); | ||
|
||
editorState.open(new ActiveFile('myFile', '/temp/myFile.json')); | ||
editorState.currentFile!.setLoaded({schema: {}, uiSchema: {}}, new DataEntries([entry1, entry2])); | ||
|
||
return useCase.execute() | ||
.then(() => { | ||
expect(editorState.currentFile!.entries.getById(1)!.editMode).toBe(false); | ||
expect(editorState.currentFile!.entries.getById(2)!.editMode).toBe(false); | ||
}); | ||
}); | ||
}); |