Skip to content

Commit 5369430

Browse files
feat(firstMile): create the all-access token for the user (#4396)
1 parent a4342ea commit 5369430

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

src/homepageExperience/components/steps/CreateToken.tsx

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,76 @@
11
// Libraries
2-
import React, {FC} from 'react'
3-
import CodeSnippet from 'src/shared/components/CodeSnippet'
4-
import {Button, ComponentColor, ComponentSize} from '@influxdata/clockface'
5-
import {connect, ConnectedProps, useDispatch, useSelector} from 'react-redux'
6-
import {RouteComponentProps, withRouter} from 'react-router-dom'
2+
import React, {useEffect, useMemo, useState, FC} from 'react'
3+
import {useDispatch, useSelector} from 'react-redux'
74

8-
// Selectors
9-
import {notify} from 'src/shared/actions/notifications'
10-
import {getResourcesTokensFailure} from 'src/shared/copy/notifications'
5+
// Actions
116
import {getAllResources} from 'src/authorizations/actions/thunks'
12-
import {dismissOverlay, showOverlay} from 'src/overlays/actions/overlays'
7+
import {createAuthorization} from 'src/authorizations/actions/thunks'
8+
9+
// Selectors
1310
import {getOrg} from 'src/organizations/selectors'
11+
import {getMe} from 'src/me/selectors'
12+
import {getAllTokensResources} from 'src/resources/selectors'
1413

1514
// Helper Components
1615
import {SafeBlankLink} from 'src/utils/SafeBlankLink'
16+
import CodeSnippet from 'src/shared/components/CodeSnippet'
17+
18+
// Utils
19+
import {allAccessPermissions} from 'src/authorizations/utils/permissions'
1720
import {event} from 'src/cloud/utils/reporting'
1821

19-
type ReduxProps = ConnectedProps<typeof connector>
22+
// Types
23+
import {AppState, Authorization} from 'src/types'
2024

2125
type OwnProps = {
2226
wizardEventName: string
2327
}
2428

25-
const CreateTokenComponent: FC<ReduxProps & RouteComponentProps & OwnProps> = ({
26-
showOverlay,
27-
dismissOverlay,
28-
getAllResources,
29-
wizardEventName,
30-
}) => {
29+
const collator = new Intl.Collator(navigator.language || 'en-US')
30+
31+
export const CreateToken: FC<OwnProps> = ({wizardEventName}) => {
3132
const org = useSelector(getOrg)
33+
const me = useSelector(getMe)
34+
const allPermissionTypes = useSelector(getAllTokensResources)
3235
const dispatch = useDispatch()
33-
const handleAllAccess = async () => {
34-
try {
35-
await getAllResources()
36-
showOverlay('add-master-token', null, dismissOverlay)
37-
} catch {
38-
dispatch(notify(getResourcesTokensFailure('all access token')))
36+
const [tokenTextboxText, setTokenTextboxText] = useState(
37+
'Generating your token'
38+
)
39+
40+
const currentAuth = useSelector((state: AppState) => {
41+
return state.resources.tokens.currentAuth.item
42+
})
43+
44+
const sortedPermissionTypes = useMemo(
45+
() => allPermissionTypes.sort((a, b) => collator.compare(a, b)),
46+
[allPermissionTypes]
47+
)
48+
49+
useEffect(() => {
50+
const fetchResources = async () => {
51+
await dispatch(getAllResources())
3952
}
40-
}
53+
fetchResources()
54+
}, [])
55+
56+
useEffect(() => {
57+
if (sortedPermissionTypes.length) {
58+
const authorization: Authorization = {
59+
orgID: org.id,
60+
description: `onboarding-${wizardEventName}-token-${Date.now()}`,
61+
permissions: allAccessPermissions(sortedPermissionTypes, org.id, me.id),
62+
}
63+
64+
dispatch(createAuthorization(authorization))
65+
event(`firstMile.${wizardEventName}.createToken.tokenCreated`)
66+
}
67+
}, [sortedPermissionTypes.length])
68+
69+
useEffect(() => {
70+
if (currentAuth.token) {
71+
setTokenTextboxText(`export INFLUXDB_TOKEN=${currentAuth.token}`)
72+
}
73+
}, [currentAuth.token])
4174

4275
// Events log handling
4376
const logCopyCodeSnippet = () => {
@@ -52,23 +85,14 @@ const CreateTokenComponent: FC<ReduxProps & RouteComponentProps & OwnProps> = ({
5285
<>
5386
<h1>Create a Token</h1>
5487
<p>
55-
InfluxDB Cloud uses Tokens to authenticate API access. Click below to
56-
create an all-access token.
88+
InfluxDB Cloud uses Tokens to authenticate API access. We've generated
89+
an all-access token for you.
5790
</p>
58-
<Button
59-
text="Generate Token"
60-
onClick={handleAllAccess}
61-
color={ComponentColor.Primary}
62-
size={ComponentSize.Medium}
63-
/>
6491
<p style={{marginTop: '51px'}}>
6592
Save your token as an environment variable; you’ll use it soon. Run this
6693
command in your terminal:
6794
</p>
68-
<CodeSnippet
69-
text="export INFLUXDB_TOKEN=<your token here>"
70-
onCopy={logCopyCodeSnippet}
71-
/>
95+
<CodeSnippet text={tokenTextboxText} onCopy={logCopyCodeSnippet} />
7296
<p style={{marginTop: '46px'}}>
7397
You can create tokens in the future in the{' '}
7498
<SafeBlankLink
@@ -82,12 +106,3 @@ const CreateTokenComponent: FC<ReduxProps & RouteComponentProps & OwnProps> = ({
82106
</>
83107
)
84108
}
85-
86-
const mdtp = {
87-
showOverlay,
88-
dismissOverlay,
89-
getAllResources,
90-
}
91-
92-
const connector = connect(null, mdtp)
93-
export const CreateToken = connector(withRouter(CreateTokenComponent))

0 commit comments

Comments
 (0)