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