umami/components/pages/TeamsList.js
Brian Cao e1f99a7d01
Rewrite admin. (#1713)
* Rewrite admin.

* Clean up password forms.

* Fix naming issues.

* CSS Naming.
2022-12-26 16:57:59 -08:00

66 lines
2.0 KiB
JavaScript

import { useState } from 'react';
import { Button, Icon, Modal, useToast, Flexbox } from 'react-basics';
import { useApi } from 'next-basics';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import TeamAddForm from 'components/forms/TeamAddForm';
import PageHeader from 'components/layout/PageHeader';
import TeamsTable from 'components/tables/TeamsTable';
import Page from 'components/layout/Page';
import { getAuthToken } from 'lib/client';
import { useQuery } from '@tanstack/react-query';
export default function TeamsList() {
const [edit, setEdit] = useState(false);
const [update, setUpdate] = useState(0);
const { get } = useApi(getAuthToken());
const { data, isLoading, error } = useQuery(['teams', update], () => get(`/teams`));
const hasData = data && data.length !== 0;
const { toast, showToast } = useToast();
const columns = [
{ name: 'name', label: 'Name', style: { flex: 2 } },
{ name: 'action', label: ' ' },
];
const handleAdd = () => {
setEdit(true);
};
const handleSave = () => {
setEdit(false);
setUpdate(state => state + 1);
showToast({ message: 'Team saved.', variant: 'success' });
};
const handleClose = () => {
setEdit(false);
};
return (
<Page loading={isLoading} error={error}>
{toast}
<PageHeader title="Teams">
<Button onClick={handleAdd}>
<Icon icon="plus" /> Create team
</Button>
</PageHeader>
{hasData && <TeamsTable columns={columns} rows={data} />}
{!hasData && (
<EmptyPlaceholder msg="You don't have any teams configured.">
<Flexbox justifyContent="center" alignItems="center">
<Button variant="primary" onClick={handleAdd}>
<Icon icon="plus" /> Create team
</Button>
</Flexbox>
</EmptyPlaceholder>
)}
{edit && (
<Modal title="Create team" onClose={handleClose}>
{close => <TeamAddForm onSave={handleSave} onClose={close} />}
</Modal>
)}
</Page>
);
}