From cdea89e8ac9fd7b5e823aff519b9f1650870cdc5 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 2 Apr 2018 22:17:08 +0900 Subject: [PATCH] refs #100 Swap backward a account in preferences --- src/main/account.js | 19 ++++++++++++++++++- src/main/index.js | 11 +++++++++++ src/renderer/store/Preferences/Account.js | 12 +++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/account.js b/src/main/account.js index 896515af..eb88222f 100644 --- a/src/main/account.js +++ b/src/main/account.js @@ -111,10 +111,27 @@ export default class Account { } ) await this.updateAccount(backwarded._id, Object.assign(backwarded, { order: (backwarded.order + 1) })) - // Forward account order. + // Forward account order const updated = await this.updateAccount(ac._id, Object.assign(ac, { order: (ac.order - 1) })) return updated } + + async backwardAccount (ac) { + const length = await this.countAuthorizedAccounts() + if (ac.order >= length) { + return ac.order + } + // Find account which is forwarded + const forwarded = await this.searchAccount( + { + order: ac.order + 1 + } + ) + await this.updateAccount(forwarded._id, Object.assign(forwarded, { order: (forwarded.order - 1) })) + // Backward account order + const updated = await this.updateAccount(ac._id, Object.assign(ac, { order: (ac.order + 1) })) + return updated + } } class EmptyRecordError { diff --git a/src/main/index.js b/src/main/index.js index 85a4b23a..f9c45f0b 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -320,6 +320,17 @@ ipcMain.on('forward-account', (event, acct) => { }) }) +ipcMain.on('backward-account', (event, acct) => { + const account = new Account(db) + account.backwardAccount(acct) + .then(() => { + event.sender.send('response-backward-account') + }) + .catch((err) => { + event.sender.send('error-backward-account', err) + }) +}) + // streaming let userStreaming = null diff --git a/src/renderer/store/Preferences/Account.js b/src/renderer/store/Preferences/Account.js index 01d21535..82a69c50 100644 --- a/src/renderer/store/Preferences/Account.js +++ b/src/renderer/store/Preferences/Account.js @@ -56,7 +56,17 @@ const Account = { }) }, backwardAccount ({ commit }, account) { - + return new Promise((resolve, reject) => { + ipcRenderer.send('backward-account', account) + ipcRenderer.once('error-backward-account', (event, err) => { + ipcRenderer.removeAllListeners('response-forward-account') + reject(err) + }) + ipcRenderer.once('response-backward-account', (event) => { + ipcRenderer.removeAllListeners('error-backward-account') + resolve() + }) + }) } } }