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

67 lines
2.1 KiB
JavaScript
Raw Normal View History

import { useState } from 'react';
2023-01-25 16:42:46 +01:00
import { Button, Icon, Modal, useToast, Icons, Text } from 'react-basics';
import { useIntl } from 'react-intl';
2022-12-28 05:20:44 +01:00
import useApi from 'hooks/useApi';
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-01-25 16:42:46 +01:00
import { labels, messages } from 'components/messages';
const { Plus } = Icons;
export default function TeamsList() {
2023-01-25 16:42:46 +01:00
const { formatMessage } = useIntl();
const [edit, setEdit] = useState(false);
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;
const { toast, showToast } = useToast();
const handleAdd = () => {
setEdit(true);
};
const handleSave = () => {
setEdit(false);
setUpdate(state => state + 1);
2023-01-25 16:42:46 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
const handleClose = () => {
setEdit(false);
};
return (
<Page loading={isLoading} error={error}>
{toast}
2023-01-25 16:42:46 +01:00
<PageHeader title={formatMessage(labels.team)}>
<Button variant="primary" onClick={handleAdd}>
<Icon>
<Plus />
</Icon>
<Text>{formatMessage(labels.createTeam)}</Text>
</Button>
</PageHeader>
2023-01-10 08:59:26 +01:00
{hasData && <TeamsTable data={data} />}
{!hasData && (
2023-01-25 16:42:46 +01:00
<EmptyPlaceholder message={formatMessage(messages.noTeams)}>
2023-01-10 08:59:26 +01:00
<Button variant="primary" onClick={handleAdd}>
2023-01-25 16:42:46 +01:00
<Icon>
<Plus />
</Icon>
<Text>{formatMessage(labels.createTeam)}</Text>
2023-01-10 08:59:26 +01:00
</Button>
</EmptyPlaceholder>
)}
{edit && (
2023-01-25 16:42:46 +01:00
<Modal title={formatMessage(labels.createTeam)} onClose={handleClose}>
{close => <TeamAddForm onSave={handleSave} onClose={close} />}
</Modal>
)}
</Page>
);
}