Skip to content

Commit 41157fc

Browse files
feat(example): survey with token actions (#1157)
* feat(example): add survey with token action * refactor: export onPress functions to methods
1 parent ee46cf7 commit 41157fc

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { PropsWithChildren } from 'react';
2+
3+
import { Box, Pressable, Text, VStack } from 'native-base';
4+
5+
interface ListTileProps extends PropsWithChildren {
6+
title: string;
7+
onPress?: () => void;
8+
}
9+
10+
export const VerticalListTile: React.FC<ListTileProps> = ({ title, onPress, children }) => {
11+
return (
12+
<Pressable
13+
onPress={onPress}
14+
p="4"
15+
rounded="2"
16+
shadow="1"
17+
borderBottomWidth="1"
18+
borderColor="coolGray.300"
19+
bg="coolGray.100"
20+
_pressed={{ bg: 'coolGray.200' }}>
21+
<VStack justifyContent="stretch">
22+
<Text>{title}</Text>
23+
<Box>{children}</Box>
24+
</VStack>
25+
</Pressable>
26+
);
27+
};

examples/default/src/screens/SurveysScreen.tsx

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22

33
import { Surveys } from 'instabug-reactnative';
44

55
import { ListTile } from '../components/ListTile';
66
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';
710

811
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+
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

Comments
 (0)