11import { useContentpassSdk } from './ContentpassContext' ;
22import { Button , ScrollView , StyleSheet , Text , View } from 'react-native' ;
3- import { useCallback , useEffect , useRef , useState } from 'react' ;
4- import type { AuthenticateResult } from 'react-native-contentpass' ;
3+ import { useEffect , useRef , useState } from 'react' ;
4+ import type { ContentpassState } from 'react-native-contentpass' ;
55import {
66 SPConsentManager ,
77 type SPUserData ,
@@ -13,6 +13,11 @@ const styles = StyleSheet.create({
1313 height : 400 ,
1414 flexGrow : 0 ,
1515 } ,
16+ buttonsContainer : {
17+ display : 'flex' ,
18+ gap : 4 ,
19+ marginTop : 10 ,
20+ } ,
1621 logsView : {
1722 marginTop : 10 ,
1823 } ,
@@ -40,21 +45,13 @@ const setupSourcepoint = (hasValidSubscription: boolean) => {
4045} ;
4146
4247export default function ContentpassUsage ( ) {
43- const [ authResult , setAuthResult ] = useState <
44- AuthenticateResult | undefined
45- > ( ) ;
48+ const [ authResult , setAuthResult ] = useState < ContentpassState | undefined > ( ) ;
4649 const contentpassSdk = useContentpassSdk ( ) ;
4750 const spConsentManager = useRef < SPConsentManager | null > ( ) ;
4851 const [ sourcepointUserData , setSourcepointUserData ] = useState <
4952 SPUserData | undefined
5053 > ( ) ;
5154
52- const authenticate = useCallback ( async ( ) => {
53- spConsentManager . current ?. dispose ( ) ;
54- const result = await contentpassSdk . authenticate ( ) ;
55- setAuthResult ( result ) ;
56- } , [ contentpassSdk ] ) ;
57-
5855 useEffect ( ( ) => {
5956 spConsentManager . current = setupSourcepoint (
6057 authResult ?. hasValidSubscription ?? false
@@ -66,7 +63,7 @@ export default function ContentpassUsage() {
6663
6764 spConsentManager . current ?. onAction ( ( action ) => {
6865 if ( action . customActionId === "cp('login')" ) {
69- authenticate ( ) ;
66+ contentpassSdk . authenticate ( ) ;
7067 }
7168 } ) ;
7269
@@ -75,7 +72,18 @@ export default function ContentpassUsage() {
7572 return ( ) => {
7673 spConsentManager . current ?. dispose ( ) ;
7774 } ;
78- } , [ authResult , authenticate ] ) ;
75+ } , [ authResult , contentpassSdk ] ) ;
76+
77+ useEffect ( ( ) => {
78+ const onContentpassStateChange = ( state : ContentpassState ) => {
79+ setAuthResult ( state ) ;
80+ } ;
81+ contentpassSdk . registerObserver ( onContentpassStateChange ) ;
82+
83+ return ( ) => {
84+ contentpassSdk . unregisterObserver ( onContentpassStateChange ) ;
85+ } ;
86+ } , [ contentpassSdk ] ) ;
7987
8088 const clearSourcepointData = ( ) => {
8189 spConsentManager . current ?. clearLocalData ( ) ;
@@ -85,7 +93,13 @@ export default function ContentpassUsage() {
8593
8694 return (
8795 < >
88- < Button title = { 'Clear sourcepoint data' } onPress = { clearSourcepointData } />
96+ < View style = { styles . buttonsContainer } >
97+ < Button
98+ title = { 'Clear sourcepoint data' }
99+ onPress = { clearSourcepointData }
100+ />
101+ < Button title = { 'Logout' } onPress = { contentpassSdk . logout } />
102+ </ View >
89103 < View style = { styles . logsView } >
90104 < Text > Authenticate result:</ Text >
91105 < Text > { JSON . stringify ( authResult , null , 2 ) } </ Text >
0 commit comments