-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dashboard API Key Management (#3423)
Develop a UI page where a logged-in user can generate a new key for their account and manage existing keys. PBENCH-1131
- Loading branch information
Showing
10 changed files
with
362 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as TYPES from "actions/types"; | ||
|
||
import { DANGER, ERROR_MSG, SUCCESS } from "assets/constants/toastConstants"; | ||
|
||
import API from "../utils/axiosInstance"; | ||
import { showToast } from "./toastActions"; | ||
import { uriTemplate } from "utils/helper"; | ||
|
||
export const getAPIkeysList = async (dispatch, getState) => { | ||
try { | ||
dispatch({ type: TYPES.LOADING }); | ||
|
||
const endpoints = getState().apiEndpoint.endpoints; | ||
const response = await API.get(uriTemplate(endpoints, "key", { key: "" })); | ||
|
||
if (response.status === 200) { | ||
dispatch({ | ||
type: TYPES.SET_API_KEY_LIST, | ||
payload: response.data, | ||
}); | ||
} else { | ||
dispatch(showToast(DANGER, ERROR_MSG)); | ||
} | ||
} catch (error) { | ||
dispatch(showToast(DANGER, error)); | ||
} | ||
dispatch({ type: TYPES.COMPLETED }); | ||
}; | ||
|
||
export const deleteAPIKey = (id) => async (dispatch, getState) => { | ||
try { | ||
dispatch({ type: TYPES.LOADING }); | ||
const endpoints = getState().apiEndpoint.endpoints; | ||
const response = await API.delete( | ||
uriTemplate(endpoints, "key", { key: id }) | ||
); | ||
|
||
if (response.status === 200) { | ||
dispatch({ | ||
type: TYPES.SET_API_KEY_LIST, | ||
payload: getState().keyManagement.keyList.filter( | ||
(item) => item.id !== id | ||
), | ||
}); | ||
|
||
const message = response.data ?? "Deleted"; | ||
const toastMsg = message?.charAt(0).toUpperCase() + message?.slice(1); | ||
|
||
dispatch(showToast(SUCCESS, toastMsg)); | ||
} else { | ||
dispatch(showToast(DANGER, ERROR_MSG)); | ||
} | ||
} catch (error) { | ||
dispatch(showToast(DANGER, error)); | ||
} | ||
dispatch({ type: TYPES.COMPLETED }); | ||
}; | ||
|
||
export const sendNewKeyRequest = (label) => async (dispatch, getState) => { | ||
try { | ||
dispatch({ type: TYPES.LOADING }); | ||
const endpoints = getState().apiEndpoint.endpoints; | ||
const keyList = [...getState().keyManagement.keyList]; | ||
|
||
const response = await API.post( | ||
uriTemplate(endpoints, "key", { key: "" }), | ||
null, | ||
{ params: { label } } | ||
); | ||
if (response.status === 201) { | ||
keyList.push(response.data); | ||
dispatch({ | ||
type: TYPES.SET_API_KEY_LIST, | ||
payload: keyList, | ||
}); | ||
dispatch(showToast(SUCCESS, "API key created successfully")); | ||
|
||
dispatch(toggleNewAPIKeyModal(false)); | ||
dispatch(setNewKeyLabel("")); | ||
} else { | ||
dispatch(showToast(DANGER, response.data.message)); | ||
} | ||
} catch { | ||
dispatch(showToast(DANGER, ERROR_MSG)); | ||
} | ||
dispatch({ type: TYPES.COMPLETED }); | ||
}; | ||
|
||
export const toggleNewAPIKeyModal = (isOpen) => ({ | ||
type: TYPES.TOGGLE_NEW_KEY_MODAL, | ||
payload: isOpen, | ||
}); | ||
|
||
export const setNewKeyLabel = (label) => ({ | ||
type: TYPES.SET_NEW_KEY_LABEL, | ||
payload: label, | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const DANGER = "danger"; | ||
export const ERROR_MSG = "Something went wrong!"; | ||
export const SUCCESS = "success"; |
69 changes: 69 additions & 0 deletions
69
dashboard/src/modules/components/ProfileComponent/KeyListTable.jsx
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,69 @@ | ||
import { Button, ClipboardCopy } from "@patternfly/react-core"; | ||
import { | ||
TableComposable, | ||
Tbody, | ||
Td, | ||
Th, | ||
Thead, | ||
Tr, | ||
} from "@patternfly/react-table"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import React from "react"; | ||
import { TrashIcon } from "@patternfly/react-icons"; | ||
import { deleteAPIKey } from "actions/keyManagementActions"; | ||
import { formatDateTime } from "utils/dateFunctions"; | ||
|
||
const KeyListTable = () => { | ||
const dispatch = useDispatch(); | ||
const keyList = useSelector((state) => state.keyManagement.keyList); | ||
const columnNames = { | ||
label: "Label", | ||
created: "Created Date & Time", | ||
key: "API key", | ||
}; | ||
|
||
return ( | ||
<TableComposable aria-label="key list table" isStriped> | ||
<Thead> | ||
<Tr> | ||
<Th width={10}>{columnNames.label}</Th> | ||
<Th width={20}>{columnNames.created}</Th> | ||
<Th width={20}>{columnNames.key}</Th> | ||
<Th width={5}></Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody className="keylist-table-body"> | ||
{keyList.map((item) => ( | ||
<Tr key={item.key}> | ||
<Td dataLabel={columnNames.label}>{item.label}</Td> | ||
<Td dataLabel={columnNames.created}> | ||
{formatDateTime(item.created)} | ||
</Td> | ||
<Td dataLabel={columnNames.key} className="key-cell"> | ||
<ClipboardCopy | ||
hoverTip="Copy API key" | ||
clickTip="Copied" | ||
variant="plain" | ||
> | ||
{item.key} | ||
</ClipboardCopy> | ||
</Td> | ||
|
||
<Td className="delete-icon-cell"> | ||
<Button | ||
variant="plain" | ||
aria-label="Delete Action" | ||
onClick={() => dispatch(deleteAPIKey(item.id))} | ||
> | ||
<TrashIcon /> | ||
</Button> | ||
</Td> | ||
</Tr> | ||
))} | ||
</Tbody> | ||
</TableComposable> | ||
); | ||
}; | ||
|
||
export default KeyListTable; |
46 changes: 46 additions & 0 deletions
46
dashboard/src/modules/components/ProfileComponent/KeyManagement.jsx
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,46 @@ | ||
import { Button, Card, CardBody } from "@patternfly/react-core"; | ||
import React, { useEffect } from "react"; | ||
import { | ||
getAPIkeysList, | ||
setNewKeyLabel, | ||
toggleNewAPIKeyModal, | ||
} from "actions/keyManagementActions"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import KeyListTable from "./KeyListTable"; | ||
import NewKeyModal from "./NewKeyModal"; | ||
|
||
const KeyManagementComponent = () => { | ||
const dispatch = useDispatch(); | ||
const isModalOpen = useSelector((state) => state.keyManagement.isModalOpen); | ||
const { idToken } = useSelector((state) => state.apiEndpoint?.keycloak); | ||
useEffect(() => { | ||
if (idToken) { | ||
dispatch(getAPIkeysList); | ||
} | ||
}, [dispatch, idToken]); | ||
const handleModalToggle = () => { | ||
dispatch(setNewKeyLabel("")); | ||
dispatch(toggleNewAPIKeyModal(!isModalOpen)); | ||
}; | ||
return ( | ||
<Card className="key-management-container"> | ||
<CardBody> | ||
<div className="heading-wrapper"> | ||
<p className="heading-title">API Keys</p> | ||
<Button variant="tertiary" onClick={handleModalToggle}> | ||
New API key | ||
</Button> | ||
</div> | ||
<p className="key-desc"> | ||
This is a list of API keys associated with your account. Remove any | ||
keys that you do not recognize. | ||
</p> | ||
<KeyListTable /> | ||
</CardBody> | ||
<NewKeyModal handleModalToggle={handleModalToggle} /> | ||
</Card> | ||
); | ||
}; | ||
|
||
export default KeyManagementComponent; |
60 changes: 60 additions & 0 deletions
60
dashboard/src/modules/components/ProfileComponent/NewKeyModal.jsx
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,60 @@ | ||
import "./index.less"; | ||
|
||
import { | ||
Button, | ||
Form, | ||
FormGroup, | ||
Modal, | ||
ModalVariant, | ||
TextInput, | ||
} from "@patternfly/react-core"; | ||
import { | ||
sendNewKeyRequest, | ||
setNewKeyLabel, | ||
} from "actions/keyManagementActions"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import React from "react"; | ||
|
||
const NewKeyModal = (props) => { | ||
const dispatch = useDispatch(); | ||
const { isModalOpen, newKeyLabel } = useSelector( | ||
(state) => state.keyManagement | ||
); | ||
|
||
return ( | ||
<Modal | ||
variant={ModalVariant.small} | ||
title="New API Key" | ||
isOpen={isModalOpen} | ||
showClose={false} | ||
actions={[ | ||
<Button | ||
key="create" | ||
variant="primary" | ||
form="modal-with-form-form" | ||
onClick={() => dispatch(sendNewKeyRequest(newKeyLabel))} | ||
> | ||
Create | ||
</Button>, | ||
<Button key="cancel" variant="link" onClick={props.handleModalToggle}> | ||
Cancel | ||
</Button>, | ||
]} | ||
> | ||
<Form id="new-api-key-form"> | ||
<FormGroup label="Enter the label" fieldId="new-api-key-form"> | ||
<TextInput | ||
type="text" | ||
id="new-api-key-form" | ||
name="new-api-key-form" | ||
value={newKeyLabel} | ||
onChange={(value) => dispatch(setNewKeyLabel(value))} | ||
/> | ||
</FormGroup> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default NewKeyModal; |
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
Oops, something went wrong.