umami/components/pages/settings/websites/WebsiteSettings.js

83 lines
2.9 KiB
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react';
2023-01-21 02:12:53 +01:00
import { Breadcrumbs, Item, Tabs, useToast, Button, Text, Icon, Icons } from 'react-basics';
import { useRouter } from 'next/router';
import Link from 'next/link';
import Page from 'components/layout/Page';
2023-01-21 02:12:53 +01:00
import PageHeader from 'components/layout/PageHeader';
2023-01-10 08:59:26 +01:00
import WebsiteEditForm from 'components/pages/settings/websites/WebsiteEditForm';
2023-02-28 05:03:04 +01:00
import WebsiteData from 'components/pages/settings/websites/WebsiteData';
2023-01-10 08:59:26 +01:00
import TrackingCode from 'components/pages/settings/websites/TrackingCode';
import ShareUrl from 'components/pages/settings/websites/ShareUrl';
2023-01-21 02:12:53 +01:00
import useApi from 'hooks/useApi';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
import useConfig from 'hooks/useConfig';
2023-01-21 02:12:53 +01:00
export default function WebsiteSettings({ websiteId }) {
const router = useRouter();
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
const { openExternal } = useConfig();
2023-01-10 08:59:26 +01:00
const { get, useQuery } = useApi();
const { toast, showToast } = useToast();
const { data, isLoading } = useQuery(
['website', websiteId],
() => get(`/websites/${websiteId}`),
{ enabled: !!websiteId, cacheTime: 0 },
);
const [values, setValues] = useState(null);
const [tab, setTab] = useState('details');
const handleSave = data => {
2023-01-21 02:12:53 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
setValues(state => ({ ...state, ...data }));
};
const handleReset = async value => {
if (value === 'delete') {
await router.push('/websites');
}
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
return (
<Page loading={isLoading || !values}>
{toast}
2023-02-02 03:39:54 +01:00
<PageHeader
title={
<Breadcrumbs>
<Item>
<Link href="/settings/websites">{formatMessage(labels.websites)}</Link>
</Item>
<Item>{values?.name}</Item>
</Breadcrumbs>
}
>
<Link href={`/websites/${websiteId}`} target={openExternal ? '_blank' : null}>
<Button variant="primary">
<Icon>
<Icons.External />
</Icon>
<Text>{formatMessage(labels.view)}</Text>
</Button>
</Link>
</PageHeader>
2023-01-10 08:59:26 +01:00
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30 }}>
2023-01-25 16:42:46 +01:00
<Item key="details">{formatMessage(labels.details)}</Item>
<Item key="tracking">{formatMessage(labels.trackingCode)}</Item>
<Item key="share">{formatMessage(labels.shareUrl)}</Item>
<Item key="data">{formatMessage(labels.data)}</Item>
</Tabs>
2023-01-06 07:56:36 +01:00
{tab === 'details' && (
<WebsiteEditForm websiteId={websiteId} data={values} onSave={handleSave} />
)}
2023-01-10 08:59:26 +01:00
{tab === 'tracking' && <TrackingCode websiteId={websiteId} data={values} />}
{tab === 'share' && <ShareUrl websiteId={websiteId} data={values} onSave={handleSave} />}
2023-02-28 05:03:04 +01:00
{tab === 'data' && <WebsiteData websiteId={websiteId} onSave={handleReset} />}
</Page>
);
}