Skip to content

Commit

Permalink
Added support for creating api key
Browse files Browse the repository at this point in the history
  • Loading branch information
runely committed Jun 7, 2022
1 parent 7066516 commit dafec25
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/hooks/useKeysAPI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ export function useKeysAPI (defaultItemsOptions = {}, top = 1000000) {
return orderBy(filtered, options.orderBy, options.order)
}, [options, _keys])

// new keys item
const newKeysItem = async name => {
const options = {
headers: {
'X-API-KEY': API.TOKEN
}
}

try {
setUpdating(true)
const { data } = await axios.post(`${API.URL}/apikeys?fullitem=true`, { name }, options)
setKeys([..._keys, data])
setUpdating(false)
return data
} catch (error) {
setUpdating(false)
throw error
}
}

const updateKeysItem = async (id, updateObject) => {
const options = {
headers: {
Expand Down Expand Up @@ -101,6 +121,7 @@ export function useKeysAPI (defaultItemsOptions = {}, top = 1000000) {
itemsOptions: options,
loading,
keys,
newKeysItem,
removeKeysItem,
setItemsOptions,
updateKeysItem,
Expand Down
40 changes: 37 additions & 3 deletions src/pages/APIKeys/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relativeDateFormat } from '@vtfk/utilities'
import { IconButton, Table } from '@vtfk/components'
import { IconButton, Table, TextField } from '@vtfk/components'
import React, { useState } from 'react'
import { isEqual } from 'lodash'
import { words as capitalizeWords } from 'capitalize'
Expand All @@ -11,8 +11,10 @@ import { useKeysAPI } from '../../hooks/useKeysAPI'
import './styles.scss'

export function APIKeys () {
const { itemsOptions, keys, loading, removeKeysItem, setItemsOptions, updateKeysItem, updating } = useKeysAPI()
const { itemsOptions, keys, loading, newKeysItem, removeKeysItem, setItemsOptions, updateKeysItem, updating } = useKeysAPI()
const [confirmationItem, setConfirmationItem] = useState(null)
const [openNewKeyDialog, setOpenNewKeyDialog] = useState(false)
const [newKeyName, setNewKeyName] = useState('')

const headers = [
{
Expand Down Expand Up @@ -81,14 +83,26 @@ export function APIKeys () {
}
}

const handleNewKeyOkClick = async () => {
try {
await newKeysItem(newKeyName)
console.log('Successfully added key', newKeyName) // TODO: Add toast for success
setOpenNewKeyDialog(false)
setNewKeyName('')
} catch (error) {
const failed = error.response?.data?.message || error.message || error
console.log('Failed to add key:', failed) // TODO: Add toast for error message
}
}

return (
<div className='apikeys-container'>
<div className='apikeys'>
<IconButton
className='add-key-button'
bordered
icon='add'
onClick={() => console.log('Add new key')}
onClick={() => setOpenNewKeyDialog(true)}
title='Add key'

/>
Expand All @@ -112,6 +126,26 @@ export function APIKeys () {
onDismiss={() => setConfirmationItem(null)}
/>
}

{
openNewKeyDialog &&
<ConfirmationDialog
open
title='Create new api key'
okBtnText='Create'
cancelBtnText='Cancel'
okBtnDisabled={updating}
onClickCancel={() => setOpenNewKeyDialog(false)}
onClickOk={() => handleNewKeyOkClick()}
onDismiss={() => setOpenNewKeyDialog(false)}
>
<TextField
placeholder='Key name'
value={newKeyName}
onChange={(e) => setNewKeyName(e.target.value)}
/>
</ConfirmationDialog>
}
</div>
)
}

1 comment on commit dafec25

@vercel
Copy link

@vercel vercel bot commented on dafec25 Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

e18-web – ./

e18-web.vercel.app
e18-web-git-main-vtfk.vercel.app
e18-web-vtfk.vercel.app

Please sign in to comment.