-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Dispatch, SetStateAction } from "react" | ||
|
||
export type ConfirmationModalData = { | ||
onMouseDownFunc: () => void | ||
warningText: string | ||
} | null | ||
|
||
interface ConfirmationModalProps { | ||
modalData: ConfirmationModalData | ||
setModalData: Dispatch<SetStateAction<ConfirmationModalData>> | ||
} | ||
|
||
export function ConfirmationModal({ modalData, setModalData }: ConfirmationModalProps) { | ||
const confirmQuery = () => { | ||
modalData?.onMouseDownFunc() | ||
setModalData(null) | ||
} | ||
return ( | ||
<div className="p-modal" id="modal"> | ||
<section className="p-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="modal-title" aria-describedby="modal-description"> | ||
<header className="p-modal__header"> | ||
<h2 className="p-modal__title" id="modal-title">Confirm Action</h2> | ||
</header> | ||
<p>{modalData?.warningText}</p> | ||
<footer className="p-modal__footer"> | ||
<button className="u-no-margin--bottom" aria-controls="modal" onMouseDown={() => setModalData(null)}>Cancel</button> | ||
<button className="p-button--negative u-no-margin--bottom" onMouseDown={confirmQuery}>Confirm</button> | ||
</footer> | ||
</section> | ||
</div> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState, Dispatch, SetStateAction, useEffect, useRef } from "react" | ||
import { UseMutationResult, useMutation, useQueryClient } from "react-query" | ||
import { RequiredCSRParams, deleteUser } from "../queries" | ||
import { ConfirmationModalData, ConfirmationModal } from "./components" | ||
import "./../globals.scss" | ||
import { useAuth } from "../auth/authContext" | ||
|
||
type rowProps = { | ||
id: number, | ||
username: string, | ||
} | ||
|
||
export default function Row({ id, username }: rowProps) { | ||
const auth = useAuth() | ||
const [confirmationModalData, setConfirmationModalData] = useState<ConfirmationModalData>(null) | ||
const queryClient = useQueryClient() | ||
const deleteMutation = useMutation(deleteUser, { | ||
onSuccess: () => queryClient.invalidateQueries('users') | ||
}) | ||
const mutationFunc = (mutation: UseMutationResult<any, unknown, RequiredCSRParams, unknown>, params: RequiredCSRParams) => { | ||
mutation.mutate(params) | ||
} | ||
|
||
const handleDelete = () => { | ||
setConfirmationModalData({ | ||
onMouseDownFunc: () => mutationFunc(deleteMutation, { id: id.toString(), authToken: auth.user ? auth.user.authToken : "" }), | ||
warningText: "Deleting a user cannot be undone." | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<tr> | ||
<td className="" width={5} aria-label="id">{id}</td> | ||
<td className="" aria-label="username">{username}</td> | ||
<td className="" aria-label="delete-button"> | ||
{id == 1 ? | ||
<button className="p-button--negative has-icon" onClick={handleDelete} disabled={true}><i className="p-icon--error"></i></button> | ||
: | ||
<button className="p-button--negative has-icon" onClick={handleDelete}><i className="p-icon--error"></i></button> | ||
} | ||
</td> | ||
{confirmationModalData != null && <ConfirmationModal modalData={confirmationModalData} setModalData={setConfirmationModalData} />} | ||
</tr> | ||
</> | ||
) | ||
} |
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,42 @@ | ||
import { useContext, useState, Dispatch, SetStateAction } from "react" | ||
import { AsideContext } from "../aside" | ||
import { UserEntry } from "../types" | ||
import Row from "./row" | ||
|
||
type TableProps = { | ||
users: UserEntry[] | ||
} | ||
|
||
export function UsersTable({ users: rows }: TableProps) { | ||
const { isOpen: isAsideOpen, setIsOpen: setAsideIsOpen } = useContext(AsideContext) | ||
return ( | ||
<div className="p-panel"> | ||
<div className="p-panel__header is-sticky"> | ||
<h4 className="p-panel__title">Users</h4> | ||
<div className="p-panel__controls"> | ||
{rows.length > 0 && <button className="u-no-margin--bottom p-button--positive" aria-label="add-csr-button" onClick={() => setAsideIsOpen(true)}>Create New User</button>} | ||
</div> | ||
</div> | ||
<div className="p-panel__content"> | ||
<div className="u-fixed-width"> | ||
<table id="csr-table" aria-label="Certificate Requests Table" className="p-table--expanding"> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Username</th> | ||
<th>Delete</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{ | ||
rows.map((row) => ( | ||
<Row key={row.id} id={row.id} username={row.username} /> | ||
) | ||
)} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} |