Skip to content

Commit 14df125

Browse files
authored
Merge pull request #270 from p3ol/feature/switch-to-react-access-v2
✨feat(access): switch example to react access v2 (with access and audit)
2 parents 530f350 + 1944202 commit 14df125

14 files changed

Lines changed: 231 additions & 165 deletions

File tree

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module.exports = {
22
extends: '@poool/eslint-config-react',
33
rules: {
44
'react/prop-types': 0,
5+
'react/react-in-jsx-scope': 0,
56
},
67
};

app/components/App.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import React, { useEffect } from 'react';
1+
import { useEffect } from 'react';
22
import { BrowserRouter, Route, Routes } from 'react-router-dom';
3-
import { PaywallContext } from '@poool/react-access';
3+
import { AccessContext } from '@poool/react-access';
44

55
import { defaultHistory } from '../utils';
66
import Home from './Home';
77
import Premium from './Premium';
88
import Free from './Free';
99
import Subscription from './Subscription';
10-
11-
// Avoid redux
12-
window.testUser = window.testUser || {
13-
logged: false,
14-
premium: false,
15-
};
10+
import Auth from './Auth';
1611

1712
export default () => {
1813
useEffect(() => {
@@ -21,17 +16,24 @@ export default () => {
2116

2217
return (
2318
<BrowserRouter history={defaultHistory}>
24-
<PaywallContext
25-
appId="155PF-L7Q6Q-EB2GG-04TF8"
26-
config={{ debug: true, cookies_enabled: true, custom_segment: 'react' }}
27-
>
28-
<Routes>
29-
<Route path="/premium" element={<Premium />} />
30-
<Route path="/free" element={<Free />} />
31-
<Route path="/subscribe" element={<Subscription />} />
32-
<Route index element={<Home />} />
33-
</Routes>
34-
</PaywallContext>
19+
<Auth>
20+
<AccessContext
21+
appId="155PF-L7Q6Q-EB2GG-04TF8"
22+
config={{
23+
debug: true,
24+
cookies_enabled: true,
25+
custom_segment: 'react',
26+
}}
27+
withAudit={true}
28+
>
29+
<Routes>
30+
<Route path="/premium" element={<Premium />} />
31+
<Route path="/free" element={<Free />} />
32+
<Route path="/subscribe" element={<Subscription />} />
33+
<Route index element={<Home />} />
34+
</Routes>
35+
</AccessContext>
36+
</Auth>
3537
</BrowserRouter>
3638
);
3739
};

app/components/Auth.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useCallback, useState } from 'react';
2+
3+
import { AuthContext } from '../contexts';
4+
5+
export default ({ children }) => {
6+
const [connecting, setConnecting] = useState(false);
7+
const [connected, setConnected] = useState(false);
8+
const [premium, setPremium] = useState(false);
9+
10+
const login = async e => {
11+
e?.preventDefault();
12+
13+
if (connecting) {
14+
return;
15+
}
16+
17+
setConnecting(true);
18+
await new Promise(resolve => setTimeout(resolve, 2000));
19+
20+
setConnecting(false);
21+
setConnected(true);
22+
setPremium(true);
23+
};
24+
25+
const getContext = useCallback(() => ({
26+
login,
27+
connecting,
28+
connected,
29+
premium,
30+
}), [connecting, connected, premium]);
31+
32+
return (
33+
<AuthContext.Provider value={getContext()} children={children} />
34+
);
35+
};

app/components/Free.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
import React, { useEffect } from 'react';
2-
import { usePoool } from '@poool/react-access';
1+
import { useEffect } from 'react';
2+
import { useAudit } from '@poool/react-access';
33

4+
import { useAuth } from '../hooks';
45
import Header from './fragments/Header';
56

67
export default () => {
7-
const { poool, appId, config } = usePoool();
8-
8+
const { lib: audit, config } = useAudit();
9+
const { premium } = useAuth();
910
useEffect(() => {
1011
init();
1112

12-
return () => poool('flush');
13-
}, [poool]);
13+
}, [audit]);
1414

1515
const init = async () => {
16-
poool?.('init', appId);
17-
poool('config', {
16+
audit?.config({
1817
...config,
19-
user_is_premium: window.testUser?.premium || false,
18+
user_is_premium: premium || false,
2019
});
21-
await poool('send', 'page-view', 'free');
20+
await audit?.sendEvent('page-view', 'free');
2221
};
2322

2423
const onLogin = async () => {
25-
await poool?.('flush');
2624
init();
2725
};
2826

app/components/Home.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
import React, { useEffect } from 'react';
1+
import { useEffect } from 'react';
2+
import { useAudit } from '@poool/react-access';
23
import { Link } from 'react-router-dom';
3-
import { usePoool } from '@poool/react-access';
44

5+
import { useAuth } from '../hooks';
56
import Header from './fragments/Header';
67

78
export default () => {
8-
const { poool, appId, config } = usePoool();
9+
const { lib: audit, config } = useAudit();
10+
const { premium } = useAuth();
911

1012
useEffect(() => {
1113
init();
12-
13-
return () => poool?.('flush');
14-
}, [poool]);
14+
}, [audit]);
1515

1616
const init = async () => {
17-
poool?.('init', appId);
18-
poool?.('config', {
17+
audit?.config({
1918
...config,
20-
user_is_premium: window.testUser?.premium || false,
19+
user_is_premium: premium || false,
2120
});
22-
23-
await poool?.('send', 'page-view', 'page');
21+
await audit?.sendEvent('page-view', 'page');
2422
};
2523

2624
const onLogin = async () => {
27-
await poool?.('flush');
2825
init();
2926
};
3027

0 commit comments

Comments
 (0)