umami/components/pages/settings/teams/TeamMembersTable.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

import {
Table,
TableHeader,
TableBody,
TableRow,
TableCell,
TableColumn,
Button,
Icon,
2023-01-25 16:42:46 +01:00
Icons,
Flexbox,
Text,
} from 'react-basics';
2023-01-25 16:42:46 +01:00
import { ROLES } from 'lib/constants';
2023-01-31 06:44:07 +01:00
import useUser from 'hooks/useUser';
import useApi from 'hooks/useApi';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-01-10 08:59:26 +01:00
export default function TeamMembersTable({ data = [], onSave, readOnly }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels } = useMessages();
2023-01-31 06:44:07 +01:00
const { user } = useUser();
const { del, useMutation } = useApi();
const { mutate } = useMutation(data => del(`/teamUsers/${data.teamUserId}`));
2023-01-25 16:42:46 +01:00
const columns = [
2023-01-31 06:44:07 +01:00
{ name: 'username', label: formatMessage(labels.username), style: { flex: 2 } },
{ name: 'role', label: formatMessage(labels.role), style: { flex: 1 } },
{ name: 'action', label: '', style: { flex: 1 } },
2023-01-25 16:42:46 +01:00
];
const handleRemoveTeamMember = teamUserId => {
mutate(
{ teamUserId },
{
onSuccess: async () => {
onSave();
},
},
);
};
return (
2023-01-25 16:42:46 +01:00
<Table columns={columns} rows={data}>
<TableHeader>
{(column, index) => {
return (
<TableColumn key={index} style={{ ...column.style }}>
{column.label}
</TableColumn>
);
}}
</TableHeader>
<TableBody>
{(row, keys, rowIndex) => {
2023-01-25 16:42:46 +01:00
const rowData = {
username: row?.user?.username,
role: formatMessage(
labels[Object.keys(ROLES).find(key => ROLES[key] === row.role) || labels.unknown],
),
2023-02-02 20:59:38 +01:00
action: !readOnly && (
2023-01-31 06:44:07 +01:00
<Flexbox flex={1} justifyContent="end">
<Button
onClick={() => handleRemoveTeamMember(row.id)}
disabled={user.id === row?.user?.id || row.role === ROLES.teamOwner}
>
2023-01-25 16:42:46 +01:00
<Icon>
<Icons.Close />
2023-01-25 16:42:46 +01:00
</Icon>
<Text>{formatMessage(labels.remove)}</Text>
</Button>
2023-01-31 06:44:07 +01:00
</Flexbox>
2023-01-25 16:42:46 +01:00
),
};
return (
2023-01-25 16:42:46 +01:00
<TableRow key={rowIndex} data={rowData} keys={keys}>
{(data, key, colIndex) => {
return (
2023-01-25 16:42:46 +01:00
<TableCell key={colIndex} style={{ ...columns[colIndex]?.style }}>
2023-01-31 06:44:07 +01:00
<Flexbox flex={1} alignItems="center">
2023-01-25 16:42:46 +01:00
{data[key]}
</Flexbox>
</TableCell>
);
}}
</TableRow>
);
}}
</TableBody>
</Table>
);
}