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

88 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';
2023-01-25 16:42:46 +01:00
import { useIntl } from 'react-intl';
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';
import WebsiteReset from 'components/pages/settings/websites/WebsiteReset';
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-01-25 16:42:46 +01:00
import { labels, messages } from 'components/messages';
2023-01-21 02:12:53 +01:00
export default function WebsiteSettings({ websiteId }) {
const router = useRouter();
2023-01-21 02:12:53 +01:00
const { formatMessage } = useIntl();
const [values, setValues] = useState(null);
2023-01-06 07:56:36 +01:00
const [tab, setTab] = useState('details');
2023-01-10 08:59:26 +01:00
const { get, useQuery } = useApi();
const { toast, showToast } = useToast();
const { data, isLoading } = useQuery(
['website', websiteId],
() => {
if (websiteId) {
return get(`/websites/${websiteId}`);
}
},
{ cacheTime: 0 },
);
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>
}
>
2023-01-10 09:13:32 +01:00
<Link href={`/websites/${websiteId}`}>
<a>
<Button variant="primary">
<Icon>
<Icons.External />
</Icon>
2023-01-25 16:42:46 +01:00
<Text>{formatMessage(labels.view)}</Text>
</Button>
</a>
</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} />}
{tab === 'data' && <WebsiteReset websiteId={websiteId} onSave={handleReset} />}
</Page>
);
}