Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

feat: show discard dialog before overwriting projects #616

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/electron/create-app-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkForUpdates } from './auto-updater';
import { showError } from './show-error';
import { showContextMenu } from './show-context-menu';
import * as Types from '../types';
import { showDiscardDialog, DiscardDialogResult } from './show-discard-dialog';

import {
ServerMessageHandlerContext,
Expand All @@ -16,6 +17,7 @@ export async function createAppMessageHandler(
ctx: ServerMessageHandlerContext,
injection: ServerMessageHandlerInjection
): Promise<(message: Message.Message) => Promise<void>> {
// tslint:disable-next-line:cyclomatic-complexity
return async function appMessageHandler(message: Message.Message): Promise<void> {
switch (message.type) {
case Message.MessageType.CheckForUpdatesRequest: {
Expand Down Expand Up @@ -100,6 +102,49 @@ export async function createAppMessageHandler(
ctx.app = Model.AlvaApp.from(message.payload.app);
break;
}
case Message.MessageType.WindowClose: {
if (!ctx.project || !ctx.project.getDraft()) {
if (ctx.win) {
ctx.win.destroy();
}
return;
}

const result = await showDiscardDialog(ctx.project);

switch (result) {
case DiscardDialogResult.Discard:
injection.ephemeralStore.clear();
ctx.project = undefined;
if (ctx.win) {
ctx.win.hide();
}
break;
case DiscardDialogResult.Save:
const saveResult = await injection.sender.transaction(
{
id: uuid.v4(),
type: Message.MessageType.Save,
payload: { publish: true }
},
{ type: Message.MessageType.SaveResult }
);

if (saveResult.payload.result === Types.PersistenceState.Error) {
return showError(saveResult.payload.result.error);
}

// Give the user some time to realize we saved
setTimeout(() => {
ctx.win && ctx.win.hide();
ctx.project = undefined;
}, 1000);
break;
case DiscardDialogResult.Cancel:
default:
return;
}
}
}
};
}
23 changes: 23 additions & 0 deletions src/electron/create-file-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { showSaveDialog } from './show-save-dialog';
import * as Types from '../types';
import * as Util from 'util';
import * as uuid from 'uuid';
import { showDiscardDialog, DiscardDialogResult } from './show-discard-dialog';
import { showError } from './show-error';

import {
ServerMessageHandlerContext,
Expand All @@ -22,9 +24,30 @@ export async function createFileMessageHandler(
ctx: ServerMessageHandlerContext,
injection: ServerMessageHandlerInjection
): Promise<(message: Message.Message) => Promise<void>> {
// tslint:disable-next-line:cyclomatic-complexity
return async function fileMessageHandler(message: Message.Message): Promise<void> {
switch (message.type) {
case Message.MessageType.CreateNewFileRequest: {
// Prompt the user to save if there has been a project previously
if (ctx.project) {
const discardResult = await showDiscardDialog(ctx.project);

if (discardResult === DiscardDialogResult.Save) {
const saveResult = await injection.sender.transaction(
{
id: uuid.v4(),
type: Message.MessageType.Save,
payload: { publish: true }
},
{ type: Message.MessageType.SaveResult }
);

if (saveResult.payload.result === Types.PersistenceState.Error) {
return showError(saveResult.payload.result.error);
}
}
}

const draftPath = Path.join(ctx.appPath, `${uuid.v4()}.alva`);

ctx.project = Model.Project.create({
Expand Down
54 changes: 9 additions & 45 deletions src/electron/start-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import * as Mobx from 'mobx';
import * as Model from '../model';
import { Sender } from '../sender/server';
import { showMainMenu } from './show-main-menu';
import { showDiscardDialog, DiscardDialogResult } from './show-discard-dialog';
import { showError } from './show-error';
import { createServer } from '../server';
import { createWindow } from './create-window';
import * as Types from '../types';
import * as uuid from 'uuid';

const log = require('electron-log');
Expand Down Expand Up @@ -69,6 +66,15 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event

server.on('client-message', e => sender.send(e));

const onClose = e => {
e.preventDefault();
sender.send({
type: Message.MessageType.WindowClose,
id: uuid.v4(),
payload: undefined
});
};

Electron.app.on('will-finish-launching', () => {
Electron.app.on('open-file', async (event, path) => {
event.preventDefault();
Expand All @@ -90,48 +96,6 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event
process.exit();
});

const onClose = async e => {
if (!ctx.project || !ctx.project.getDraft()) {
return;
}

e.preventDefault();
const result = await showDiscardDialog(ctx.project);

switch (result) {
case DiscardDialogResult.Discard:
ephemeralStore.clear();
ctx.project = undefined;
if (ctx.win) {
ctx.win.hide();
}
break;
case DiscardDialogResult.Save:
const saveResult = await sender.transaction(
{
id: uuid.v4(),
type: Message.MessageType.Save,
payload: { publish: true }
},
{ type: Message.MessageType.SaveResult }
);

if (saveResult.payload.result === Types.PersistenceState.Error) {
return showError(saveResult.payload.result.error);
}

// Give the user some time to realize we saved
setTimeout(() => {
ctx.win && ctx.win.hide();
ctx.project = undefined;
}, 1000);
break;
case DiscardDialogResult.Cancel:
default:
return;
}
};

Electron.app.on('activate', async () => {
if (process.platform === 'darwin' && !ctx.win) {
ctx.win = (await createWindow({ port: ctx.port as number })).window;
Expand Down
7 changes: 5 additions & 2 deletions src/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export enum MessageType {
UpdatePatternLibraryResponse = 'update-pattern-library-response',
ChangeUserStore = 'user-store-change',
SelectElement = 'select-element',
HighlightElement = 'highlight-element'
HighlightElement = 'highlight-element',
WindowClose = 'window-close'
}

export type Message =
Expand Down Expand Up @@ -149,7 +150,8 @@ export type Message =
| UpdatePatternLibraryResponse
| ChangeUserStore
| SelectElement
| HighlightElement;
| HighlightElement
| WindowClose;

export type ActivatePage = Envelope<MessageType.ActivatePage, { id: string }>;
export type AppLoaded = EmptyEnvelope<MessageType.AppLoaded>;
Expand Down Expand Up @@ -419,3 +421,4 @@ export type MobxUpdateMessage = Envelope<MessageType.MobxUpdate, MobxUpdatePaylo
export type MobxAddMessage = Envelope<MessageType.MobxAdd, MobxAddPayload>;
export type MobxDeleteMessage = Envelope<MessageType.MobxDelete, MobxDeletePayload>;
export type MobxSpliceMessage = Envelope<MessageType.MobxSplice, MobxSplicePayload>;
export type WindowClose = EmptyEnvelope<MessageType.WindowClose>;