-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): add page to create service account token (#823)
* 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
Showing
10 changed files
with
455 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
sdks/js/packages/core/react/components/organization/api-keys/service-user/add-token.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.