Skip to content

Commit

Permalink
- added delete button per spec
Browse files Browse the repository at this point in the history
- implemented the logic for multiple tokens deletion
- some ui changes and cleanup
  • Loading branch information
erka committed Nov 21, 2023
1 parent bd271c0 commit eea794e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
14 changes: 7 additions & 7 deletions ui/src/app/tokens/Tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ShowTokenPanel from '~/components/tokens/ShowTokenPanel';
import TokenForm from '~/components/tokens/TokenForm';
import TokenTable from '~/components/tokens/TokenTable';
import Well from '~/components/Well';
import { deleteToken, listAuthMethods, listTokens } from '~/data/api';
import { deleteTokens, listAuthMethods, listTokens } from '~/data/api';
import { useError } from '~/data/hooks/error';
import { IAuthMethod, IAuthMethodList } from '~/types/Auth';
import {
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function Tokens() {

const [showDeleteTokenModal, setShowDeleteTokenModal] =
useState<boolean>(false);
const [deletingToken, setDeletingToken] = useState<IAuthToken | null>(null);
const [deletingTokens, setDeletingTokens] = useState<IAuthToken[]| null>(null);

Check warning on line 40 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `|·null>(null` with `·|·null>(⏎····null⏎··`

Check failure on line 40 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Operator '|' must be spaced

Check warning on line 40 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `|·null>(null` with `·|·null>(⏎····null⏎··`

Check failure on line 40 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Operator '|' must be spaced

const tokenFormRef = useRef(null);

Expand Down Expand Up @@ -135,16 +135,16 @@ export default function Tokens() {
<DeletePanel
panelMessage={
<>
Are you sure you want to delete the token{' '}
Are you sure you want to delete the token(s)
<span className="text-violet-500 font-medium">
{deletingToken?.name}
{deletingTokens?.map((t) => ' ' + t.name)}
</span>
? This action cannot be undone.
</>
}
panelType="Token"
panelType="Tokens"
setOpen={setShowDeleteTokenModal}
handleDelete={() => deleteToken(deletingToken?.id ?? '')} // TODO: Determine impact of blank ID param
handleDelete={() => deleteTokens(deletingTokens?.map((t)=> t.id) || [])}

Check warning on line 147 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `·deleteTokens(deletingTokens?.map((t)=>·t.id)·||·[])` with `⏎············deleteTokens(deletingTokens?.map((t)·=>·t.id)·||·[])⏎··········`

Check warning on line 147 in ui/src/app/tokens/Tokens.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `·deleteTokens(deletingTokens?.map((t)=>·t.id)·||·[])` with `⏎············deleteTokens(deletingTokens?.map((t)·=>·t.id)·||·[])⏎··········`
onSuccess={() => {
incrementTokensVersion();
}}
Expand Down Expand Up @@ -186,7 +186,7 @@ export default function Tokens() {
{tokens && tokens.length > 0 ? (
<TokenTable
tokens={tokens}
setDeletingToken={setDeletingToken}
setDeletingTokens={setDeletingTokens}
setShowDeleteTokenModal={setShowDeleteTokenModal}
/>
) : (
Expand Down
44 changes: 24 additions & 20 deletions ui/src/components/tokens/TokenTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@ import { format, parseISO } from 'date-fns';
import React, { HTMLProps, useState } from 'react';
import Searchbox from '~/components/Searchbox';

Check warning on line 16 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Insert `Button·from·'~/components/forms/buttons/Button';⏎import·`

Check warning on line 16 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Insert `Button·from·'~/components/forms/buttons/Button';⏎import·`
import { IAuthToken } from '~/types/auth/Token';
import Button from '~/components/forms/buttons/Button';

Check warning on line 18 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Delete `import·Button·from·'~/components/forms/buttons/Button';⏎`

Check warning on line 18 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Delete `import·Button·from·'~/components/forms/buttons/Button';⏎`

type TokenRowActionsProps = {
row: Row<IAuthToken>;
setDeletingToken: (token: IAuthToken) => void;
setDeletingTokens: (tokens: IAuthToken[]) => void;
setShowDeleteTokenModal: (show: boolean) => void;
};

function TokenRowActions(props: TokenRowActionsProps) {
const { row, setDeletingToken, setShowDeleteTokenModal } = props;
const { row, setDeletingTokens, setShowDeleteTokenModal } = props;

let classes ="text-violet-600 hover:text-violet-900"
let className ="text-violet-600 hover:text-violet-900"

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Operator '=' must be spaced

Check warning on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `"text-violet-600·hover:text-violet-900"` with `·'text-violet-600·hover:text-violet-900';`

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Strings must use singlequote

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Operator '=' must be spaced

Check warning on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `"text-violet-600·hover:text-violet-900"` with `·'text-violet-600·hover:text-violet-900';`

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Strings must use singlequote

Check failure on line 29 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon
if (row.getIsSelected()) {
classes = "text-gray-400 hover:cursor-not-allowed"
className = "text-gray-400 hover:cursor-not-allowed"

Check warning on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `"text-gray-400·hover:cursor-not-allowed"` with `'text-gray-400·hover:cursor-not-allowed';`

Check failure on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Strings must use singlequote

Check failure on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon

Check warning on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Replace `"text-gray-400·hover:cursor-not-allowed"` with `'text-gray-400·hover:cursor-not-allowed';`

Check failure on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Strings must use singlequote

Check failure on line 31 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon
}

return (
<a
href="#"
className={classes}
className={className}
onClick={(e) => {
e.preventDefault();
if (!row.getIsSelected()){

Check warning on line 40 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Insert `·`

Check failure on line 40 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing space before opening brace

Check warning on line 40 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Insert `·`

Check failure on line 40 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing space before opening brace
setDeletingToken(row.original);
setDeletingTokens([row.original]);
setShowDeleteTokenModal(true);
}
}}
Expand All @@ -50,20 +51,26 @@ function TokenRowActions(props: TokenRowActionsProps) {

type TokenTableProps = {
tokens: IAuthToken[];
setDeletingToken: (token: IAuthToken) => void;
setDeletingTokens: (tokens: IAuthToken[]) => void;
setShowDeleteTokenModal: (show: boolean) => void;
};


export default function TokenTable(props: TokenTableProps) {
const { tokens, setDeletingToken, setShowDeleteTokenModal } = props;
const { tokens, setDeletingTokens, setShowDeleteTokenModal } = props;
const searchThreshold = 10;
const [sorting, setSorting] = useState<SortingState>([]);

const [filter, setFilter] = useState<string>('');
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})

Check failure on line 65 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon

Check failure on line 65 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon
const columnHelper = createColumnHelper<IAuthToken>();

const deleteMultipleTokens = (table :Table) => {
setDeletingTokens(table.getSelectedRowModel().rows.map((r) => r.original));
setShowDeleteTokenModal(true);
}

Check failure on line 71 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon

Check failure on line 71 in ui/src/components/tokens/TokenTable.tsx

View workflow job for this annotation

GitHub Actions / Lint UI

Missing semicolon


const columns = [
{
id: 'select',
Expand All @@ -84,11 +91,16 @@ export default function TokenTable(props: TokenTableProps) {
/>,
meta: {
className:
'whitespace-nowrap py-4 pl-3 pr-4 text-left text-sm font-medium sm:pr-6'
'whitespace-nowrap py-4 pl-3 pr-4 text-left'
}
},
columnHelper.accessor('name', {
header: 'Name',
header: ({ table }: Table) => {
if (table.getSelectedRowModel().rows.length > 0){
return <Button onClick={(e)=> {e.stopPropagation(); deleteMultipleTokens(table)}} title='Delete'>Delete</Button>
}
return 'Name'
},
cell: (info) => info.getValue(),
meta: {
className:
Expand Down Expand Up @@ -142,7 +154,7 @@ export default function TokenTable(props: TokenTableProps) {
<TokenRowActions
// eslint-disable-next-line react/prop-types
row={row}
setDeletingToken={setDeletingToken}
setDeletingTokens={setDeletingTokens}
setShowDeleteTokenModal={setShowDeleteTokenModal}
/>
),
Expand All @@ -153,8 +165,6 @@ export default function TokenTable(props: TokenTableProps) {
})
];



const table = useReactTable({
data: tokens,
columns,
Expand All @@ -173,12 +183,6 @@ export default function TokenTable(props: TokenTableProps) {

return (
<>
<button
onClick={() => console.info('selection', table.getSelectedRowModel().flatRows)}
>
Log `rowSelection` state
</button>

{tokens.length >= searchThreshold && (
<Searchbox className="mb-6" value={filter ?? ''} onChange={setFilter} />
)}
Expand Down Expand Up @@ -284,7 +288,7 @@ function IndeterminateCheckbox({
<input
type="checkbox"
ref={ref}
className={className + ' cursor-pointer'}
className={className + ' cursor-pointer w-4 h-4 text-purple-600 bg-gray-100 border-gray-300 rounded focus:ring-purple-500'}
{...rest}
/>
)
Expand Down
4 changes: 2 additions & 2 deletions ui/src/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export async function createToken(values: IAuthTokenBase) {
return post('/method/token', values, authURL);
}

export async function deleteToken(id: string) {
return del(`/tokens/${id}`, authURL);
export async function deleteTokens(ids: string[]) {
return Promise.all(ids.map((id) => del(`/tokens/${id}`, authURL)));
}

export async function listTokens(method = 'METHOD_TOKEN') {
Expand Down

0 comments on commit eea794e

Please sign in to comment.