Skip to content

feat: add success modal with token value to create token page #7170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion site/src/i18n/en/tokensPage.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"submit": "Create token"
},
"createSuccess": "Token has been created",
"createError": "Failed to create token"
"createError": "Failed to create token",
"successModal": {
"title": "Creation successful",
"description": "Make sure you copy the below token before proceeding:"
}
}
}
32 changes: 32 additions & 0 deletions site/src/pages/CreateTokenPage/CreateTokenPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
renderWithAuth,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers"
import { CreateTokenPage } from "pages/CreateTokenPage/CreateTokenPage"
import * as API from "api/api"
import { screen, within } from "@testing-library/react"
import userEvent from "@testing-library/user-event"

describe("TokenPage", () => {
it("shows the success modal", async () => {
jest.spyOn(API, "createToken").mockResolvedValueOnce({
key: "abcd",
})

// When
const { container } = renderWithAuth(<CreateTokenPage />, {
route: "/settings/tokens/new",
path: "/settings/tokens/new",
})
await waitForLoaderToBeRemoved()

const form = container.querySelector("form") as HTMLFormElement
await userEvent.type(screen.getByLabelText(/Name/), "my-token")
await userEvent.click(
within(form).getByRole("button", { name: /create token/i }),
)

// Then
expect(screen.getByText("abcd")).toBeInTheDocument()
})
})
36 changes: 34 additions & 2 deletions site/src/pages/CreateTokenPage/CreateTokenPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ import { createToken, getTokenConfig } from "api/api"
import { CreateTokenForm } from "./CreateTokenForm"
import { NANO_HOUR, CreateTokenData } from "./utils"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { CodeExample } from "components/CodeExample/CodeExample"
import { makeStyles } from "@material-ui/core/styles"

const initialValues: CreateTokenData = {
name: "",
lifetime: 30,
}

const CreateTokenPage: FC = () => {
export const CreateTokenPage: FC = () => {
const { t } = useTranslation("tokensPage")
const styles = useStyles()
const navigate = useNavigate()

const {
mutate: saveToken,
isLoading: isCreating,
isError: creationFailed,
isSuccess: creationSuccessful,
data: newToken,
} = useMutation(createToken)
const {
data: tokenConfig,
Expand Down Expand Up @@ -60,12 +66,18 @@ const CreateTokenPage: FC = () => {
},
{
onError: onCreateError,
onSuccess: onCreateSuccess,
},
)
},
})

const tokenDescription = (
<>
<p>{t("createToken.successModal.description")}</p>
<CodeExample code={newToken?.key ?? ""} className={styles.codeExample} />
</>
)

if (fetchingTokenConfig) {
return <Loader />
}
Expand All @@ -90,9 +102,29 @@ const CreateTokenPage: FC = () => {
isCreating={isCreating}
creationFailed={creationFailed}
/>

<ConfirmDialog
type="info"
hideCancel
title={t("createToken.successModal.title")}
description={tokenDescription}
open={creationSuccessful && Boolean(newToken.key)}
confirmLoading={isCreating}
onConfirm={onCreateSuccess}
onClose={onCreateSuccess}
/>
</FullPageHorizontalForm>
</>
)
}

const useStyles = makeStyles((theme) => ({
codeExample: {
minHeight: "auto",
userSelect: "all",
width: "100%",
marginTop: theme.spacing(3),
},
}))

export default CreateTokenPage