Lists.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import React from "react"
  2. import { Link } from "react-router-dom"
  3. import {
  4. Row,
  5. Col,
  6. Modal,
  7. Form,
  8. Input,
  9. Select,
  10. Button,
  11. Table,
  12. Icon,
  13. Tooltip,
  14. Tag,
  15. Popconfirm,
  16. Spin,
  17. notification
  18. } from "antd"
  19. import Utils from "./utils"
  20. import * as cs from "./constants"
  21. const tagColors = {
  22. private: "orange",
  23. public: "green"
  24. }
  25. class CreateFormDef extends React.PureComponent {
  26. state = {
  27. confirmDirty: false,
  28. modalWaiting: false
  29. }
  30. // Handle create / edit form submission.
  31. handleSubmit = e => {
  32. e.preventDefault()
  33. this.props.form.validateFields((err, values) => {
  34. if (err) {
  35. return
  36. }
  37. this.setState({ modalWaiting: true })
  38. if (this.props.formType === cs.FormCreate) {
  39. // Create a new list.
  40. this.props
  41. .modelRequest(
  42. cs.ModelLists,
  43. cs.Routes.CreateList,
  44. cs.MethodPost,
  45. values
  46. )
  47. .then(() => {
  48. notification["success"]({
  49. placement: cs.MsgPosition,
  50. message: "List created",
  51. description: `"${values["name"]}" created`
  52. })
  53. this.props.fetchRecords()
  54. this.props.onClose()
  55. this.setState({ modalWaiting: false })
  56. })
  57. .catch(e => {
  58. notification["error"]({ message: "Error", description: e.message })
  59. this.setState({ modalWaiting: false })
  60. })
  61. } else {
  62. // Edit a list.
  63. this.props
  64. .modelRequest(cs.ModelLists, cs.Routes.UpdateList, cs.MethodPut, {
  65. ...values,
  66. id: this.props.record.id
  67. })
  68. .then(() => {
  69. notification["success"]({
  70. placement: cs.MsgPosition,
  71. message: "List modified",
  72. description: `"${values["name"]}" modified`
  73. })
  74. this.props.fetchRecords()
  75. this.props.onClose()
  76. this.setState({ modalWaiting: false })
  77. })
  78. .catch(e => {
  79. notification["error"]({
  80. placement: cs.MsgPosition,
  81. message: "Error",
  82. description: e.message
  83. })
  84. this.setState({ modalWaiting: false })
  85. })
  86. }
  87. })
  88. }
  89. modalTitle(formType, record) {
  90. if (formType === cs.FormCreate) {
  91. return "Create a list"
  92. }
  93. return (
  94. <div>
  95. <Tag
  96. color={
  97. tagColors.hasOwnProperty(record.type) ? tagColors[record.type] : ""
  98. }
  99. >
  100. {record.type}
  101. </Tag>{" "}
  102. {record.name}
  103. <br />
  104. <span className="text-tiny text-grey">
  105. ID {record.id} / UUID {record.uuid}
  106. </span>
  107. </div>
  108. )
  109. }
  110. render() {
  111. const { formType, record, onClose } = this.props
  112. const { getFieldDecorator } = this.props.form
  113. const formItemLayout = {
  114. labelCol: { xs: { span: 16 }, sm: { span: 4 } },
  115. wrapperCol: { xs: { span: 16 }, sm: { span: 18 } }
  116. }
  117. if (formType === null) {
  118. return null
  119. }
  120. return (
  121. <Modal
  122. visible={true}
  123. title={this.modalTitle(this.state.form, record)}
  124. okText={this.state.form === cs.FormCreate ? "Create" : "Save"}
  125. confirmLoading={this.state.modalWaiting}
  126. onCancel={onClose}
  127. onOk={this.handleSubmit}
  128. >
  129. <div id="modal-alert-container" />
  130. <Spin
  131. spinning={this.props.reqStates[cs.ModelLists] === cs.StatePending}
  132. >
  133. <Form onSubmit={this.handleSubmit}>
  134. <Form.Item {...formItemLayout} label="Name">
  135. {getFieldDecorator("name", {
  136. initialValue: record.name,
  137. rules: [{ required: true }]
  138. })(<Input autoFocus maxLength="200" />)}
  139. </Form.Item>
  140. <Form.Item
  141. {...formItemLayout}
  142. name="type"
  143. label="Type"
  144. extra="Public lists are open to the world to subscribe"
  145. >
  146. {getFieldDecorator("type", {
  147. initialValue: record.type ? record.type : "private",
  148. rules: [{ required: true }]
  149. })(
  150. <Select style={{ maxWidth: 120 }}>
  151. <Select.Option value="private">Private</Select.Option>
  152. <Select.Option value="public">Public</Select.Option>
  153. </Select>
  154. )}
  155. </Form.Item>
  156. <Form.Item
  157. {...formItemLayout}
  158. label="Tags"
  159. extra="Hit Enter after typing a word to add multiple tags"
  160. >
  161. {getFieldDecorator("tags", { initialValue: record.tags })(
  162. <Select mode="tags" />
  163. )}
  164. </Form.Item>
  165. </Form>
  166. </Spin>
  167. </Modal>
  168. )
  169. }
  170. }
  171. const CreateForm = Form.create()(CreateFormDef)
  172. class Lists extends React.PureComponent {
  173. state = {
  174. formType: null,
  175. record: {}
  176. }
  177. constructor(props) {
  178. super(props)
  179. this.columns = [
  180. {
  181. title: "Name",
  182. dataIndex: "name",
  183. sorter: true,
  184. width: "40%",
  185. render: (text, record) => {
  186. const out = []
  187. out.push(
  188. <div className="name" key={`name-${record.id}`}>
  189. <a role="button" onClick={() => this.handleShowEditForm(record)}>{text}</a>
  190. </div>
  191. )
  192. if (record.tags.length > 0) {
  193. for (let i = 0; i < record.tags.length; i++) {
  194. out.push(<Tag key={`tag-${i}`}>{record.tags[i]}</Tag>)
  195. }
  196. }
  197. return out
  198. }
  199. },
  200. {
  201. title: "Type",
  202. dataIndex: "type",
  203. width: "10%",
  204. render: (type, _) => {
  205. let color = type === "private" ? "orange" : "green"
  206. return <Tag color={color}>{type}</Tag>
  207. }
  208. },
  209. {
  210. title: "Subscribers",
  211. dataIndex: "subscriber_count",
  212. width: "15%",
  213. align: "center",
  214. render: (text, record) => {
  215. return (
  216. <div className="name" key={`name-${record.id}`}>
  217. <Link to={`/subscribers/lists/${record.id}`}>{text}</Link>
  218. </div>
  219. )
  220. }
  221. },
  222. {
  223. title: "Created",
  224. dataIndex: "created_at",
  225. render: (date, _) => {
  226. return Utils.DateString(date)
  227. }
  228. },
  229. {
  230. title: "Updated",
  231. dataIndex: "updated_at",
  232. render: (date, _) => {
  233. return Utils.DateString(date)
  234. }
  235. },
  236. {
  237. title: "",
  238. dataIndex: "actions",
  239. width: "10%",
  240. render: (text, record) => {
  241. return (
  242. <div className="actions">
  243. <Tooltip title="Send a campaign">
  244. <a role="button">
  245. <Icon type="rocket" />
  246. </a>
  247. </Tooltip>
  248. <Tooltip title="Edit list">
  249. <a
  250. role="button"
  251. onClick={() => this.handleShowEditForm(record)}
  252. >
  253. <Icon type="edit" />
  254. </a>
  255. </Tooltip>
  256. <Popconfirm
  257. title="Are you sure?"
  258. onConfirm={() => this.deleteRecord(record)}
  259. >
  260. <Tooltip title="Delete list" placement="bottom">
  261. <a role="button">
  262. <Icon type="delete" />
  263. </a>
  264. </Tooltip>
  265. </Popconfirm>
  266. </div>
  267. )
  268. }
  269. }
  270. ]
  271. }
  272. componentDidMount() {
  273. this.props.pageTitle("Lists")
  274. this.fetchRecords()
  275. }
  276. fetchRecords = () => {
  277. this.props.modelRequest(cs.ModelLists, cs.Routes.GetLists, cs.MethodGet)
  278. }
  279. deleteRecord = record => {
  280. this.props
  281. .modelRequest(cs.ModelLists, cs.Routes.DeleteList, cs.MethodDelete, {
  282. id: record.id
  283. })
  284. .then(() => {
  285. notification["success"]({
  286. placement: cs.MsgPosition,
  287. message: "List deleted",
  288. description: `"${record.name}" deleted`
  289. })
  290. // Reload the table.
  291. this.fetchRecords()
  292. })
  293. .catch(e => {
  294. notification["error"]({
  295. placement: cs.MsgPosition,
  296. message: "Error",
  297. description: e.message
  298. })
  299. })
  300. }
  301. handleHideForm = () => {
  302. this.setState({ formType: null })
  303. }
  304. handleShowCreateForm = () => {
  305. this.setState({ formType: cs.FormCreate, record: {} })
  306. }
  307. handleShowEditForm = record => {
  308. this.setState({ formType: cs.FormEdit, record: record })
  309. }
  310. render() {
  311. return (
  312. <section className="content">
  313. <Row>
  314. <Col span={22}>
  315. <h1>Lists ({this.props.data[cs.ModelLists].length}) </h1>
  316. </Col>
  317. <Col span={2}>
  318. <Button
  319. type="primary"
  320. icon="plus"
  321. onClick={this.handleShowCreateForm}
  322. >
  323. Create list
  324. </Button>
  325. </Col>
  326. </Row>
  327. <br />
  328. <Table
  329. className="lists"
  330. columns={this.columns}
  331. rowKey={record => record.uuid}
  332. dataSource={this.props.data[cs.ModelLists]}
  333. loading={this.props.reqStates[cs.ModelLists] !== cs.StateDone}
  334. pagination={false}
  335. />
  336. <CreateForm
  337. {...this.props}
  338. formType={this.state.formType}
  339. record={this.state.record}
  340. onClose={this.handleHideForm}
  341. fetchRecords={this.fetchRecords}
  342. />
  343. </section>
  344. )
  345. }
  346. }
  347. export default Lists