|
| 1 | +import * as React from 'react'; |
| 2 | +import { Modal, StyleSheet,View } from 'react-native'; |
| 3 | + |
| 4 | +import { FeedbackForm } from './FeedbackForm'; |
| 5 | + |
| 6 | +class FeedbackFormManager { |
| 7 | + private static _isVisible = false; |
| 8 | + private static _setVisibility: (visible: boolean) => void; |
| 9 | + |
| 10 | + public static initialize(setVisibility: (visible: boolean) => void): void { |
| 11 | + this._setVisibility = setVisibility; |
| 12 | + } |
| 13 | + |
| 14 | + public static show(): void { |
| 15 | + if (this._setVisibility) { |
| 16 | + this._isVisible = true; |
| 17 | + this._setVisibility(true); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public static hide(): void { |
| 22 | + if (this._setVisibility) { |
| 23 | + this._isVisible = false; |
| 24 | + this._setVisibility(false); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public static isFormVisible(): boolean { |
| 29 | + return this._isVisible; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +interface FeedbackFormProviderProps { |
| 34 | + children: React.ReactNode; |
| 35 | +} |
| 36 | + |
| 37 | +class FeedbackFormProvider extends React.Component<FeedbackFormProviderProps> { |
| 38 | + public state = { |
| 39 | + isVisible: false, |
| 40 | + }; |
| 41 | + |
| 42 | + public constructor(props: FeedbackFormProviderProps) { |
| 43 | + super(props); |
| 44 | + FeedbackFormManager.initialize(this._setVisibilityFunction); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Renders the feedback form modal. |
| 49 | + */ |
| 50 | + public render(): React.ReactNode { |
| 51 | + const { isVisible } = this.state; |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + {this.props.children} |
| 56 | + {isVisible && ( |
| 57 | + <Modal visible={isVisible} transparent animationType="slide"> |
| 58 | + <View style={styles.modalBackground}> |
| 59 | + <FeedbackForm |
| 60 | + onFormClose={this._handleClose} |
| 61 | + onFormSubmitted={this._handleClose} |
| 62 | + /> |
| 63 | + </View> |
| 64 | + </Modal> |
| 65 | + )} |
| 66 | + </> |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + private _setVisibilityFunction = (visible: boolean): void => { |
| 71 | + this.setState({ isVisible: visible }); |
| 72 | + }; |
| 73 | + |
| 74 | + private _handleClose = (): void => { |
| 75 | + FeedbackFormManager.hide(); |
| 76 | + this.setState({ isVisible: false }); |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +const showFeedbackForm = (): void => { |
| 81 | + FeedbackFormManager.show(); |
| 82 | +}; |
| 83 | + |
| 84 | +const styles = StyleSheet.create({ |
| 85 | + modalBackground: { |
| 86 | + flex: 1, |
| 87 | + justifyContent: 'center', |
| 88 | + backgroundColor: 'rgba(0,0,0,0.5)', |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +export { showFeedbackForm, FeedbackFormProvider }; |
0 commit comments