Updated query hooks for teams and websites.

This commit is contained in:
Mike Cao 2024-01-29 01:32:05 -08:00
parent 9448aa3ab5
commit 2fa50892d8
61 changed files with 508 additions and 539 deletions

View file

@ -1,5 +1,5 @@
'use client';
import { useContext, useEffect, useState, Key } from 'react';
import { useState, Key } from 'react';
import { Item, Tabs, useToasts, Button, Text, Icon, Icons, Loading } from 'react-basics';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
@ -8,56 +8,42 @@ import WebsiteEditForm from './[id]/WebsiteEditForm';
import WebsiteData from './[id]/WebsiteData';
import TrackingCode from './[id]/TrackingCode';
import ShareUrl from './[id]/ShareUrl';
import { useApi } from 'components/hooks';
import { useMessages } from 'components/hooks';
import SettingsContext from '../SettingsContext';
import { useWebsite, useMessages } from 'components/hooks';
import { touch } from 'store/cache';
export function WebsiteSettings({ websiteId, openExternal = false }) {
const router = useRouter();
const { formatMessage, labels, messages } = useMessages();
const { get, useQuery } = useApi();
const { showToast } = useToasts();
const { websitesUrl, websitesPath, settingsPath } = useContext(SettingsContext);
const { data, isLoading } = useQuery({
queryKey: ['website', websiteId],
queryFn: () => get(`${websitesUrl}/${websiteId}`),
enabled: !!websiteId,
gcTime: 0,
});
const [values, setValues] = useState(null);
const { data: website, isLoading } = useWebsite(websiteId, { gcTime: 0 });
const [tab, setTab] = useState<Key>('details');
const showSuccess = () => {
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
const handleSave = (data: any) => {
const handleSave = () => {
showSuccess();
setValues((state: any) => ({ ...state, ...data }));
touch('websites');
};
const handleReset = async (value: string) => {
if (value === 'delete') {
router.push(settingsPath);
router.push('/settings/websites');
} else if (value === 'reset') {
showSuccess();
}
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
if (isLoading || !values) {
if (isLoading) {
return <Loading position="page" />;
}
return (
<>
<PageHeader title={values?.name}>
<Link href={`${websitesPath}/${websiteId}`} target={openExternal ? '_blank' : null}>
<PageHeader title={website?.name}>
<Link href={`/websites/${websiteId}`} target={openExternal ? '_blank' : null}>
<Button variant="primary">
<Icon>
<Icons.External />
@ -73,10 +59,10 @@ export function WebsiteSettings({ websiteId, openExternal = false }) {
<Item key="data">{formatMessage(labels.data)}</Item>
</Tabs>
{tab === 'details' && (
<WebsiteEditForm websiteId={websiteId} data={values} onSave={handleSave} />
<WebsiteEditForm websiteId={websiteId} data={website} onSave={handleSave} />
)}
{tab === 'tracking' && <TrackingCode websiteId={websiteId} />}
{tab === 'share' && <ShareUrl websiteId={websiteId} data={values} onSave={handleSave} />}
{tab === 'share' && <ShareUrl websiteId={websiteId} data={website} onSave={handleSave} />}
{tab === 'data' && <WebsiteData websiteId={websiteId} onSave={handleReset} />}
</>
);