umami/components/settings/WebsiteSettings.js

167 lines
5.5 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
2020-08-18 07:47:58 +02:00
import classNames from 'classnames';
2020-08-12 07:24:41 +02:00
import Table from 'components/common/Table';
import Button from 'components/common/Button';
import PageHeader from 'components/layout/PageHeader';
import Modal from 'components/common/Modal';
2020-09-06 02:27:01 +02:00
import WebsiteEditForm from 'components/forms/WebsiteEditForm';
import DeleteForm from 'components/forms/DeleteForm';
import TrackingCodeForm from 'components/forms/TrackingCodeForm';
import ShareUrlForm from 'components/forms/ShareUrlForm';
2020-08-12 07:24:41 +02:00
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import ButtonLayout from 'components/layout/ButtonLayout';
2020-09-06 02:27:01 +02:00
import Toast from 'components/common/Toast';
2020-08-09 08:48:43 +02:00
import Pen from 'assets/pen.svg';
import Trash from 'assets/trash.svg';
import Plus from 'assets/plus.svg';
import Code from 'assets/code.svg';
2020-08-15 10:17:15 +02:00
import Link from 'assets/link.svg';
2020-09-06 02:27:01 +02:00
import useFetch from 'hooks/useFetch';
2020-08-09 08:48:43 +02:00
import styles from './WebsiteSettings.module.css';
export default function WebsiteSettings() {
const [editWebsite, setEditWebsite] = useState();
const [deleteWebsite, setDeleteWebsite] = useState();
const [addWebsite, setAddWebsite] = useState();
const [showCode, setShowCode] = useState();
2020-08-15 10:17:15 +02:00
const [showUrl, setShowUrl] = useState();
2020-08-09 08:48:43 +02:00
const [saved, setSaved] = useState(0);
2020-09-01 00:57:59 +02:00
const [message, setMessage] = useState();
const { data } = useFetch(`/api/websites`, {}, { update: [saved] });
2020-08-09 08:48:43 +02:00
2020-08-12 07:24:41 +02:00
const Buttons = row => (
<ButtonLayout>
2020-08-15 10:17:15 +02:00
{row.share_id && (
<Button
icon={<Link />}
size="small"
2020-09-06 02:27:01 +02:00
tooltip={<FormattedMessage id="tooltip.get-share-url" defaultMessage="Get share URL" />}
2020-08-15 10:17:15 +02:00
tooltipId={`button-share-${row.website_id}`}
onClick={() => setShowUrl(row)}
/>
)}
<Button
icon={<Code />}
size="small"
2020-09-06 02:27:01 +02:00
tooltip={
<FormattedMessage id="tooltip.get-tracking-code" defaultMessage="Get tracking code" />
}
2020-08-15 10:17:15 +02:00
tooltipId={`button-code-${row.website_id}`}
onClick={() => setShowCode(row)}
/>
2020-08-12 07:24:41 +02:00
<Button icon={<Pen />} size="small" onClick={() => setEditWebsite(row)}>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="button.edit" defaultMessage="Edit" />
</div>
2020-08-12 07:24:41 +02:00
</Button>
<Button icon={<Trash />} size="small" onClick={() => setDeleteWebsite(row)}>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="button.delete" defaultMessage="Delete" />
</div>
2020-08-12 07:24:41 +02:00
</Button>
</ButtonLayout>
2020-08-12 07:24:41 +02:00
);
2020-08-09 08:48:43 +02:00
const columns = [
2020-09-06 02:27:01 +02:00
{
key: 'name',
label: <FormattedMessage id="label.name" defaultMessage="Name" />,
className: 'col-6 col-xl-4',
2020-09-06 02:27:01 +02:00
},
{
key: 'domain',
label: <FormattedMessage id="label.domain" defaultMessage="Domain" />,
className: 'col-6 col-xl-4',
2020-09-06 02:27:01 +02:00
},
2020-08-09 08:48:43 +02:00
{
key: 'action',
className: classNames(styles.buttons, 'col-12 col-xl-4 pt-2 pt-xl-0'),
2020-08-12 07:24:41 +02:00
render: Buttons,
2020-08-09 08:48:43 +02:00
},
];
function handleSave() {
setSaved(state => state + 1);
2020-09-06 02:27:01 +02:00
setMessage(<FormattedMessage id="message.save-success" defaultMessage="Saved successfully." />);
2020-08-09 08:48:43 +02:00
handleClose();
}
function handleClose() {
setAddWebsite(null);
setEditWebsite(null);
setDeleteWebsite(null);
setShowCode(null);
2020-08-15 10:17:15 +02:00
setShowUrl(null);
2020-08-09 08:48:43 +02:00
}
if (!data) {
return null;
}
const empty = (
2020-09-06 02:27:01 +02:00
<EmptyPlaceholder
msg={
<FormattedMessage
id="placeholder.message.no-websites-configured"
defaultMessage="You don't have any websites configured."
/>
}
>
<Button icon={<Plus />} size="medium" onClick={() => setAddWebsite(true)}>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="button.add-website" defaultMessage="Add website" />
</div>
</Button>
</EmptyPlaceholder>
);
2020-08-09 08:48:43 +02:00
return (
<>
<PageHeader>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="settings.websites" defaultMessage="Websites" />
</div>
2020-08-09 08:48:43 +02:00
<Button icon={<Plus />} size="small" onClick={() => setAddWebsite(true)}>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="button.add-website" defaultMessage="Add website" />
</div>
2020-08-09 08:48:43 +02:00
</Button>
</PageHeader>
<Table columns={columns} rows={data} empty={empty} />
2020-08-09 08:48:43 +02:00
{editWebsite && (
2020-09-06 02:27:01 +02:00
<Modal title={<FormattedMessage id="title.edit-website" defaultMessage="Edit website" />}>
2020-08-09 08:48:43 +02:00
<WebsiteEditForm values={editWebsite} onSave={handleSave} onClose={handleClose} />
</Modal>
)}
{addWebsite && (
2020-09-06 02:27:01 +02:00
<Modal title={<FormattedMessage id="title.add-website" defaultMessage="Add website" />}>
2020-08-09 08:48:43 +02:00
<WebsiteEditForm onSave={handleSave} onClose={handleClose} />
</Modal>
)}
{deleteWebsite && (
2020-09-06 02:27:01 +02:00
<Modal
title={<FormattedMessage id="title.delete-website" defaultMessage="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 && (
2020-09-06 02:27:01 +02:00
<Modal title={<FormattedMessage id="title.tracking-code" defaultMessage="Tracking code" />}>
2020-08-15 10:17:15 +02:00
<TrackingCodeForm values={showCode} onClose={handleClose} />
</Modal>
)}
{showUrl && (
2020-09-06 02:27:01 +02:00
<Modal title={<FormattedMessage id="title.share-url" defaultMessage="Share URL" />}>
2020-08-15 10:17:15 +02:00
<ShareUrlForm values={showUrl} onClose={handleClose} />
2020-08-09 08:48:43 +02:00
</Modal>
)}
2020-09-01 00:57:59 +02:00
{message && <Toast message={message} onClose={() => setMessage(null)} />}
2020-08-09 08:48:43 +02:00
</>
);
}