|
1 | 1 | import { useContentpassSdk } from './ContentpassContext'; |
2 | | -import { Button, Text } from 'react-native'; |
3 | | -import { useState } from 'react'; |
| 2 | +import { Button, ScrollView, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { useEffect, useRef, useState } from 'react'; |
4 | 4 | import type { AuthenticateResult } from 'react-native-contentpass'; |
| 5 | +import { |
| 6 | + SPConsentManager, |
| 7 | + type SPUserData, |
| 8 | +} from '@sourcepoint/react-native-cmp'; |
| 9 | + |
| 10 | +const styles = StyleSheet.create({ |
| 11 | + sourcepointDataContainer: { |
| 12 | + padding: 10, |
| 13 | + height: 400, |
| 14 | + flexGrow: 0, |
| 15 | + }, |
| 16 | +}); |
| 17 | + |
| 18 | +const sourcePointConfig = { |
| 19 | + accountId: 375, |
| 20 | + propertyId: 37858, |
| 21 | + propertyName: 'mobile.cmpsourcepoint.demo', |
| 22 | +}; |
5 | 23 |
|
6 | 24 | export default function ContentpassUsage() { |
7 | 25 | const [authResult, setAuthResult] = useState< |
8 | 26 | AuthenticateResult | undefined |
9 | 27 | >(); |
10 | 28 | const contentpassSdk = useContentpassSdk(); |
| 29 | + const consentManager = useRef<SPConsentManager | null>(); |
| 30 | + const [sourcepointUserData, setSourcepointUserData] = useState< |
| 31 | + SPUserData | undefined |
| 32 | + >(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const { accountId, propertyName, propertyId } = sourcePointConfig; |
| 36 | + consentManager.current = new SPConsentManager(); |
| 37 | + consentManager.current?.build(accountId, propertyId, propertyName, { |
| 38 | + gdpr: { |
| 39 | + targetingParams: { |
| 40 | + acps: authResult?.hasValidSubscription ? 'true' : 'false', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + consentManager.current?.onFinished(() => { |
| 46 | + consentManager.current?.getUserData().then(setSourcepointUserData); |
| 47 | + }); |
| 48 | + |
| 49 | + consentManager.current?.loadMessage(); |
| 50 | + |
| 51 | + return () => { |
| 52 | + try { |
| 53 | + consentManager.current?.dispose(); |
| 54 | + } catch (err) { |
| 55 | + // FIXME: contact sourcepoint as dispose method sometimes crashes app |
| 56 | + } |
| 57 | + }; |
| 58 | + }, [authResult]); |
11 | 59 |
|
12 | 60 | const authenticate = async () => { |
13 | 61 | const result = await contentpassSdk.authenticate(); |
14 | 62 | setAuthResult(result); |
15 | 63 | }; |
16 | 64 |
|
| 65 | + const clearSourcepointData = () => { |
| 66 | + consentManager.current?.clearLocalData(); |
| 67 | + setSourcepointUserData(undefined); |
| 68 | + }; |
| 69 | + |
17 | 70 | return ( |
18 | 71 | <> |
19 | 72 | <Button title={'Authenticate'} onPress={authenticate} /> |
20 | | - <Text>Result: {JSON.stringify(authResult)}</Text> |
| 73 | + <Button title={'Clear sourcepoint data'} onPress={clearSourcepointData} /> |
| 74 | + <View style={{ marginTop: 10 }}> |
| 75 | + <Text>Authenticate result:</Text> |
| 76 | + <Text>{JSON.stringify(authResult, null, 2)}</Text> |
| 77 | + <Text>Sourcepoint user data:</Text> |
| 78 | + <ScrollView style={styles.sourcepointDataContainer}> |
| 79 | + <Text>{JSON.stringify(sourcepointUserData, null, 2)}</Text> |
| 80 | + </ScrollView> |
| 81 | + </View> |
21 | 82 | </> |
22 | 83 | ); |
23 | 84 | } |
0 commit comments