2020-08-31 00:29:31 +02:00
|
|
|
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-09-16 05:34:30 +02:00
|
|
|
import Link from 'components/common/Link';
|
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';
|
2020-08-23 04:05:07 +02:00
|
|
|
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-09-16 05:34:30 +02:00
|
|
|
import LinkIcon 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();
|
2020-10-09 21:39:03 +02:00
|
|
|
const { data } = useFetch(`/api/websites`, {}, [saved]);
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2020-08-12 07:24:41 +02:00
|
|
|
const Buttons = row => (
|
2020-09-22 06:34:55 +02:00
|
|
|
<ButtonLayout align="right">
|
2020-08-15 10:17:15 +02:00
|
|
|
{row.share_id && (
|
|
|
|
<Button
|
2020-09-16 05:34:30 +02:00
|
|
|
icon={<LinkIcon />}
|
2020-08-15 10:17:15 +02:00
|
|
|
size="small"
|
2020-09-17 07:29:40 +02:00
|
|
|
tooltip={<FormattedMessage id="message.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={
|
2020-09-17 07:29:40 +02:00
|
|
|
<FormattedMessage id="message.get-tracking-code" defaultMessage="Get tracking code" />
|
2020-09-06 02:27:01 +02:00
|
|
|
}
|
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-26 07:31:18 +02:00
|
|
|
<FormattedMessage id="button.edit" defaultMessage="Edit" />
|
2020-08-12 07:24:41 +02:00
|
|
|
</Button>
|
|
|
|
<Button icon={<Trash />} size="small" onClick={() => setDeleteWebsite(row)}>
|
2020-09-26 07:31:18 +02:00
|
|
|
<FormattedMessage id="button.delete" defaultMessage="Delete" />
|
2020-08-12 07:24:41 +02:00
|
|
|
</Button>
|
2020-08-23 04:05:07 +02:00
|
|
|
</ButtonLayout>
|
2020-08-12 07:24:41 +02:00
|
|
|
);
|
|
|
|
|
2020-09-16 05:34:30 +02:00
|
|
|
const DetailsLink = ({ website_id, name }) => (
|
|
|
|
<Link href="/website/[...id]" as={`/website/${website_id}/${name}`}>
|
|
|
|
{name}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
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" />,
|
2020-09-10 01:12:29 +02:00
|
|
|
className: 'col-6 col-xl-4',
|
2020-09-16 05:34:30 +02:00
|
|
|
render: DetailsLink,
|
2020-09-06 02:27:01 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'domain',
|
|
|
|
label: <FormattedMessage id="label.domain" defaultMessage="Domain" />,
|
2020-09-10 01:12:29 +02:00
|
|
|
className: 'col-6 col-xl-4',
|
2020-09-06 02:27:01 +02:00
|
|
|
},
|
2020-08-09 08:48:43 +02:00
|
|
|
{
|
|
|
|
key: 'action',
|
2020-09-10 01:12:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:54:03 +02:00
|
|
|
const empty = (
|
2020-09-06 02:27:01 +02:00
|
|
|
<EmptyPlaceholder
|
|
|
|
msg={
|
|
|
|
<FormattedMessage
|
2020-09-17 07:29:40 +02:00
|
|
|
id="message.no-websites-configured"
|
2020-09-06 02:27:01 +02:00
|
|
|
defaultMessage="You don't have any websites configured."
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2020-08-11 04:54:03 +02:00
|
|
|
<Button icon={<Plus />} size="medium" onClick={() => setAddWebsite(true)}>
|
2020-09-26 07:31:18 +02:00
|
|
|
<FormattedMessage id="button.add-website" defaultMessage="Add website" />
|
2020-08-11 04:54:03 +02:00
|
|
|
</Button>
|
|
|
|
</EmptyPlaceholder>
|
|
|
|
);
|
|
|
|
|
2020-08-09 08:48:43 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageHeader>
|
2020-09-06 02:27:01 +02:00
|
|
|
<div>
|
2020-09-17 07:29:40 +02:00
|
|
|
<FormattedMessage id="label.websites" defaultMessage="Websites" />
|
2020-09-06 02:27:01 +02:00
|
|
|
</div>
|
2020-08-09 08:48:43 +02:00
|
|
|
<Button icon={<Plus />} size="small" onClick={() => setAddWebsite(true)}>
|
2020-09-26 07:31:18 +02:00
|
|
|
<FormattedMessage id="button.add-website" defaultMessage="Add website" />
|
2020-08-09 08:48:43 +02:00
|
|
|
</Button>
|
|
|
|
</PageHeader>
|
2020-08-11 04:54:03 +02:00
|
|
|
<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
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|