listmonk/frontend/src/Templates.js

444 lines
12 KiB
JavaScript
Raw Normal View History

2019-07-04 15:11:30 +02:00
import React from "react";
2019-03-09 08:46:47 +01:00
import {
Row,
Col,
Modal,
Form,
Input,
Button,
Table,
Icon,
Tooltip,
Tag,
Popconfirm,
Spin,
notification
2019-07-04 15:11:30 +02:00
} from "antd";
2018-10-25 15:51:47 +02:00
2019-07-04 15:11:30 +02:00
import ModalPreview from "./ModalPreview";
import Utils from "./utils";
import * as cs from "./constants";
2018-10-25 15:51:47 +02:00
class CreateFormDef extends React.PureComponent {
2019-03-09 08:46:47 +01:00
state = {
confirmDirty: false,
modalWaiting: false,
previewName: "",
previewBody: ""
2019-07-04 15:11:30 +02:00
};
2019-03-09 08:46:47 +01:00
// Handle create / edit form submission.
handleSubmit = e => {
2019-07-04 15:11:30 +02:00
e.preventDefault();
2019-03-09 08:46:47 +01:00
this.props.form.validateFields((err, values) => {
if (err) {
2019-07-04 15:11:30 +02:00
return;
2019-03-09 08:46:47 +01:00
}
2019-07-04 15:11:30 +02:00
this.setState({ modalWaiting: true });
2019-03-09 08:46:47 +01:00
if (this.props.formType === cs.FormCreate) {
// Create a new list.
this.props
.modelRequest(
cs.ModelTemplates,
cs.Routes.CreateTemplate,
cs.MethodPost,
values
)
.then(() => {
notification["success"]({
placement: cs.MsgPosition,
message: "Template added",
description: `"${values["name"]}" added`
2019-07-04 15:11:30 +02:00
});
this.props.fetchRecords();
this.props.onClose();
this.setState({ modalWaiting: false });
2019-03-09 08:46:47 +01:00
})
.catch(e => {
notification["error"]({
placement: cs.MsgPosition,
message: "Error",
description: e.message
2019-07-04 15:11:30 +02:00
});
this.setState({ modalWaiting: false });
});
2019-03-09 08:46:47 +01:00
} else {
// Edit a list.
this.props
.modelRequest(
cs.ModelTemplates,
cs.Routes.UpdateTemplate,
cs.MethodPut,
{ ...values, id: this.props.record.id }
)
.then(() => {
notification["success"]({
placement: cs.MsgPosition,
message: "Template updated",
description: `"${values["name"]}" modified`
2019-07-04 15:11:30 +02:00
});
this.props.fetchRecords();
this.props.onClose();
this.setState({ modalWaiting: false });
2019-03-09 08:46:47 +01:00
})
.catch(e => {
notification["error"]({
placement: cs.MsgPosition,
message: "Error",
description: e.message
2019-07-04 15:11:30 +02:00
});
this.setState({ modalWaiting: false });
});
2019-03-09 08:46:47 +01:00
}
2019-07-04 15:11:30 +02:00
});
};
2019-03-09 08:46:47 +01:00
handleConfirmBlur = e => {
2019-07-04 15:11:30 +02:00
const value = e.target.value;
this.setState({ confirmDirty: this.state.confirmDirty || !!value });
};
2019-03-09 08:46:47 +01:00
handlePreview = (name, body) => {
2019-07-04 15:11:30 +02:00
this.setState({ previewName: name, previewBody: body });
};
2019-03-09 08:46:47 +01:00
render() {
2019-07-04 15:11:30 +02:00
const { formType, record, onClose } = this.props;
const { getFieldDecorator } = this.props.form;
2019-03-09 08:46:47 +01:00
const formItemLayout = {
labelCol: { xs: { span: 16 }, sm: { span: 4 } },
wrapperCol: { xs: { span: 16 }, sm: { span: 18 } }
2019-07-04 15:11:30 +02:00
};
2018-10-25 15:51:47 +02:00
2019-03-09 08:46:47 +01:00
if (formType === null) {
2019-07-04 15:11:30 +02:00
return null;
}
2019-03-09 08:46:47 +01:00
return (
<div>
<Modal
visible={true}
title={formType === cs.FormCreate ? "Add template" : record.name}
okText={this.state.form === cs.FormCreate ? "Add" : "Save"}
width="90%"
height={900}
confirmLoading={this.state.modalWaiting}
onCancel={onClose}
onOk={this.handleSubmit}
>
<Spin
spinning={
this.props.reqStates[cs.ModelTemplates] === cs.StatePending
}
>
<Form onSubmit={this.handleSubmit}>
<Form.Item {...formItemLayout} label="Name">
{getFieldDecorator("name", {
initialValue: record.name,
rules: [{ required: true }]
2019-03-28 13:34:27 +01:00
})(<Input autoFocus maxLength={200} />)}
2019-03-09 08:46:47 +01:00
</Form.Item>
<Form.Item {...formItemLayout} name="body" label="Raw HTML">
{getFieldDecorator("body", {
initialValue: record.body ? record.body : "",
rules: [{ required: true }]
})(<Input.TextArea autosize={{ minRows: 10, maxRows: 30 }} />)}
</Form.Item>
{this.props.form.getFieldValue("body") !== "" && (
<Form.Item {...formItemLayout} colon={false} label="&nbsp;">
<Button
icon="search"
onClick={() =>
this.handlePreview(
this.props.form.getFieldValue("name"),
this.props.form.getFieldValue("body")
)
}
>
Preview
</Button>
</Form.Item>
)}
</Form>
</Spin>
<Row>
<Col span="4" />
<Col span="18" className="text-grey text-small">
The placeholder{" "}
<code>
{"{"}
{"{"} template "content" . {"}"}
{"}"}
</code>{" "}
should appear in the template.{" "}
2019-07-04 15:11:30 +02:00
<a
href="https://listmonk.app/docs/templating"
target="_blank"
rel="noopener noreferrer"
>
Learn more <Icon type="link" />.
2019-03-09 08:46:47 +01:00
</a>
.
</Col>
</Row>
</Modal>
{this.state.previewBody && (
<ModalPreview
title={
this.state.previewName
? this.state.previewName
: "Template preview"
}
previewURL={cs.Routes.PreviewNewTemplate}
body={this.state.previewBody}
onCancel={() => {
2019-07-04 15:11:30 +02:00
this.setState({ previewBody: null, previewName: null });
2019-03-09 08:46:47 +01:00
}}
/>
)}
</div>
2019-07-04 15:11:30 +02:00
);
2019-03-09 08:46:47 +01:00
}
2018-10-25 15:51:47 +02:00
}
2019-07-04 15:11:30 +02:00
const CreateForm = Form.create()(CreateFormDef);
2018-10-25 15:51:47 +02:00
class Templates extends React.PureComponent {
2019-03-09 08:46:47 +01:00
state = {
formType: null,
record: {},
previewRecord: null
2019-07-04 15:11:30 +02:00
};
2019-03-09 08:46:47 +01:00
constructor(props) {
2019-07-04 15:11:30 +02:00
super(props);
2019-03-09 08:46:47 +01:00
this.columns = [
{
title: "Name",
dataIndex: "name",
sorter: true,
width: "50%",
render: (text, record) => {
return (
<div className="name">
<a role="button" onClick={() => this.handleShowEditForm(record)}>
{text}
</a>
{record.is_default && (
<div>
<Tag>Default</Tag>
</div>
)}
</div>
2019-07-04 15:11:30 +02:00
);
2019-03-09 08:46:47 +01:00
}
},
{
title: "Created",
dataIndex: "created_at",
render: (date, _) => {
2019-07-04 15:11:30 +02:00
return Utils.DateString(date);
2019-03-09 08:46:47 +01:00
}
},
{
title: "Updated",
dataIndex: "updated_at",
render: (date, _) => {
2019-07-04 15:11:30 +02:00
return Utils.DateString(date);
2019-03-09 08:46:47 +01:00
}
},
{
title: "",
dataIndex: "actions",
width: "20%",
className: "actions",
render: (text, record) => {
return (
<div className="actions">
<Tooltip
title="Preview template"
onClick={() => this.handlePreview(record)}
>
<a role="button">
<Icon type="search" />
</a>
</Tooltip>
{!record.is_default && (
<Popconfirm
title="Are you sure?"
onConfirm={() => this.handleSetDefault(record)}
>
<Tooltip title="Set as default" placement="bottom">
<a role="button">
<Icon type="check" />
</a>
</Tooltip>
</Popconfirm>
)}
<Tooltip title="Edit template">
<a
role="button"
onClick={() => this.handleShowEditForm(record)}
>
<Icon type="edit" />
</a>
</Tooltip>
{record.id !== 1 && (
<Popconfirm
title="Are you sure?"
onConfirm={() => this.handleDeleteRecord(record)}
>
<Tooltip title="Delete template" placement="bottom">
<a role="button">
<Icon type="delete" />
</a>
</Tooltip>
</Popconfirm>
)}
</div>
2019-07-04 15:11:30 +02:00
);
2019-03-09 08:46:47 +01:00
}
}
2019-07-04 15:11:30 +02:00
];
2019-03-09 08:46:47 +01:00
}
componentDidMount() {
2019-07-04 15:11:30 +02:00
this.props.pageTitle("Templates");
this.fetchRecords();
2019-03-09 08:46:47 +01:00
}
fetchRecords = () => {
this.props.modelRequest(
cs.ModelTemplates,
cs.Routes.GetTemplates,
cs.MethodGet
2019-07-04 15:11:30 +02:00
);
};
2019-03-09 08:46:47 +01:00
handleDeleteRecord = record => {
this.props
.modelRequest(
cs.ModelTemplates,
cs.Routes.DeleteTemplate,
cs.MethodDelete,
{ id: record.id }
)
.then(() => {
notification["success"]({
placement: cs.MsgPosition,
message: "Template deleted",
description: `"${record.name}" deleted`
2019-07-04 15:11:30 +02:00
});
2018-10-25 15:51:47 +02:00
2019-03-09 08:46:47 +01:00
// Reload the table.
2019-07-04 15:11:30 +02:00
this.fetchRecords();
2019-03-09 08:46:47 +01:00
})
.catch(e => {
2019-07-04 15:11:30 +02:00
notification["error"]({ message: "Error", description: e.message });
});
};
2019-03-09 08:46:47 +01:00
handleSetDefault = record => {
this.props
.modelRequest(
cs.ModelTemplates,
cs.Routes.SetDefaultTemplate,
cs.MethodPut,
{ id: record.id }
)
.then(() => {
notification["success"]({
placement: cs.MsgPosition,
message: "Template updated",
description: `"${record.name}" set as default`
2019-07-04 15:11:30 +02:00
});
2018-10-25 15:51:47 +02:00
2019-03-09 08:46:47 +01:00
// Reload the table.
2019-07-04 15:11:30 +02:00
this.fetchRecords();
2019-03-09 08:46:47 +01:00
})
.catch(e => {
notification["error"]({
placement: cs.MsgPosition,
message: "Error",
description: e.message
2019-07-04 15:11:30 +02:00
});
});
};
2019-03-09 08:46:47 +01:00
handlePreview = record => {
2019-07-04 15:11:30 +02:00
this.setState({ previewRecord: record });
};
2019-03-09 08:46:47 +01:00
hideForm = () => {
2019-07-04 15:11:30 +02:00
this.setState({ formType: null });
};
2019-03-09 08:46:47 +01:00
handleShowCreateForm = () => {
2019-07-04 15:11:30 +02:00
this.setState({ formType: cs.FormCreate, record: {} });
};
2019-03-09 08:46:47 +01:00
handleShowEditForm = record => {
2019-07-04 15:11:30 +02:00
this.setState({ formType: cs.FormEdit, record: record });
};
2019-03-09 08:46:47 +01:00
render() {
return (
<section className="content templates">
<Row>
2019-08-04 17:50:28 +02:00
<Col xs={12} sm={14}>
2019-03-09 08:46:47 +01:00
<h1>Templates ({this.props.data[cs.ModelTemplates].length}) </h1>
</Col>
2019-08-04 17:50:28 +02:00
<Col xs={12} sm={10} className="align-right">
2019-03-09 08:46:47 +01:00
<Button
type="primary"
icon="plus"
onClick={this.handleShowCreateForm}
>
Add template
</Button>
</Col>
</Row>
<br />
<Table
columns={this.columns}
rowKey={record => record.id}
dataSource={this.props.data[cs.ModelTemplates]}
loading={this.props.reqStates[cs.ModelTemplates] !== cs.StateDone}
pagination={false}
/>
<CreateForm
{...this.props}
formType={this.state.formType}
record={this.state.record}
onClose={this.hideForm}
fetchRecords={this.fetchRecords}
/>
{this.state.previewRecord && (
<ModalPreview
title={this.state.previewRecord.name}
previewURL={cs.Routes.PreviewTemplate.replace(
":id",
this.state.previewRecord.id
)}
onCancel={() => {
2019-07-04 15:11:30 +02:00
this.setState({ previewRecord: null });
2019-03-09 08:46:47 +01:00
}}
/>
)}
</section>
2019-07-04 15:11:30 +02:00
);
2019-03-09 08:46:47 +01:00
}
2018-10-25 15:51:47 +02:00
}
2019-07-04 15:11:30 +02:00
export default Templates;