Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation support to show-about-modal-dialog module #457

26 changes: 26 additions & 0 deletions build/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
"confirmButtonText": "OK"
}
},
"showAboutModalDialog": {
"confirmButtonText": "OK",
"copyButtonText": "Copy",
"gitHubButtonText": "Open GitHub"
},
"showMessageModalDialog": {
"confirmButtonText": "OK",
"cancelButtonText": "Cancel"
Expand Down Expand Up @@ -192,6 +197,27 @@
"removingDBDescription": "Removing DB ...",
"clearingAllDataDescription": "Clearing all data ..."
}
},
"backupDB": {
"backupDBTitle": "DB backup",
"dbBackupHasCompletedMessage": "DB backup has completed successfully",
"dbBackupHasFailedMessage": "DB backup has failed",
"confirmButtonText": "OK"
},
"migrationDB": {
"messageModalDialog": {
"dbMigrationHasFailedMessage": "DВ migration has failed",
"dbMigrationHasCompletedMessage": "DB migration has completed successfully",
"confirmButtonText": "OK",
"cancelButtonText": "Cancel"
},
"actionRequestModalDialog": {
"title": "The migration has failed",
"message": "What should be done?",
"exitButtonText": "Exit",
"restoreDBButtonText": "Try to restore DB",
"removeDBButtonText": "Remove DB"
}
}
},
"menu": {
Expand Down
26 changes: 26 additions & 0 deletions build/locales/ru/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
"confirmButtonText": "OK"
}
},
"showAboutModalDialog": {
"confirmButtonText": "OK",
"copyButtonText": "Копировать",
"gitHubButtonText": "Открыть GitHub"
},
"showMessageModalDialog": {
"confirmButtonText": "OK",
"cancelButtonText": "Отмена"
Expand Down Expand Up @@ -192,6 +197,27 @@
"removingDBDescription": "Удаление БД ...",
"clearingAllDataDescription": "Очистка всех данные ..."
}
},
"backupDB": {
"backupDBTitle": "Резервное копирование БД",
"dbBackupHasCompletedMessage": "Резервное копирование БД успешно завершено",
"dbBackupHasFailedMessage": "Резервное копирование БД не удалось",
"confirmButtonText": "OK"
},
"migrationDB": {
"messageModalDialog": {
"dbMigrationHasFailedMessage": "Миграция БД не удалась",
"dbMigrationHasCompletedMessage": "Миграция БД успешно завершена",
"confirmButtonText": "OK",
"cancelButtonText": "Отмена"
},
"actionRequestModalDialog": {
"title": "Миграция не удалась",
"message": "Что следует сделать?",
"exitButtonText": "Выход",
"restoreDBButtonText": "Попробовать восстановить БД",
"removeDBButtonText": "Удалить БД"
}
}
},
"menu": {
Expand Down
122 changes: 77 additions & 45 deletions src/manage-worker-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const showMessageModalDialog = require(
const isMainWinAvailable = require(
'./helpers/is-main-win-available'
)
const {
showWindow
} = require('./helpers/manage-window')
const showTrxTaxReportNotification = require(
'./show-notification/show-trx-tax-report-notification'
)
Expand All @@ -27,17 +24,46 @@ const PROCESS_STATES = require(
'../bfx-reports-framework/workers/loc.api/process.message.manager/process.states'
)

module.exports = (ipc) => {
const win = isMainWinAvailable(wins.mainWindow)
? wins.mainWindow
: BrowserWindow.getFocusedWindow()
const modalDialogPromiseSet = new Set()

const resolveModalDialogInSequence = async (asyncHandler) => {
let resolve = () => {}
const promise = new Promise((_resolve) => {
resolve = _resolve
})

const promisesForAwaiting = [...modalDialogPromiseSet]
modalDialogPromiseSet.add(promise)
await Promise.all(promisesForAwaiting)
const res = await asyncHandler()
resolve()
modalDialogPromiseSet.delete(promise)
return res
}

const getParentWindow = () => {
if (isMainWinAvailable(
wins.mainWindow,
{ shouldCheckVisibility: true }
)) {
return wins.mainWindow
}
if (isMainWinAvailable(wins.loadingWindow)) {
return wins.loadingWindow
}

return BrowserWindow.getFocusedWindow()
}

module.exports = (ipc) => {
if (!ipc) {
return
}

ipc.on('message', async (mess) => {
try {
const win = getParentWindow()

if (
!mess ||
typeof mess !== 'object' ||
Expand All @@ -61,23 +87,22 @@ module.exports = (ipc) => {
state === PROCESS_MESSAGES.DB_HAS_BEEN_RESTORED ||
state === PROCESS_MESSAGES.DB_HAS_NOT_BEEN_RESTORED
) {
await showWindow(win)

const hasNotDbBeenRestored = state === PROCESS_MESSAGES.DB_HAS_NOT_BEEN_RESTORED
const type = hasNotDbBeenRestored
? 'error'
: 'info'

await showMessageModalDialog(win, {
await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type,
title: i18next.t('common.restoreDB.messageModalDialog.title'),
message: hasNotDbBeenRestored
? i18next.t('common.restoreDB.messageModalDialog.dbNotRestoredMessage')
: i18next.t('common.restoreDB.messageModalDialog.dbRestoredMessage'),
buttons: [i18next.t('common.restoreDB.messageModalDialog.confirmButtonText')],
defaultId: 0,
cancelId: 0
})
cancelId: 0,
shouldParentWindowBeShown: true
}))

// To enforce migration launch if restores prev db schema version
if (data?.isNotVerSupported) {
Expand All @@ -90,8 +115,6 @@ module.exports = (ipc) => {
state === PROCESS_MESSAGES.ALL_TABLE_HAVE_BEEN_REMOVED ||
state === PROCESS_MESSAGES.ALL_TABLE_HAVE_NOT_BEEN_REMOVED
) {
await showWindow(win)

const haveNotAllDbDataBeenRemoved = state === PROCESS_MESSAGES.ALL_TABLE_HAVE_NOT_BEEN_REMOVED
const message = haveNotAllDbDataBeenRemoved
? i18next.t('common.removeDB.messageModalDialog.dbDataHasNotBeenRemovedMessage')
Expand All @@ -100,41 +123,43 @@ module.exports = (ipc) => {
? 'error'
: 'info'

await showMessageModalDialog(win, {
await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type,
title: i18next.t('common.removeDB.messageModalDialog.dbRemovingTitle'),
message,
buttons: [
i18next.t('common.removeDB.messageModalDialog.confirmButtonText')
],
defaultId: 0,
cancelId: 0
})
cancelId: 0,
shouldParentWindowBeShown: true
}))

return
}
if (
state === PROCESS_MESSAGES.BACKUP_FINISHED ||
state === PROCESS_MESSAGES.ERROR_BACKUP
) {
await showWindow(win)

const isBackupError = state === PROCESS_MESSAGES.ERROR_BACKUP
const messChunk = isBackupError
? ' not'
: ''
const message = isBackupError
? i18next.t('common.backupDB.dbBackupHasFailedMessage')
: i18next.t('common.backupDB.dbBackupHasCompletedMessage')
const type = isBackupError
? 'error'
: 'info'

await showMessageModalDialog(win, {
await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type,
title: 'DB backup',
message: `DB backup has${messChunk} been successfully`,
buttons: ['OK'],
title: i18next.t('common.backupDB.backupDBTitle'),
message,
buttons: [
i18next.t('common.backupDB.confirmButtonText')
],
defaultId: 0,
cancelId: 0
})
cancelId: 0,
shouldParentWindowBeShown: true
}))

if (isBackupError) {
return
Expand Down Expand Up @@ -170,38 +195,44 @@ module.exports = (ipc) => {
state === PROCESS_MESSAGES.ERROR_MIGRATIONS ||
state === PROCESS_MESSAGES.READY_MIGRATIONS
) {
await showWindow(win)

const isMigrationsError = state === PROCESS_MESSAGES.ERROR_MIGRATIONS
const type = isMigrationsError
? 'error'
: 'info'
const message = isMigrationsError
? 'DВ migration failed, all data has been deleted,\nsynchronization will start from scratch'
: 'DB migration completed successfully'
? i18next.t('common.migrationDB.messageModalDialog.dbMigrationHasFailedMessage')
: i18next.t('common.migrationDB.messageModalDialog.dbMigrationHasCompletedMessage')
const buttons = isMigrationsError
? ['Cancel']
: ['OK']
? [i18next.t('common.migrationDB.messageModalDialog.cancelButtonText')]
: [i18next.t('common.migrationDB.messageModalDialog.confirmButtonText')]

await showMessageModalDialog(win, {
await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type,
message,
buttons,
defaultId: 0,
cancelId: 0
})
cancelId: 0,
shouldParentWindowBeShown: true
}))

return
}
if (state === PROCESS_MESSAGES.REQUEST_MIGRATION_HAS_FAILED_WHAT_SHOULD_BE_DONE) {
const {
btnId
} = await showMessageModalDialog(win, {
} = await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type: 'question',
title: 'The migration has failed',
message: 'What should be done?',
buttons: ['Exit', 'Try to restore DB', 'Remove DB']
})
title: i18next
.t('common.migrationDB.actionRequestModalDialog.title'),
message: i18next
.t('common.migrationDB.actionRequestModalDialog.message'),
buttons: [
i18next.t('common.migrationDB.actionRequestModalDialog.exitButtonText'),
i18next.t('common.migrationDB.actionRequestModalDialog.restoreDBButtonText'),
i18next.t('common.migrationDB.actionRequestModalDialog.removeDBButtonText')
],
shouldParentWindowBeShown: true
}))

if (btnId === 0) {
app.quit()
Expand All @@ -226,15 +257,16 @@ module.exports = (ipc) => {

const {
btnId
} = await showMessageModalDialog(win, {
} = await resolveModalDialogInSequence(() => showMessageModalDialog(win, {
type: 'question',
title,
message: i18next.t('common.removeDB.messageModalDialog.removeDBMessage'),
buttons: [
i18next.t('common.removeDB.messageModalDialog.cancelButtonText'),
i18next.t('common.removeDB.messageModalDialog.confirmButtonText')
]
})
],
shouldParentWindowBeShown: true
}))

if (btnId === 0) {
if (data.isNotDbRestored) {
Expand Down
7 changes: 6 additions & 1 deletion src/show-about-modal-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
BrowserWindow
} = require('electron')
const path = require('path')
const i18next = require('i18next')

const getDebugInfo = require('./helpers/get-debug-info')

Expand All @@ -32,7 +33,11 @@ module.exports = () => {
title: productName,
message: productName,
detail,
buttons: ['Copy', 'GitHub', 'OK'],
buttons: [
i18next.t('common.showAboutModalDialog.copyButtonText'),
i18next.t('common.showAboutModalDialog.gitHubButtonText'),
i18next.t('common.showAboutModalDialog.confirmButtonText')
],
defaultId: 2,
cancelId: 2,
icon: path.join(__dirname, '../build/icons/64x64.png')
Expand Down
7 changes: 7 additions & 0 deletions src/show-message-modal-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const i18next = require('i18next')

const wins = require('./window-creators/windows')
const isMainWinAvailable = require('./helpers/is-main-win-available')
const {
showWindow
} = require('./helpers/manage-window')

module.exports = async (win, opts = {}) => {
const defaultWin = isMainWinAvailable(wins.mainWindow)
Expand All @@ -14,6 +17,10 @@ module.exports = async (win, opts = {}) => {
? win
: defaultWin

if (opts?.shouldParentWindowBeShown) {
await showWindow(win)
}

const {
response: btnId,
checkboxChecked
Expand Down