Skip to content

Commit

Permalink
feat(sdk): add page to create service account token (#823)
Browse files Browse the repository at this point in the history
* feat: add link to service account page

* feat: add page for service user

* feat: show token list for service user

* feat: add screen for token generation

* feat: create token from list screen instead of popup

* feat: add copy to clipboard for token

* feat: auto generate key for first time

* feat: add height to api keys table

* chore: remove duplicate class

* fix: lint

* fix: package path
  • Loading branch information
rsbh authored and anujk14 committed Dec 2, 2024
1 parent fe9947b commit 7139581
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import * as yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { useCallback } from 'react';

const DEFAULT_KEY_NAME = 'Initial Generated Key';

const serviceAccountSchema = yup
.object({
title: yup.string().required('Name is a required field')
Expand Down Expand Up @@ -49,12 +51,25 @@ export const AddServiceAccount = () => {
body: data,
org_id: orgId
});
toast.success('Service user created');

navigate({
to: '/api-keys/$id',
params: { id: serviceuser?.id ?? '' }
});
if (serviceuser?.id) {
const {
data: { token }
} = await client.frontierServiceCreateServiceUserToken(
serviceuser?.id,
{ title: DEFAULT_KEY_NAME }
);

toast.success('Service user created');

navigate({
to: '/api-keys/$id',
params: { id: serviceuser?.id ?? '' },
state: {
token: token
}
});
}
} catch ({ error }: any) {
toast.error('Something went wrong', {
description: error.message
Expand All @@ -70,7 +85,6 @@ export const AddServiceAccount = () => {
<Dialog open={true}>
{/* @ts-ignore */}
<Dialog.Content
style={{}}
overlayClassname={styles.overlay}
className={styles.addDialogContent}
>
Expand Down Expand Up @@ -122,7 +136,7 @@ export const AddServiceAccount = () => {
<Flex justify="end" className={styles.addDialogFormBtnWrapper}>
<Button
variant="primary"
size="medium"
size="normal"
type="submit"
data-test-id="frontier-sdk-add-service-account-btn"
loading={isSubmitting}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TrashIcon } from '@radix-ui/react-icons';
import { ApsaraColumnDef } from '@raystack/apsara';
import { Button, Flex, Text } from '@raystack/apsara/v1';
import { Link } from '@tanstack/react-router';
import dayjs from 'dayjs';
import { V1Beta1ServiceUser } from '~/api-client';

Expand All @@ -15,9 +16,18 @@ export const getColumns = ({
accessorKey: 'title',
cell: ({ row, getValue }) => {
return (
<Flex direction="column">
<Text>{getValue()}</Text>
</Flex>
<Link
to={`/api-keys/$id`}
params={{
id: row.original.id || ''
}}
style={{
textDecoration: 'none',
color: 'var(--rs-color-text-base-primary)'
}}
>
{getValue()}
</Link>
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ const ServiceAccountsTable = ({
const navigate = useNavigate({ from: '/api-keys' });

return (
<DataTable data={serviceUsers} columns={columns} isLoading={isLoading}>
<DataTable
data={serviceUsers}
columns={columns}
isLoading={isLoading}
parentStyle={{ height: 'calc(70vh - 150px)' }}
>
<DataTable.Toolbar className={styles.tableToolbar}>
<Flex justify="between" gap="small">
<Flex className={styles.tableToolbarSearchWrapper}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { Controller, useForm } from 'react-hook-form';
import { useFrontier } from '~/react/contexts/FrontierContext';
import { useCallback } from 'react';
import { Flex, toast, Text, Button } from '@raystack/apsara/v1';
import styles from './styles.module.css';
import { TextField } from '@raystack/apsara';
import { V1Beta1ServiceUserToken } from '~/api-client';

const serviceAccountSchema = yup
.object({
title: yup.string().required('Name is a required field')
})
.required();

type FormData = yup.InferType<typeof serviceAccountSchema>;

export default function AddServiceUserToken({
serviceUserId,
onAddToken = () => {}
}: {
serviceUserId: string;
onAddToken: (token: V1Beta1ServiceUserToken) => void;
}) {
const { client } = useFrontier();
const {
control,
handleSubmit,
formState: { errors, isSubmitting }
} = useForm({
resolver: yupResolver(serviceAccountSchema)
});

const onSubmit = useCallback(
async (data: FormData) => {
if (!client) return;

try {
const {
data: { token }
} = await client.frontierServiceCreateServiceUserToken(
serviceUserId,
data
);
if (token) {
onAddToken(token);
toast.success('Api key created');
}
} catch ({ error }: any) {
toast.error('Something went wrong', {
description: error.message
});
}
},
[client, onAddToken, serviceUserId]
);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Flex gap="small">
<Flex className={styles.addKeyInputWrapper} direction={'column'}>
<Controller
render={({ field }) => (
<TextField
{...field}
size={'medium'}
placeholder="Provide service key name"
/>
)}
name="title"
control={control}
/>
<Text size={1} variant="danger" className={styles.addKeyInputError}>
{errors.title && String(errors.title?.message)}
</Text>
</Flex>
<Button
data-test-id="frontier-sdk-api-keys-new-token-btn"
type="submit"
loading={isSubmitting}
disabled={isSubmitting}
loaderText="Generating..."
>
Generate new key
</Button>
</Flex>
</form>
);
}
Loading

0 comments on commit 7139581

Please sign in to comment.