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

90 lines
3.1 KiB
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react';
2023-06-13 20:59:12 +02:00
import { Breadcrumbs, Item, Tabs, useToasts, 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
2023-04-21 17:00:42 +02:00
export 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();
2023-06-13 20:59:12 +02:00
const { showToast } = useToasts();
const { data, isLoading } = useQuery(
['website', websiteId],
() => get(`/websites/${websiteId}`),
{ enabled: !!websiteId, cacheTime: 0 },
);
const [values, setValues] = useState(null);
const [tab, setTab] = useState('details');
2023-04-05 18:55:21 +02:00
const showSuccess = () => {
2023-01-21 02:12:53 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
2023-04-05 18:55:21 +02:00
};
const handleSave = data => {
showSuccess();
setValues(state => ({ ...state, ...data }));
};
const handleReset = async value => {
if (value === 'delete') {
2023-04-05 18:55:21 +02:00
await router.push('/settings/websites');
} else if (value === 'reset') {
showSuccess();
}
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
return (
<Page loading={isLoading || !values}>
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>
);
}
2023-04-21 17:00:42 +02:00
export default WebsiteSettings;