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

93 lines
2.9 KiB
JavaScript
Raw Normal View History

import { useState } from 'react';
2023-06-13 20:59:12 +02:00
import { Button, Icon, Modal, ModalTrigger, useToasts, Text, Flexbox } from 'react-basics';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
2023-01-10 08:59:26 +01:00
import TeamAddForm from 'components/pages/settings/teams/TeamAddForm';
import PageHeader from 'components/layout/PageHeader';
2023-01-10 08:59:26 +01:00
import TeamsTable from 'components/pages/settings/teams/TeamsTable';
import Page from 'components/layout/Page';
2023-02-02 03:39:54 +01:00
import Icons from 'components/icons';
2023-04-21 17:00:42 +02:00
import TeamJoinForm from './TeamJoinForm';
2023-03-22 22:05:55 +01:00
import useApi from 'hooks/useApi';
import useMessages from 'hooks/useMessages';
2023-07-07 08:02:16 +02:00
import { ROLES } from 'lib/constants';
import useUser from 'hooks/useUser';
2023-01-25 16:42:46 +01:00
export default function TeamsList() {
2023-07-07 08:02:16 +02:00
const { user } = useUser();
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
const [update, setUpdate] = useState(0);
2023-01-10 08:59:26 +01:00
const { get, useQuery } = useApi();
const { data, isLoading, error } = useQuery(['teams', update], () => get(`/teams`));
const hasData = data && data.length !== 0;
2023-06-13 20:59:12 +02:00
const { showToast } = useToasts();
const handleSave = () => {
setUpdate(state => state + 1);
2023-01-25 16:42:46 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
2023-02-02 03:39:54 +01:00
const handleJoin = () => {
2023-02-02 20:59:38 +01:00
setUpdate(state => state + 1);
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
const handleDelete = () => {
setUpdate(state => state + 1);
2023-02-02 03:39:54 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
const joinButton = (
<ModalTrigger>
<Button variant="secondary">
<Icon>
<Icons.AddUser />
</Icon>
<Text>{formatMessage(labels.joinTeam)}</Text>
</Button>
<Modal title={formatMessage(labels.joinTeam)}>
{close => <TeamJoinForm onSave={handleJoin} onClose={close} />}
</Modal>
</ModalTrigger>
);
const createButton = (
2023-07-07 08:02:16 +02:00
<>
{user.role !== ROLES.viewOnly && (
<ModalTrigger>
<Button variant="primary">
<Icon>
<Icons.Plus />
</Icon>
<Text>{formatMessage(labels.createTeam)}</Text>
</Button>
<Modal title={formatMessage(labels.createTeam)}>
{close => <TeamAddForm onSave={handleSave} onClose={close} />}
</Modal>
</ModalTrigger>
)}
</>
);
return (
<Page loading={isLoading} error={error}>
2023-02-28 05:03:04 +01:00
<PageHeader title={formatMessage(labels.teams)}>
2023-02-02 03:39:54 +01:00
{hasData && (
<Flexbox gap={10}>
{joinButton}
{createButton}
</Flexbox>
)}
</PageHeader>
2023-02-02 20:59:38 +01:00
{hasData && <TeamsTable data={data} onDelete={handleDelete} />}
{!hasData && (
2023-01-25 16:42:46 +01:00
<EmptyPlaceholder message={formatMessage(messages.noTeams)}>
<Flexbox gap={10}>
{joinButton}
{createButton}
</Flexbox>
</EmptyPlaceholder>
)}
</Page>
);
}