|
| 1 | +import { captureFeedback } from '@sentry/core'; |
| 2 | +import type { SendFeedbackParams } from '@sentry/types'; |
| 3 | +import * as React from 'react'; |
| 4 | +import type { KeyboardTypeOptions } from 'react-native'; |
| 5 | +import { Alert, Text, TextInput, TouchableOpacity, View } from 'react-native'; |
| 6 | + |
| 7 | +import { |
| 8 | + CANCEL_BUTTON_LABEL, |
| 9 | + EMAIL_ERROR, |
| 10 | + EMAIL_LABEL, |
| 11 | + EMAIL_PLACEHOLDER, |
| 12 | + ERROR_TITLE, |
| 13 | + FORM_ERROR, |
| 14 | + FORM_TITLE, |
| 15 | + IS_REQUIRED_LABEL, |
| 16 | + MESSAGE_LABEL, |
| 17 | + MESSAGE_PLACEHOLDER, |
| 18 | + NAME_LABEL, |
| 19 | + NAME_PLACEHOLDER, |
| 20 | + SUBMIT_BUTTON_LABEL} from './constants'; |
| 21 | +import defaultStyles from './FeedbackForm.styles'; |
| 22 | +import type { FeedbackFormProps, FeedbackFormState } from './FeedbackForm.types'; |
| 23 | +import LabelText from './LabelText'; |
| 24 | + |
| 25 | +/** |
| 26 | + * @beta |
| 27 | + * Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback. |
| 28 | + */ |
| 29 | +export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFormState> { |
| 30 | + public constructor(props: FeedbackFormProps) { |
| 31 | + super(props); |
| 32 | + this.state = { |
| 33 | + name: '', |
| 34 | + email: '', |
| 35 | + description: '', |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + public handleFeedbackSubmit: () => void = () => { |
| 40 | + const { name, email, description } = this.state; |
| 41 | + const { closeScreen, text } = this.props; |
| 42 | + |
| 43 | + const trimmedName = name?.trim(); |
| 44 | + const trimmedEmail = email?.trim(); |
| 45 | + const trimmedDescription = description?.trim(); |
| 46 | + |
| 47 | + if (!trimmedName || !trimmedEmail || !trimmedDescription) { |
| 48 | + const errorMessage = text?.formError || FORM_ERROR; |
| 49 | + Alert.alert(text?.errorTitle || ERROR_TITLE, errorMessage); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (!this._isValidEmail(trimmedEmail)) { |
| 54 | + const errorMessage = text?.emailError || EMAIL_ERROR; |
| 55 | + Alert.alert(text?.errorTitle || ERROR_TITLE, errorMessage); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const userFeedback: SendFeedbackParams = { |
| 60 | + message: trimmedDescription, |
| 61 | + name: trimmedName, |
| 62 | + email: trimmedEmail, |
| 63 | + }; |
| 64 | + |
| 65 | + captureFeedback(userFeedback); |
| 66 | + closeScreen(); |
| 67 | + }; |
| 68 | + |
| 69 | + /** |
| 70 | + * Renders the feedback form screen. |
| 71 | + */ |
| 72 | + public render(): React.ReactNode { |
| 73 | + const { closeScreen, text, styles } = this.props; |
| 74 | + const { name, email, description } = this.state; |
| 75 | + |
| 76 | + return ( |
| 77 | + <View style={styles?.container || defaultStyles.container}> |
| 78 | + <Text style={styles?.title || defaultStyles.title}>{text?.formTitle || FORM_TITLE}</Text> |
| 79 | + |
| 80 | + <LabelText |
| 81 | + label={text?.nameLabel || NAME_LABEL} |
| 82 | + isRequired={true} |
| 83 | + isRequiredLabel={text?.isRequiredLabel || IS_REQUIRED_LABEL} |
| 84 | + styles={styles?.label || defaultStyles.label} |
| 85 | + /> |
| 86 | + <TextInput |
| 87 | + style={styles?.input || defaultStyles.input} |
| 88 | + placeholder={text?.namePlaceholder || NAME_PLACEHOLDER} |
| 89 | + value={name} |
| 90 | + onChangeText={(value) => this.setState({ name: value })} |
| 91 | + /> |
| 92 | + <LabelText |
| 93 | + label={text?.emailLabel || EMAIL_LABEL} |
| 94 | + isRequired={true} |
| 95 | + isRequiredLabel={text?.isRequiredLabel || IS_REQUIRED_LABEL} |
| 96 | + styles={styles?.label || defaultStyles.label} |
| 97 | + /> |
| 98 | + <TextInput |
| 99 | + style={styles?.input || defaultStyles.input} |
| 100 | + placeholder={text?.emailPlaceholder || EMAIL_PLACEHOLDER} |
| 101 | + keyboardType={'email-address' as KeyboardTypeOptions} |
| 102 | + value={email} |
| 103 | + onChangeText={(value) => this.setState({ email: value })} |
| 104 | + /> |
| 105 | + <LabelText |
| 106 | + label={text?.descriptionLabel || MESSAGE_LABEL} |
| 107 | + isRequired={true} |
| 108 | + isRequiredLabel={text?.isRequiredLabel || IS_REQUIRED_LABEL} |
| 109 | + styles={styles?.label || defaultStyles.label} |
| 110 | + /> |
| 111 | + <TextInput |
| 112 | + style={[styles?.input || defaultStyles.input, styles?.textArea || defaultStyles.textArea]} |
| 113 | + placeholder={text?.descriptionPlaceholder || MESSAGE_PLACEHOLDER} |
| 114 | + value={description} |
| 115 | + onChangeText={(value) => this.setState({ description: value })} |
| 116 | + multiline |
| 117 | + /> |
| 118 | + |
| 119 | + <TouchableOpacity style={styles?.submitButton || defaultStyles.submitButton} onPress={this.handleFeedbackSubmit}> |
| 120 | + <Text style={styles?.submitText || defaultStyles.submitText}>{text?.submitButton || SUBMIT_BUTTON_LABEL}</Text> |
| 121 | + </TouchableOpacity> |
| 122 | + |
| 123 | + <TouchableOpacity style={styles?.cancelButton || defaultStyles.cancelButton} onPress={closeScreen}> |
| 124 | + <Text style={styles?.cancelText || defaultStyles.cancelText}>{text?.cancelButton || CANCEL_BUTTON_LABEL}</Text> |
| 125 | + </TouchableOpacity> |
| 126 | + </View> |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private _isValidEmail = (email: string): boolean => { |
| 131 | + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 132 | + return emailRegex.test(email); |
| 133 | + }; |
| 134 | +} |
0 commit comments