page.js 5.2 KB

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