2022-12-27 01:57:59 +01:00
|
|
|
import { useState } from 'react';
|
2023-02-02 03:39:54 +01:00
|
|
|
import { Button, Icon, Modal, ModalTrigger, useToast, Text, Flexbox } from 'react-basics';
|
2023-01-25 16:42:46 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2022-12-27 01:57:59 +01:00
|
|
|
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
2023-01-10 08:59:26 +01:00
|
|
|
import TeamAddForm from 'components/pages/settings/teams/TeamAddForm';
|
2022-12-27 01:57:59 +01:00
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2023-01-10 08:59:26 +01:00
|
|
|
import TeamsTable from 'components/pages/settings/teams/TeamsTable';
|
2022-12-27 01:57:59 +01:00
|
|
|
import Page from 'components/layout/Page';
|
2023-01-25 16:42:46 +01:00
|
|
|
import { labels, messages } from 'components/messages';
|
2023-02-02 03:39:54 +01:00
|
|
|
import Icons from 'components/icons';
|
|
|
|
import TeamJoinForm from './JoinTeamForm';
|
2023-01-25 16:42:46 +01:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
export default function TeamsList() {
|
2023-01-25 16:42:46 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2022-12-27 01:57:59 +01:00
|
|
|
const [update, setUpdate] = useState(0);
|
2023-01-10 08:59:26 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
const { data, isLoading, error } = useQuery(['teams', update], () => get(`/teams`));
|
|
|
|
const hasData = data && data.length !== 0;
|
|
|
|
const { toast, showToast } = useToast();
|
|
|
|
|
|
|
|
const handleSave = () => {
|
|
|
|
setUpdate(state => state + 1);
|
2023-01-25 16:42:46 +01:00
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
2022-12-27 01:57:59 +01:00
|
|
|
};
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
|
2023-01-28 06:53:13 +01:00
|
|
|
const createButton = (
|
|
|
|
<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>
|
|
|
|
);
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Page loading={isLoading} error={error}>
|
|
|
|
{toast}
|
2023-02-02 03:39:54 +01:00
|
|
|
<PageHeader title={formatMessage(labels.team)}>
|
|
|
|
{hasData && (
|
|
|
|
<Flexbox gap={10}>
|
|
|
|
{joinButton}
|
|
|
|
{createButton}
|
|
|
|
</Flexbox>
|
|
|
|
)}
|
|
|
|
</PageHeader>
|
2023-02-02 20:59:38 +01:00
|
|
|
{hasData && <TeamsTable data={data} onDelete={handleDelete} />}
|
2022-12-27 01:57:59 +01:00
|
|
|
{!hasData && (
|
2023-01-25 16:42:46 +01:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noTeams)}>
|
2023-01-28 06:53:13 +01:00
|
|
|
{createButton}
|
2022-12-27 01:57:59 +01:00
|
|
|
</EmptyPlaceholder>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|