From 4b9977b93f342c85f6ea90d984a91c62f52b02df Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 10 May 2021 17:12:52 +0200 Subject: [PATCH] Add confirm dialog --- src/App.tsx | 3 +- src/ui/widgets/DialogsProvider.tsx | 65 ++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a4adbdc..92b96eb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import React from 'react'; import './App.css'; import { Button, createMuiTheme, ThemeProvider } from '@material-ui/core'; -import { ApplicationDialogsProvider, matAlert } from './ui/widgets/DialogsProvider'; +import { ApplicationDialogsProvider, matAlert, matConfirm } from './ui/widgets/DialogsProvider'; function App() { const darkTheme = createMuiTheme({ @@ -19,6 +19,7 @@ function App() { {/* TODO : holder of login state */} + ); diff --git a/src/ui/widgets/DialogsProvider.tsx b/src/ui/widgets/DialogsProvider.tsx index 418f50e..7caadc6 100644 --- a/src/ui/widgets/DialogsProvider.tsx +++ b/src/ui/widgets/DialogsProvider.tsx @@ -15,27 +15,41 @@ interface AppDiagProvState { alertMessage : string, showAlert : boolean, + // Confirm dialog + confirmMessage: string, + showConfirm: boolean, + resolveConfirm ?: (confirmed: boolean) => void, + } export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> { + private acceptConfirm: () => void; + private rejectConfirm: () => void; constructor(props: any) { super(props); - this.setState({ + this.state = { // Alert dialog alertMessage: "", showAlert: false, - }); + + showConfirm: false, + confirmMessage: "", + resolveConfirm: undefined, + }; this.handleCloseAlert = this.handleCloseAlert.bind(this); + + this.acceptConfirm = this.handleCloseConfirm.bind(this, true); + this.rejectConfirm = this.handleCloseConfirm.bind(this, false); } showAlert(message: string) { this.setState({ showAlert: true, - alertMessage: message + alertMessage: message, }) } @@ -43,6 +57,24 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS this.setState({showAlert: false}); } + showConfirm(message: string) : Promise { + return new Promise((res, _rej) => { + this.setState({ + showConfirm: true, + confirmMessage: message, + resolveConfirm: res + }); + }); + } + + handleCloseConfirm(accept: boolean) { + this.setState({ + showConfirm: false + }); + + this.state.resolveConfirm && this.state.resolveConfirm(accept); + } + render() { cache = this; @@ -70,10 +102,37 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS + + {/* Confirm dialog */} + + Confirm action + + + {this.state.confirmMessage} + + + + + + + ) } } export function matAlert(msg: string) { cache.showAlert(msg); +} + +export function matConfirm(msg: string) : Promise { + return cache.showConfirm(msg); } \ No newline at end of file