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

Show native notification for sync if processing with hidden main window #390

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
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const allowedProcessMessagesSet = _getAllowedStatesSet({
'READY_TRX_TAX_REPORT',
'ERROR_TRX_TAX_REPORT',

'READY_SYNC',
'ERROR_SYNC',

'ALL_TABLE_HAVE_BEEN_CLEARED',
'ALL_TABLE_HAVE_NOT_BEEN_CLEARED',

Expand Down
10 changes: 9 additions & 1 deletion src/helpers/manage-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ const centerWindow = (win, workArea) => {
win.setBounds(boundsOpts)
}

const isWindowInvisible = (win) => {
return (
!win?.isVisible() ||
!win?.isFocused()
)
}

module.exports = {
hideWindow,
showWindow,
centerWindow
centerWindow,
isWindowInvisible
}
35 changes: 18 additions & 17 deletions src/manage-worker-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ const showMessageModalDialog = require(
const isMainWinAvailable = require(
'./helpers/is-main-win-available'
)
const { showWindow } = require('./helpers/manage-window')
const showNotification = require('./show-notification')
const {
showWindow
} = require('./helpers/manage-window')
const showTrxTaxReportNotification = require(
'./show-notification/show-trx-tax-report-notification'
)
const showSyncNotification = require(
'./show-notification/show-sync-notification'
)
const PROCESS_MESSAGES = require(
'../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages'
)
Expand Down Expand Up @@ -238,22 +245,16 @@ module.exports = (ipc) => {
ipc.send({ state: PROCESS_STATES.REMOVE_ALL_TABLES })
}
if (
(
state === PROCESS_MESSAGES.READY_TRX_TAX_REPORT ||
state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT
) &&
(
!wins?.mainWindow?.isVisible() ||
!wins?.mainWindow?.isFocused()
)
state === PROCESS_MESSAGES.READY_TRX_TAX_REPORT ||
state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT
) {
const isError = state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT
const body = isError
? 'An unexpected error occurred while generating the tax report!'
: 'Your tax report is ready!'
const urgency = isError ? 'critical' : 'normal'

showNotification({ body, urgency })
showTrxTaxReportNotification(mess)
}
if (
state === PROCESS_MESSAGES.READY_SYNC ||
state === PROCESS_MESSAGES.ERROR_SYNC
) {
showSyncNotification(mess)
}
} catch (err) {
console.error(err)
Expand Down
43 changes: 43 additions & 0 deletions src/show-notification/show-sync-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const PROCESS_MESSAGES = require(
'../../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages'
)
const wins = require('../windows')
const { isWindowInvisible } = require('../helpers/manage-window')
const showNotification = require('./')

const getBody = (params) => {
const {
isError,
isInterrupted
} = params ?? {}

if (isError) {
return 'Data sync completed with an error!'
}
if (isInterrupted) {
return 'Data sync interrupted!'
}

return 'Data sync completed successfully!'
}

module.exports = (mess) => {
const {
state = '',
data = {}
} = mess ?? {}

if (!isWindowInvisible(wins?.mainWindow)) {
return
}

const isError = state === PROCESS_MESSAGES.ERROR_SYNC
const isInterrupted = !!data?.isInterrupted

const body = getBody({ isError, isInterrupted })
const urgency = isError ? 'critical' : 'normal'

showNotification({ body, urgency })
}
26 changes: 26 additions & 0 deletions src/show-notification/show-trx-tax-report-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const PROCESS_MESSAGES = require(
'../../bfx-reports-framework/workers/loc.api/process.message.manager/process.messages'
)
const wins = require('../windows')
const { isWindowInvisible } = require('../helpers/manage-window')
const showNotification = require('./')

module.exports = (mess) => {
const {
state = ''
} = mess ?? {}

if (!isWindowInvisible(wins?.mainWindow)) {
return
}

const isError = state === PROCESS_MESSAGES.ERROR_TRX_TAX_REPORT
const body = isError
? 'An unexpected error occurred while generating the tax report!'
: 'Your tax report is ready!'
const urgency = isError ? 'critical' : 'normal'

showNotification({ body, urgency })
}