1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
3
3
import { Surveys } from 'instabug-reactnative' ;
4
4
5
5
import { ListTile } from '../components/ListTile' ;
6
6
import { 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' ;
7
10
8
11
export 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
+
9
54
return (
10
55
< Screen >
11
56
< ListTile
@@ -16,6 +61,31 @@ export const SurveysScreen: React.FC = () => {
16
61
title = "Show Custom Survey"
17
62
onPress = { ( ) => Surveys . showSurvey ( '6ZaEI4nVdjg19r5uekS5nw' ) }
18
63
/>
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 >
19
89
</ Screen >
20
90
) ;
21
91
} ;
0 commit comments