2022-12-27 01:57:59 +01:00
|
|
|
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';
|
2022-12-27 01:57:59 +01:00
|
|
|
|
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();
|
2022-12-27 01:57:59 +01:00
|
|
|
const [values, setValues] = useState(null);
|
2023-01-10 08:59:26 +01:00
|
|
|
const [tab, setTab] = useState('details');
|
|
|
|
const { get, useQuery } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
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,
|
|
|
|
);
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
const handleSave = data => {
|
2023-01-25 16:42:46 +01:00
|
|
|
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
2022-12-27 01:57:59 +01:00
|
|
|
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>
|
2022-12-27 01:57:59 +01:00
|
|
|
</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} />}
|
2022-12-27 01:57:59 +01:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|