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

71 lines
2.3 KiB
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react';
import { Breadcrumbs, Item, Tabs, useToast } from 'react-basics';
import Link from 'next/link';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
2023-02-02 20:59:38 +01:00
import { ROLES } from 'lib/constants';
import useUser from 'hooks/useUser';
import useApi from 'hooks/useApi';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-02-02 20:59:38 +01:00
import TeamEditForm from './TeamEditForm';
import TeamMembers from './TeamMembers';
2023-01-25 16:42:46 +01:00
import TeamWebsites from './TeamWebsites';
2023-01-25 16:42:46 +01:00
export default function TeamSettings({ teamId }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
2023-02-02 20:59:38 +01:00
const { user } = useUser();
const [values, setValues] = useState(null);
2023-01-10 08:59:26 +01:00
const [tab, setTab] = useState('details');
const { get, useQuery } = useApi();
const { toast, showToast } = useToast();
const { data, isLoading } = useQuery(
['team', teamId],
() => {
if (teamId) {
return get(`/teams/${teamId}`);
}
},
{ cacheTime: 0 },
);
2023-02-02 20:59:38 +01:00
const canEdit = data?.teamUser?.find(
({ userId, role }) => role === ROLES.teamOwner && userId === user.id,
);
const handleSave = data => {
2023-01-25 16:42:46 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
setValues(state => ({ ...state, ...data }));
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
return (
<Page loading={isLoading || !values}>
{toast}
2023-02-02 03:39:54 +01:00
<PageHeader
title={
<Breadcrumbs>
<Item>
2023-04-18 16:35:45 +02:00
<Link href="/settings/teams">{formatMessage(labels.teams)}</Link>
2023-02-02 03:39:54 +01:00
</Item>
<Item>{values?.name}</Item>
</Breadcrumbs>
}
/>
2023-01-25 16:42:46 +01:00
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30 }}>
<Item key="details">{formatMessage(labels.details)}</Item>
<Item key="members">{formatMessage(labels.members)}</Item>
<Item key="websites">{formatMessage(labels.websites)}</Item>
</Tabs>
2023-02-02 20:59:38 +01:00
{tab === 'details' && (
<TeamEditForm teamId={teamId} data={values} onSave={handleSave} readOnly={!canEdit} />
)}
{tab === 'members' && <TeamMembers teamId={teamId} readOnly={!canEdit} />}
{tab === 'websites' && <TeamWebsites teamId={teamId} readOnly={!canEdit} />}
</Page>
);
}