// @flow import * as React from 'react'; import {ActivityIndicator, Button, Dialog, Paragraph, Portal} from 'react-native-paper'; import i18n from "i18n-js"; type Props = { visible: boolean, onDismiss: () => void, onAccept: () => Promise, // async function to be executed title: string, titleLoading: string, message: string, startLoading: boolean, } type State = { loading: boolean, } class LoadingConfirmDialog extends React.PureComponent { static defaultProps = { title: '', message: '', onDismiss: () => {}, onAccept: () => {return Promise.resolve()}, startLoading: false, } state = { loading: this.props.startLoading, }; /** * Set the dialog into loading state and closes it when operation finishes */ onClickAccept = () => { this.setState({loading: true}); this.props.onAccept().then(this.hideLoading); }; /** * Waits for fade out animations to finish before hiding loading * @returns {TimeoutID} */ hideLoading = () => setTimeout(() => { this.setState({loading: false}) }, 200); /** * Hide the dialog if it is not loading */ onDismiss = () => { if (!this.state.loading) this.props.onDismiss(); }; render() { return ( {this.state.loading ? this.props.titleLoading : this.props.title} {this.state.loading ? : {this.props.message} } {this.state.loading ? null : } ); } } export default LoadingConfirmDialog;