2022-12-27 01:57:59 +01:00
|
|
|
import Link from 'next/link';
|
|
|
|
import {
|
2023-03-09 21:42:12 +01:00
|
|
|
Button,
|
|
|
|
Flexbox,
|
|
|
|
Icon,
|
|
|
|
Icons,
|
|
|
|
Modal,
|
|
|
|
ModalTrigger,
|
2022-12-27 01:57:59 +01:00
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableColumn,
|
2023-03-09 21:42:12 +01:00
|
|
|
TableHeader,
|
|
|
|
TableRow,
|
2023-01-25 16:42:46 +01:00
|
|
|
Text,
|
2022-12-27 01:57:59 +01:00
|
|
|
} from 'react-basics';
|
2023-02-02 20:59:38 +01:00
|
|
|
import TeamDeleteForm from './TeamDeleteForm';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
|
|
|
import useUser from 'hooks/useUser';
|
|
|
|
import { ROLES } from 'lib/constants';
|
2022-12-27 01:57:59 +01:00
|
|
|
|
2023-02-02 20:59:38 +01:00
|
|
|
export default function TeamsTable({ data = [], onDelete }) {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-03-09 21:42:12 +01:00
|
|
|
const { user } = useUser();
|
2023-01-25 16:42:46 +01:00
|
|
|
|
|
|
|
const columns = [
|
|
|
|
{ name: 'name', label: formatMessage(labels.name), style: { flex: 2 } },
|
2023-02-02 03:39:54 +01:00
|
|
|
{ name: 'owner', label: formatMessage(labels.owner) },
|
2023-01-25 16:42:46 +01:00
|
|
|
{ name: 'action', label: ' ' },
|
|
|
|
];
|
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
return (
|
2023-01-25 16:42:46 +01:00
|
|
|
<Table columns={columns} rows={data}>
|
2022-12-27 01:57:59 +01:00
|
|
|
<TableHeader>
|
|
|
|
{(column, index) => {
|
|
|
|
return (
|
|
|
|
<TableColumn key={index} style={{ ...column.style }}>
|
|
|
|
{column.label}
|
|
|
|
</TableColumn>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody>
|
|
|
|
{(row, keys, rowIndex) => {
|
|
|
|
const { id } = row;
|
2023-03-09 21:42:12 +01:00
|
|
|
const owner = row.teamUser.find(({ role }) => role === ROLES.teamOwner);
|
|
|
|
const showDelete = user.id === owner?.userId;
|
2022-12-27 01:57:59 +01:00
|
|
|
|
2023-02-02 03:39:54 +01:00
|
|
|
const rowData = {
|
|
|
|
...row,
|
2023-03-09 21:42:12 +01:00
|
|
|
owner: owner?.user?.username,
|
2023-02-02 03:39:54 +01:00
|
|
|
action: (
|
2023-02-02 20:59:38 +01:00
|
|
|
<Flexbox flex={1} gap={10} justifyContent="end">
|
2023-02-02 03:39:54 +01:00
|
|
|
<Link href={`/settings/teams/${id}`}>
|
2023-03-09 05:23:32 +01:00
|
|
|
<Button>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Edit />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(labels.edit)}</Text>
|
|
|
|
</Button>
|
2023-02-02 03:39:54 +01:00
|
|
|
</Link>
|
2023-03-09 21:42:12 +01:00
|
|
|
{showDelete && (
|
|
|
|
<ModalTrigger>
|
|
|
|
<Button>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Trash />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(labels.delete)}</Text>
|
|
|
|
</Button>
|
|
|
|
<Modal title={formatMessage(labels.deleteTeam)}>
|
|
|
|
{close => (
|
|
|
|
<TeamDeleteForm
|
|
|
|
teamId={row.id}
|
|
|
|
teamName={row.name}
|
|
|
|
onSave={onDelete}
|
|
|
|
onClose={close}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Modal>
|
|
|
|
</ModalTrigger>
|
|
|
|
)}
|
2023-02-02 03:39:54 +01:00
|
|
|
</Flexbox>
|
|
|
|
),
|
|
|
|
};
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
return (
|
2023-02-02 03:39:54 +01:00
|
|
|
<TableRow key={rowIndex} data={rowData} keys={keys}>
|
2022-12-27 01:57:59 +01:00
|
|
|
{(data, key, colIndex) => {
|
|
|
|
return (
|
2023-01-25 16:42:46 +01:00
|
|
|
<TableCell key={colIndex} style={{ ...columns[colIndex]?.style }}>
|
|
|
|
<Flexbox flex={1} alignItems="center">
|
|
|
|
{data[key]}
|
|
|
|
</Flexbox>
|
2022-12-27 01:57:59 +01:00
|
|
|
</TableCell>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TableRow>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
);
|
|
|
|
}
|