-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/add confirm before close dialog (#893)
* Feat: add now confirm before close dialog
- Loading branch information
1 parent
e41a04f
commit f1f8325
Showing
7 changed files
with
211 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2015 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { getCurrentWindow } from '@electron/remote'; | ||
|
||
import { ConfirmationDialog } from '../Dialog/Dialog'; | ||
import { AppThunk } from '../store'; | ||
import { | ||
addConfirmBeforeClose, | ||
clearConfirmBeforeClose, | ||
ConfirmBeforeCloseApp, | ||
getNextConfirmDialog, | ||
getShowConfirmCloseDialog, | ||
setShowCloseDialog, | ||
} from './confirmBeforeCloseSlice'; | ||
|
||
export default () => { | ||
const dispatch = useDispatch(); | ||
const [confirmedDialogs, setConfirmedDialogs] = useState< | ||
ConfirmBeforeCloseApp[] | ||
>([]); | ||
|
||
const showCloseDialog = useSelector(getShowConfirmCloseDialog); | ||
const nextConfirmDialog = useSelector(getNextConfirmDialog); | ||
|
||
useEffect(() => { | ||
if (!nextConfirmDialog && showCloseDialog) { | ||
confirmedDialogs.forEach(confirmedDialog => { | ||
if (confirmedDialog.onClose) confirmedDialog.onClose(); | ||
}); | ||
setConfirmedDialogs([]); | ||
getCurrentWindow().close(); | ||
} | ||
}, [nextConfirmDialog, dispatch, showCloseDialog, confirmedDialogs]); | ||
|
||
useEffect(() => { | ||
const action = (ev: BeforeUnloadEvent) => | ||
dispatch<AppThunk>((_, getState) => { | ||
const hasToGetExplicitConform = | ||
getState().confirmBeforeCloseDialog.confirmCloseApp.length > | ||
0; | ||
if (hasToGetExplicitConform) { | ||
dispatch(setShowCloseDialog(true)); | ||
ev.returnValue = true; | ||
} | ||
}); | ||
|
||
window.addEventListener('beforeunload', action, true); | ||
|
||
return () => { | ||
window.removeEventListener('beforeunload', action); | ||
}; | ||
}, [dispatch]); | ||
|
||
return ( | ||
<ConfirmationDialog | ||
headerIcon="alert-outline" | ||
title="Closing nPM PowerUP" | ||
isVisible={showCloseDialog && !!nextConfirmDialog} | ||
onConfirm={() => { | ||
if (nextConfirmDialog) { | ||
setConfirmedDialogs([ | ||
...confirmedDialogs, | ||
nextConfirmDialog, | ||
]); | ||
dispatch(clearConfirmBeforeClose(nextConfirmDialog.id)); | ||
} | ||
}} | ||
onCancel={() => { | ||
dispatch(setShowCloseDialog(false)); | ||
confirmedDialogs.forEach(confirmedDialog => | ||
dispatch(addConfirmBeforeClose(confirmedDialog)) | ||
); | ||
setConfirmedDialogs([]); | ||
}} | ||
> | ||
{nextConfirmDialog?.message} | ||
</ConfirmationDialog> | ||
); | ||
}; |
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,96 @@ | ||
/* | ||
* Copyright (c) 2024 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause | ||
*/ | ||
|
||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
import type { AppThunk, RootState } from '../store'; | ||
|
||
export interface ConfirmBeforeCloseApp { | ||
id: string; | ||
message: React.ReactNode; | ||
onClose?: () => void; | ||
} | ||
|
||
export interface ConfirmBeforeCloseState { | ||
confirmCloseApp: ConfirmBeforeCloseApp[]; | ||
showCloseDialog: boolean; | ||
} | ||
|
||
const initialState: ConfirmBeforeCloseState = { | ||
confirmCloseApp: [], | ||
showCloseDialog: false, | ||
}; | ||
|
||
const slice = createSlice({ | ||
name: 'confirmBeforeCloseDialog', | ||
initialState, | ||
reducers: { | ||
addConfirmBeforeClose( | ||
state, | ||
action: PayloadAction<ConfirmBeforeCloseApp> | ||
) { | ||
const index = state.confirmCloseApp.findIndex( | ||
confirmCloseApp => confirmCloseApp.id === action.payload.id | ||
); | ||
|
||
if (index !== -1) { | ||
state.confirmCloseApp[index] = action.payload; | ||
} else { | ||
state.confirmCloseApp = [ | ||
action.payload, | ||
...state.confirmCloseApp, | ||
]; | ||
} | ||
}, | ||
clearConfirmBeforeClose(state, action: PayloadAction<string>) { | ||
state.confirmCloseApp = state.confirmCloseApp.filter( | ||
confirmCloseApp => confirmCloseApp.id !== action.payload | ||
); | ||
}, | ||
setShowCloseDialog(state, action: PayloadAction<boolean>) { | ||
state.showCloseDialog = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { | ||
reducer, | ||
actions: { | ||
addConfirmBeforeClose, | ||
setShowCloseDialog, | ||
clearConfirmBeforeClose, | ||
}, | ||
} = slice; | ||
|
||
export const getNextConfirmDialog = (state: RootState) => | ||
state.confirmBeforeCloseDialog.confirmCloseApp.length > 0 | ||
? state.confirmBeforeCloseDialog.confirmCloseApp[0] | ||
: undefined; | ||
|
||
export const getShowConfirmCloseDialog = (state: RootState) => | ||
state.confirmBeforeCloseDialog.showCloseDialog; | ||
|
||
export const preventAppCloseUntilComplete = | ||
( | ||
dialogInfo: Omit<ConfirmBeforeCloseApp, 'id'>, | ||
promise: Promise<unknown>, | ||
abortController?: AbortController | ||
): AppThunk => | ||
dispatch => { | ||
const id = uuid(); | ||
dispatch( | ||
addConfirmBeforeClose({ | ||
...dialogInfo, | ||
id, | ||
onClose: () => { | ||
dialogInfo.onClose?.(); | ||
abortController?.abort(); | ||
}, | ||
}) | ||
); | ||
promise.finally(() => dispatch(clearConfirmBeforeClose(id))); | ||
}; |
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
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