page.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React from 'react';
  2. import { Form, Input, Button, Row, Col } from 'antd';
  3. import './index.less';
  4. import Editor from '@src/components/Editor';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import Select from '@src/components/Select';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError, getMap } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { Ready } from '../../../stores/ready';
  12. export default class extends Page {
  13. init() {
  14. Ready.allCategory().then(result => {
  15. this.categoryList = result.map(row => {
  16. row.value = row.id;
  17. return row;
  18. });
  19. this.categoryMap = getMap(result, 'id', 'title');
  20. const select = this.categoryList.filter(row => row.parentId === 0);
  21. select.push({ label: '其他', value: 0 });
  22. this.setState({
  23. parentCategoryId: {
  24. select,
  25. },
  26. });
  27. this.initData();
  28. });
  29. }
  30. onChangeSearch(parentValue, value) {
  31. const { setFieldsValue } = this.props.form;
  32. const info = {};
  33. let showParentCategory = false;
  34. let showCategory = false;
  35. if (parentValue) {
  36. info.disabled = false;
  37. info.select = this.categoryList.filter(row => row.parentId === parentValue);
  38. info.select.push({ label: '其他', value: 0 });
  39. if (value === 0) {
  40. showCategory = true;
  41. } else if (!value) {
  42. setFieldsValue({ categoryId: null });
  43. }
  44. } else if (parentValue === 0) {
  45. showParentCategory = true;
  46. showCategory = true;
  47. info.disabled = true;
  48. info.select = [];
  49. setFieldsValue({ categoryId: null });
  50. } else {
  51. info.disabled = true;
  52. info.select = [];
  53. }
  54. this.setState({
  55. showParentCategory,
  56. showCategory,
  57. categoryId: info,
  58. });
  59. }
  60. initData() {
  61. if (!this.categoryList) return;
  62. const { id } = this.params;
  63. const { form } = this.props;
  64. let handler;
  65. if (id) {
  66. handler = Ready.getArticle({ id });
  67. } else {
  68. handler = Promise.resolve({});
  69. }
  70. handler
  71. .then(result => {
  72. form.setFieldsValue(result);
  73. this.onChangeSearch(result.parentCategoryId, result.categoryId);
  74. });
  75. }
  76. submit() {
  77. const { form } = this.props;
  78. form.validateFields((err) => {
  79. if (!err) {
  80. const data = form.getFieldsValue();
  81. let handler;
  82. if (data.id) {
  83. handler = Ready.editArticle(data);
  84. } else {
  85. handler = Ready.addArticle(data);
  86. }
  87. handler.then(() => {
  88. asyncSMessage('保存成功');
  89. goBack();
  90. }).catch((e) => {
  91. if (e.result) form.setFields(formatFormError(data, e.result));
  92. });
  93. }
  94. });
  95. }
  96. renderBase() {
  97. const { getFieldDecorator, getFieldValue } = this.props.form;
  98. return <Block>
  99. <Form>
  100. {getFieldDecorator('id')(<input hidden />)}
  101. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='一级标题'>
  102. {getFieldDecorator('parentCategoryId', {
  103. rules: [
  104. { required: !this.state.showParentCategory, message: '请选择标题' },
  105. ],
  106. })(
  107. <Select {...this.state.parentCategoryId} placeholder='请选择标题' onChange={(value) => {
  108. this.onChangeSearch(value, null);
  109. }} />,
  110. )}
  111. {this.state.showParentCategory && getFieldDecorator('parentCategory', {
  112. rules: [
  113. { required: true, message: '请输入标题' },
  114. ],
  115. })(
  116. <Input placeholder='请输入标题' />,
  117. )}
  118. </Form.Item>
  119. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='二级标题'>
  120. {getFieldDecorator('categoryId', {
  121. rules: [
  122. { required: !this.state.showCategory, message: '请选择标题' },
  123. ],
  124. })(
  125. <Select {...this.state.categoryId} placeholder='请选择标题' onChange={(value) => {
  126. this.onChangeSearch(getFieldValue('parentCategoryId'), value);
  127. }} />,
  128. )}
  129. {this.state.showCategory && getFieldDecorator('category', {
  130. rules: [
  131. { required: true, message: '请输入标题' },
  132. ],
  133. })(
  134. <Input placeholder='请输入标题' />,
  135. )}
  136. </Form.Item>
  137. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='文章标题'>
  138. {getFieldDecorator('title', {
  139. rules: [
  140. { required: true, message: '请输入标题' },
  141. ],
  142. })(
  143. <Input placeholder='请输入标题' />,
  144. )}
  145. </Form.Item>
  146. <Form.Item label='正文'>
  147. {getFieldDecorator('content', {
  148. })(
  149. <Editor placeholder='输入内容' />,
  150. )}
  151. </Form.Item>
  152. </Form>
  153. </Block>;
  154. }
  155. renderView() {
  156. return <div flex>
  157. {this.renderBase()}
  158. <Row type="flex" justify="center">
  159. <Col>
  160. <Button type="primary" onClick={() => {
  161. this.submit();
  162. }}>保存</Button>
  163. </Col>
  164. </Row>
  165. </div>;
  166. }
  167. }