umami/components/pages/WebsiteSettings.js
Brian Cao e1f99a7d01
Rewrite admin. (#1713)
* Rewrite admin.

* Clean up password forms.

* Fix naming issues.

* CSS Naming.
2022-12-26 16:57:59 -08:00

77 lines
2.5 KiB
JavaScript

import { useEffect, useState } from 'react';
import { Breadcrumbs, Item, Tabs, useToast, Button, Icon } from 'react-basics';
import { useQuery } from '@tanstack/react-query';
import { useApi } from 'next-basics';
import Link from 'next/link';
import Page from 'components/layout/Page';
import WebsiteEditForm from 'components/forms/WebsiteEditForm';
import WebsiteReset from 'components/forms/WebsiteReset';
import PageHeader from 'components/layout/PageHeader';
import TrackingCodeForm from 'components/forms/TrackingCodeForm';
import ShareUrlForm from 'components/forms/ShareUrlForm';
import { getAuthToken } from 'lib/client';
import ExternalLink from 'assets/external-link.svg';
export default function Websites({ websiteId }) {
const [values, setValues] = useState(null);
const [tab, setTab] = useState('general');
const { get } = useApi(getAuthToken());
const { toast, showToast } = useToast();
const { data, isLoading } = useQuery(
['website', websiteId],
() => {
if (websiteId) {
return get(`/websites/${websiteId}`);
}
},
{ cacheTime: 0 },
);
const handleSave = data => {
showToast({ message: 'Saved successfully.', variant: 'success' });
setValues(state => ({ ...state, ...data }));
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
return (
<Page loading={isLoading || !values}>
{toast}
<PageHeader>
<Breadcrumbs>
<Item>
<Link href="/websites">Websites</Link>
</Item>
<Item>{values?.name}</Item>
</Breadcrumbs>
<Link href={`/analytics/websites/${websiteId}`}>
<a target="_blank">
<Button variant="primary">
<Icon>
<ExternalLink />
</Icon>
View
</Button>
</a>
</Link>
</PageHeader>
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30, fontSize: 14 }}>
<Item key="general">General</Item>
<Item key="tracking">Tracking code</Item>
<Item key="share">Share URL</Item>
<Item key="danger">Danger zone</Item>
</Tabs>
{tab === 'general' && (
<WebsiteEditForm websiteId={websiteId} data={values} onSave={handleSave} />
)}
{tab === 'tracking' && <TrackingCodeForm websiteId={websiteId} data={values} />}
{tab === 'share' && <ShareUrlForm websiteId={websiteId} data={values} onSave={handleSave} />}
{tab === 'danger' && <WebsiteReset websiteId={websiteId} onSave={handleSave} />}
</Page>
);
}