Add confirm dialog

This commit is contained in:
Pierre HUBERT 2021-05-10 17:12:52 +02:00
parent 10e0440a5a
commit 4b9977b93f
2 changed files with 64 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import './App.css'; import './App.css';
import { Button, createMuiTheme, ThemeProvider } from '@material-ui/core'; 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() { function App() {
const darkTheme = createMuiTheme({ const darkTheme = createMuiTheme({
@ -19,6 +19,7 @@ function App() {
{/* TODO : holder of login state */} {/* TODO : holder of login state */}
<Button onClick={()=>matAlert("heloo!!")}>alert</Button> <Button onClick={()=>matAlert("heloo!!")}>alert</Button>
<Button onClick={()=>matConfirm("heloo!!").then((r) => matAlert("response: " + r))}>confirm</Button>
</div> </div>
</ThemeProvider> </ThemeProvider>
); );

View File

@ -15,27 +15,41 @@ interface AppDiagProvState {
alertMessage : string, alertMessage : string,
showAlert : boolean, showAlert : boolean,
// Confirm dialog
confirmMessage: string,
showConfirm: boolean,
resolveConfirm ?: (confirmed: boolean) => void,
} }
export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> { export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> {
private acceptConfirm: () => void;
private rejectConfirm: () => void;
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.setState({ this.state = {
// Alert dialog // Alert dialog
alertMessage: "", alertMessage: "",
showAlert: false, showAlert: false,
});
showConfirm: false,
confirmMessage: "",
resolveConfirm: undefined,
};
this.handleCloseAlert = this.handleCloseAlert.bind(this); this.handleCloseAlert = this.handleCloseAlert.bind(this);
this.acceptConfirm = this.handleCloseConfirm.bind(this, true);
this.rejectConfirm = this.handleCloseConfirm.bind(this, false);
} }
showAlert(message: string) { showAlert(message: string) {
this.setState({ this.setState({
showAlert: true, showAlert: true,
alertMessage: message alertMessage: message,
}) })
} }
@ -43,6 +57,24 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
this.setState({showAlert: false}); this.setState({showAlert: false});
} }
showConfirm(message: string) : Promise<boolean> {
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() { render() {
cache = this; cache = this;
@ -70,10 +102,37 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
{/* Confirm dialog */}
<Dialog
open={this.state.showConfirm}
onClose={this.rejectConfirm}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title" style={{minWidth: "300px"}}>Confirm action</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
{this.state.confirmMessage}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.rejectConfirm} color="default">
Cancel
</Button>
<Button onClick={this.acceptConfirm} color="default">
Confirm
</Button>
</DialogActions>
</Dialog>
</div>) </div>)
} }
} }
export function matAlert(msg: string) { export function matAlert(msg: string) {
cache.showAlert(msg); cache.showAlert(msg);
}
export function matConfirm(msg: string) : Promise<boolean> {
return cache.showConfirm(msg);
} }