-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.tsx
214 lines (201 loc) · 5.54 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {AuthState, onAuthUIStateChange} from '@aws-amplify/ui-components'
import {CognitoUser} from '@aws-amplify/auth'
import {Button, CircularProgress} from '@mui/material'
import Amplify, {Auth, Hub} from 'aws-amplify'
import React from 'react'
// @ts-ignore
import cytoscape from 'cytoscape'
// @ts-ignore
import euler from 'cytoscape-euler'
// @ts-ignore
import cola from 'cytoscape-cola'
// @ts-ignore
import dagre from 'cytoscape-dagre'
// @ts-ignore
import spread from 'cytoscape-spread'
import GraphEditor, {ACTIONS} from './components/CaseLawExplorer'
import * as API from './components/CaseLawExplorer/API'
import {TermsOfService} from './components/CaseLawExplorer/components/TermsOfService'
import * as R from 'colay/ramda'
import {useMeasure, View} from 'colay-ui'
import {useImmer} from 'colay-ui/hooks/useImmer'
import {Signin} from './components/CaseLawExplorer/Signin'
spread(cytoscape)
cytoscape.use(dagre)
cytoscape.use(euler)
cytoscape.use(cola)
export interface UserAttributes {
sub: string
email: string
email_verified: boolean
'custom:isOldUser': 'yes' | null
}
// 2022-11-10
// The aws-auth library is exporting incorrect type signatures for CognitoUser
// (missing an attributes member), leading to type errors. Manually extend it
// here and use the extended type to eliminate the errors until aws-auth is
// fixed.
interface CognitoUserExt extends CognitoUser {
attributes: UserAttributes
}
function getUser(): Promise<CognitoUserExt> {
return Auth.currentAuthenticatedUser().catch(() => console.log('Not signed in'))
}
const runQuery = async () => {
const result = await API.testAuth({
ecli: 'ECLI:NL:HR:2012:BV5128'
})
console.log('API RESULT: ', result)
}
const App = () => {
const dispatch = React.useCallback(action => {
switch (action.type) {
case ACTIONS.TEST_API:
runQuery()
break
default:
break
}
}, [])
const [containerRef, {width, height, initialized}] = useMeasure()
return (
<View
ref={containerRef}
style={{
width: '100%',
height: '100%'
}}
>
{initialized && <GraphEditor dispatch={dispatch} {...{width, height}} />}
</View>
)
}
const AppContainer = () => {
const [authState, setAuthState] = React.useState()
const [state, setState] = React.useState({
user: null as CognitoUserExt | null,
isLoading: true
})
const [termsOfServiceUser, setTermsOfServiceUser] = React.useState(null)
React.useEffect(() => {
Hub.listen('auth', ({payload: {event, data}}) => {
switch (event) {
case 'signIn':
case 'cognitoHostedUI': {
getUser().then(userData => {
setState({
...state,
isLoading: false,
user: userData
})
console.log('signIn', userData)
})
break
}
case 'signOut':
setState({
...state,
isLoading: false,
user: null
})
break
case 'signIn_failure':
case 'cognitoHostedUI_failure':
console.log('Sign in failure', data)
break
}
})
getUser().then(userData =>
setState({
...state,
isLoading: false,
user: userData
})
)
}, [])
if (state.isLoading) {
return (
<View
style={{
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center'
}}
>
<CircularProgress />
</View>
)
}
return (
<>
{state.user ? (
state.user.attributes['custom:isOldUser'] !== 'yes' ? (
<TermsOfService
user={state.user}
onAgree={async () => {
await Auth.updateUserAttributes(state.user, {
'custom:isOldUser': 'yes'
})
// Due to being in an async, the type checker complains that user
// might be null, despite the null check above. Using state.user!
// to silence this warning.
state.user!.attributes['custom:isOldUser'] = 'yes'
setState({
...state,
user: state.user
})
}}
onDisagree={() => {
alert('To proceed on signin, you need to accept the Terms of Usage!')
}}
/>
) : (
<App />
)
) : (
<Signin
onSignin={async () => {
await Auth
.federatedSignIn
// undefined,
// {
// expires_at: new Date().getTime() + 1000 * 60 * 60 * 12 * 3 ,
// }
// {
// customProvider: 'COGNITO_IDENTITY_POOLS',
// provider: 'COGNITO',
// }
()
setState({
...state,
isLoading: true
})
}}
onSignup={() => {
Auth.federatedSignIn()
setState({
...state,
isLoading: true
})
}}
/>
// <TermsOfService
// user={user}
// onAgree={async () => {
// updateState((draft) => {
// draft.helpModal.isOpen = true
// })
// await Auth.updateUserAttributes(user, {
// 'custom:isOldUser': 'yes'
// })
// }}
// onDisagree={() => {
// alert('To proceed on signin, you need to accept the Terms of Usage!')
// }}
// />
)}
</>
)
}
export default AppContainer