umami/components/WebsiteSettings.js

116 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-08-09 08:48:43 +02:00
import React, { useState, useEffect } from 'react';
import Table from './common/Table';
import Button from './common/Button';
import PageHeader from './layout/PageHeader';
import Pen from 'assets/pen.svg';
import Trash from 'assets/trash.svg';
import Plus from 'assets/plus.svg';
import Code from 'assets/code.svg';
import { get } from 'lib/web';
import Modal from './common/Modal';
import WebsiteEditForm from './forms/WebsiteEditForm';
2020-08-09 11:03:37 +02:00
import DeleteForm from './forms/DeleteForm';
2020-08-09 08:48:43 +02:00
import WebsiteCodeForm from './forms/WebsiteCodeForm';
import styles from './WebsiteSettings.module.css';
import EmptyPlaceholder from './common/EmptyPlaceholder';
import Arrow from '../assets/arrow-right.svg';
2020-08-09 08:48:43 +02:00
export default function WebsiteSettings() {
const [data, setData] = useState();
const [editWebsite, setEditWebsite] = useState();
const [deleteWebsite, setDeleteWebsite] = useState();
const [addWebsite, setAddWebsite] = useState();
const [showCode, setShowCode] = useState();
const [saved, setSaved] = useState(0);
const columns = [
{ key: 'name', label: 'Name', className: styles.col },
{ key: 'domain', label: 'Domain', className: styles.col },
{
key: 'action',
className: styles.buttons,
render: row => (
<>
<Button icon={<Code />} size="small" onClick={() => setShowCode(row)}>
<div>Get Code</div>
</Button>
<Button icon={<Pen />} size="small" onClick={() => setEditWebsite(row)}>
<div>Edit</div>
</Button>
<Button icon={<Trash />} size="small" onClick={() => setDeleteWebsite(row)}>
<div>Delete</div>
</Button>
</>
),
},
];
function handleSave() {
setSaved(state => state + 1);
handleClose();
}
function handleClose() {
setAddWebsite(null);
setEditWebsite(null);
setDeleteWebsite(null);
setShowCode(null);
}
async function loadData() {
setData(await get(`/api/website`));
}
useEffect(() => {
loadData();
}, [saved]);
if (!data) {
return null;
}
const empty = (
<EmptyPlaceholder msg={"You don't have any websites configured."}>
<Button icon={<Plus />} size="medium" onClick={() => setAddWebsite(true)}>
<div>Add website</div>
</Button>
</EmptyPlaceholder>
);
2020-08-09 08:48:43 +02:00
return (
<>
<PageHeader>
<div>Websites</div>
<Button icon={<Plus />} size="small" onClick={() => setAddWebsite(true)}>
<div>Add website</div>
</Button>
</PageHeader>
<Table columns={columns} rows={data} empty={empty} />
2020-08-09 08:48:43 +02:00
{editWebsite && (
<Modal title="Edit website">
<WebsiteEditForm values={editWebsite} onSave={handleSave} onClose={handleClose} />
</Modal>
)}
{addWebsite && (
<Modal title="Add website">
<WebsiteEditForm onSave={handleSave} onClose={handleClose} />
</Modal>
)}
{deleteWebsite && (
<Modal title="Delete website">
2020-08-09 11:03:37 +02:00
<DeleteForm
values={{ type: 'website', id: deleteWebsite.website_id, name: deleteWebsite.name }}
onSave={handleSave}
onClose={handleClose}
/>
2020-08-09 08:48:43 +02:00
</Modal>
)}
{showCode && (
<Modal title="Tracking code">
<WebsiteCodeForm values={showCode} onClose={handleClose} />
</Modal>
)}
</>
);
}