1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22
33import { Surveys } from 'instabug-reactnative' ;
44
55import { ListTile } from '../components/ListTile' ;
66import { Screen } from '../components/Screen' ;
7+ import { VerticalListTile } from '../components/VerticalListTile' ;
8+ import { Button , Input , Text , useToast , VStack } from 'native-base' ;
9+ import { StyleSheet , View } from 'react-native' ;
710
811export const SurveysScreen : React . FC = ( ) => {
12+ const [ surveyToken , setSurveyToken ] = useState ( '' ) ;
13+ const toast = useToast ( ) ;
14+ const [ userAttributesFormError , setUserAttributesFormError ] = useState < any > ( { } ) ;
15+
16+ const validateUserAttributeForm = ( ) => {
17+ const errors : any = { } ;
18+ if ( surveyToken . length === 0 ) {
19+ errors . surveyToken = 'Value is required' ;
20+ }
21+
22+ setUserAttributesFormError ( errors ) ;
23+ return Object . keys ( errors ) . length === 0 ;
24+ } ;
25+ const styles = StyleSheet . create ( {
26+ inputWrapper : {
27+ padding : 4 ,
28+ flex : 1 ,
29+ } ,
30+ errorText : {
31+ color : '#ff0000' ,
32+ } ,
33+ formContainer : {
34+ flexDirection : 'row' ,
35+ alignItems : 'stretch' ,
36+ } ,
37+ } ) ;
38+
39+ const checkIfUserHasResponded = async ( ) => {
40+ if ( validateUserAttributeForm ( ) ) {
41+ const hasResponded = await Surveys . hasRespondedToSurvey ( surveyToken ) ;
42+ toast . show ( {
43+ description : hasResponded ? 'YES' : 'NO' ,
44+ } ) ;
45+ }
46+ } ;
47+
48+ const showSurveyWithToken = ( ) => {
49+ if ( validateUserAttributeForm ( ) ) {
50+ Surveys . showSurvey ( surveyToken ) ;
51+ }
52+ } ;
53+
954 return (
1055 < Screen >
1156 < ListTile
@@ -16,6 +61,31 @@ export const SurveysScreen: React.FC = () => {
1661 title = "Show Custom Survey"
1762 onPress = { ( ) => Surveys . showSurvey ( '6ZaEI4nVdjg19r5uekS5nw' ) }
1863 />
64+
65+ < VerticalListTile title = "Survey token actions" >
66+ < VStack >
67+ < View style = { styles . formContainer } >
68+ < View style = { styles . inputWrapper } >
69+ < Input
70+ placeholder = "Survey token"
71+ onChangeText = { ( key ) => setSurveyToken ( key ) }
72+ defaultValue = { surveyToken }
73+ />
74+ { userAttributesFormError . surveyToken ? (
75+ < Text style = { styles . errorText } > { userAttributesFormError . surveyToken } </ Text >
76+ ) : null }
77+ </ View >
78+ </ View >
79+
80+ < Button mt = "4" onPress = { showSurveyWithToken } >
81+ Show survey
82+ </ Button >
83+
84+ < Button mt = "4" onPress = { checkIfUserHasResponded } >
85+ If User has responded survey
86+ </ Button >
87+ </ VStack >
88+ </ VerticalListTile >
1989 </ Screen >
2090 ) ;
2191} ;
0 commit comments