-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): allow editing of multiple incoming auth tokens with `…
…EditableTable`
- Loading branch information
1 parent
8dc334b
commit 0e82227
Showing
2 changed files
with
163 additions
and
3 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,154 @@ | ||
import type { ReactNode } from 'react' | ||
import { useEffect, useState } from 'react' | ||
import { Input } from './Input' | ||
import { Table } from './Table' | ||
import { FieldError } from './FieldError' | ||
import { Button } from './Button' | ||
|
||
type EditableTableProps = { | ||
name: string | ||
label: string | ||
options: EditableTableOption[] | ||
error?: string | string[] | ||
description?: ReactNode | ||
valueFormatter?: (values: string[]) => string | ||
required?: boolean | ||
} | ||
|
||
type EditableTableOption = { | ||
label: string | ||
value: string | ||
canDelete?: boolean | ||
canEdit?: boolean | ||
showInput?: boolean | ||
} | ||
|
||
export const EditableTable = ({ | ||
name, | ||
label, | ||
options, | ||
error, | ||
description = undefined, | ||
valueFormatter = (values) => values.join(','), | ||
required = false | ||
}: EditableTableProps) => { | ||
const [optionsList, setOptionsList] = useState<EditableTableOption[]>(options) | ||
const [value, setValue] = useState<string>('') | ||
|
||
const toggleEditInput = (index: number) => { | ||
setOptionsList( | ||
optionsList.map((option, i) => { | ||
if (i === index) { | ||
return { | ||
...option, | ||
showInput: true | ||
} | ||
} | ||
return option | ||
}) | ||
) | ||
} | ||
|
||
const editOption = (index: number, value: string) => { | ||
if (!value) { | ||
deleteOption(index) | ||
return | ||
} | ||
setOptionsList( | ||
optionsList.map((option, i) => { | ||
if (i === index) { | ||
return { | ||
...option, | ||
showInput: false, | ||
value | ||
} | ||
} | ||
return option | ||
}) | ||
) | ||
} | ||
|
||
const deleteOption = (index: number) => { | ||
setOptionsList(optionsList.filter((_, i) => i !== index)) | ||
} | ||
|
||
const addOption = () => { | ||
setOptionsList([ | ||
...optionsList, | ||
{ label: '', value: '', canDelete: true, canEdit: true, showInput: true } | ||
]) | ||
} | ||
|
||
useEffect(() => { | ||
setValue(getValue()) | ||
}, [optionsList]) | ||
|
||
const getValue = () => { | ||
return valueFormatter(optionsList.map((option) => option.value)) | ||
} | ||
|
||
return ( | ||
<> | ||
<Input | ||
type='hidden' | ||
name={name} | ||
value={value} | ||
required={required} | ||
label={label} | ||
/> | ||
<Table> | ||
<Table.Head columns={['Token', 'Action']} /> | ||
<Table.Body> | ||
{(optionsList || []).map((option, index) => ( | ||
<Table.Row key={index}> | ||
<Table.Cell key={0}> | ||
{option.showInput ? ( | ||
<Input | ||
type='text' | ||
onKeyDown={(e) => | ||
e.key === 'Enter' && | ||
(e.preventDefault(), | ||
editOption(index, e.currentTarget.value)) | ||
} | ||
defaultValue={option.value} | ||
required={required} | ||
/> | ||
) : ( | ||
<span>{option.value}</span> | ||
)} | ||
</Table.Cell> | ||
<Table.Cell key={1}> | ||
{option.canEdit && !option.showInput ? ( | ||
<Button | ||
aria-label='save http information' | ||
onClick={() => toggleEditInput(index)} | ||
> | ||
Edit | ||
</Button> | ||
) : null} | ||
{option.canDelete ? ( | ||
<Button | ||
className='ml-2' | ||
aria-label='delete http information' | ||
onClick={() => deleteOption(index)} | ||
> | ||
Delete | ||
</Button> | ||
) : null} | ||
</Table.Cell> | ||
</Table.Row> | ||
))} | ||
</Table.Body> | ||
</Table> | ||
<div className='flex justify-end mt-2'> | ||
<Button aria-label='add http information' onClick={() => addOption()}> | ||
Add | ||
</Button> | ||
</div> | ||
{description ? ( | ||
<div className='font-medium text-sm'>{description}</div> | ||
) : null} | ||
<FieldError error={error} /> | ||
</> | ||
) | ||
} |
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