Skip to content

Commit fec888a

Browse files
Kira-Pilotpull[bot]
authored andcommitted
feat: add success modal with token value to create token page (#7170)
* added token success modal * added a test for new modal
1 parent fa13afc commit fec888a

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

site/src/i18n/en/tokensPage.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
"submit": "Create token"
4747
},
4848
"createSuccess": "Token has been created",
49-
"createError": "Failed to create token"
49+
"createError": "Failed to create token",
50+
"successModal": {
51+
"title": "Creation successful",
52+
"description": "Make sure you copy the below token before proceeding:"
53+
}
5054
}
5155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
renderWithAuth,
3+
waitForLoaderToBeRemoved,
4+
} from "testHelpers/renderHelpers"
5+
import { CreateTokenPage } from "pages/CreateTokenPage/CreateTokenPage"
6+
import * as API from "api/api"
7+
import { screen, within } from "@testing-library/react"
8+
import userEvent from "@testing-library/user-event"
9+
10+
describe("TokenPage", () => {
11+
it("shows the success modal", async () => {
12+
jest.spyOn(API, "createToken").mockResolvedValueOnce({
13+
key: "abcd",
14+
})
15+
16+
// When
17+
const { container } = renderWithAuth(<CreateTokenPage />, {
18+
route: "/settings/tokens/new",
19+
path: "/settings/tokens/new",
20+
})
21+
await waitForLoaderToBeRemoved()
22+
23+
const form = container.querySelector("form") as HTMLFormElement
24+
await userEvent.type(screen.getByLabelText(/Name/), "my-token")
25+
await userEvent.click(
26+
within(form).getByRole("button", { name: /create token/i }),
27+
)
28+
29+
// Then
30+
expect(screen.getByText("abcd")).toBeInTheDocument()
31+
})
32+
})

site/src/pages/CreateTokenPage/CreateTokenPage.tsx

+34-2
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@ import { createToken, getTokenConfig } from "api/api"
1212
import { CreateTokenForm } from "./CreateTokenForm"
1313
import { NANO_HOUR, CreateTokenData } from "./utils"
1414
import { AlertBanner } from "components/AlertBanner/AlertBanner"
15+
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
16+
import { CodeExample } from "components/CodeExample/CodeExample"
17+
import { makeStyles } from "@material-ui/core/styles"
1518

1619
const initialValues: CreateTokenData = {
1720
name: "",
1821
lifetime: 30,
1922
}
2023

21-
const CreateTokenPage: FC = () => {
24+
export const CreateTokenPage: FC = () => {
2225
const { t } = useTranslation("tokensPage")
26+
const styles = useStyles()
2327
const navigate = useNavigate()
2428

2529
const {
2630
mutate: saveToken,
2731
isLoading: isCreating,
2832
isError: creationFailed,
33+
isSuccess: creationSuccessful,
34+
data: newToken,
2935
} = useMutation(createToken)
3036
const {
3137
data: tokenConfig,
@@ -60,12 +66,18 @@ const CreateTokenPage: FC = () => {
6066
},
6167
{
6268
onError: onCreateError,
63-
onSuccess: onCreateSuccess,
6469
},
6570
)
6671
},
6772
})
6873

74+
const tokenDescription = (
75+
<>
76+
<p>{t("createToken.successModal.description")}</p>
77+
<CodeExample code={newToken?.key ?? ""} className={styles.codeExample} />
78+
</>
79+
)
80+
6981
if (fetchingTokenConfig) {
7082
return <Loader />
7183
}
@@ -90,9 +102,29 @@ const CreateTokenPage: FC = () => {
90102
isCreating={isCreating}
91103
creationFailed={creationFailed}
92104
/>
105+
106+
<ConfirmDialog
107+
type="info"
108+
hideCancel
109+
title={t("createToken.successModal.title")}
110+
description={tokenDescription}
111+
open={creationSuccessful && Boolean(newToken.key)}
112+
confirmLoading={isCreating}
113+
onConfirm={onCreateSuccess}
114+
onClose={onCreateSuccess}
115+
/>
93116
</FullPageHorizontalForm>
94117
</>
95118
)
96119
}
97120

121+
const useStyles = makeStyles((theme) => ({
122+
codeExample: {
123+
minHeight: "auto",
124+
userSelect: "all",
125+
width: "100%",
126+
marginTop: theme.spacing(3),
127+
},
128+
}))
129+
98130
export default CreateTokenPage

0 commit comments

Comments
 (0)