2022-12-27 01:57:59 +01:00
|
|
|
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-28 06:53:13 +01:00
|
|
|
import { useRouter } from 'next/router';
|
2022-12-27 01:57:59 +01:00
|
|
|
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';
|
2023-03-30 08:08:26 +02:00
|
|
|
import useConfig from 'hooks/useConfig';
|
2023-01-21 02:12:53 +01:00
|
|
|
|
|
|
|
export default function WebsiteSettings({ websiteId }) {
|
2023-01-28 06:53:13 +01:00
|
|
|
const router = useRouter();
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2023-03-30 08:08:26 +02:00
|
|
|
const { openExternal } = useConfig();
|
2023-01-10 08:59:26 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
const { toast, showToast } = useToast();
|
|
|
|
const { data, isLoading } = useQuery(
|
|
|
|
['website', websiteId],
|
2023-03-30 08:08:26 +02:00
|
|
|
() => get(`/websites/${websiteId}`),
|
|
|
|
{ enabled: !!websiteId, cacheTime: 0 },
|
2022-12-27 01:57:59 +01:00
|
|
|
);
|
2023-03-30 08:08:26 +02:00
|
|
|
const [values, setValues] = useState(null);
|
|
|
|
const [tab, setTab] = useState('details');
|
2022-12-27 01:57:59 +01:00
|
|
|
|
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();
|
2022-12-27 01:57:59 +01:00
|
|
|
setValues(state => ({ ...state, ...data }));
|
|
|
|
};
|
|
|
|
|
2023-01-28 06:53:13 +01:00
|
|
|
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();
|
2023-01-28 06:53:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
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-03-30 08:08:26 +02:00
|
|
|
<Link href={`/websites/${websiteId}`} target={openExternal ? '_blank' : null}>
|
2023-03-09 05:23:32 +01:00
|
|
|
<Button variant="primary">
|
|
|
|
<Icon>
|
|
|
|
<Icons.External />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(labels.view)}</Text>
|
|
|
|
</Button>
|
2022-12-27 01:57:59 +01:00
|
|
|
</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>
|
2022-12-27 01:57:59 +01:00
|
|
|
</Tabs>
|
2023-01-06 07:56:36 +01:00
|
|
|
{tab === 'details' && (
|
2022-12-27 01:57:59 +01:00
|
|
|
<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} />}
|
2022-12-27 01:57:59 +01:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|