123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- import React from 'react';
- import { Form, Input, Button, Row, Col } from 'antd';
- import './index.less';
- import Editor from '@src/components/Editor';
- import Page from '@src/containers/Page';
- import Block from '@src/components/Block';
- import Select from '@src/components/Select';
- // import FileUpload from '@src/components/FileUpload';
- import { formatFormError, getMap } from '@src/services/Tools';
- import { asyncSMessage } from '@src/services/AsyncTools';
- import { Ready } from '../../../stores/ready';
- import { System } from '../../../stores/system';
- export default class extends Page {
- init() {
- Ready.allCategory().then(result => {
- this.categoryList = result.map(row => {
- row.value = row.id;
- return row;
- });
- this.categoryMap = getMap(result, 'id', 'title');
- const select = this.categoryList.filter(row => row.parentId === 0);
- select.push({ label: '其他', value: 0 });
- this.setState({
- parentCategoryId: {
- select,
- },
- });
- this.initData();
- });
- }
- onChangeSearch(parentValue, value) {
- const { setFieldsValue } = this.props.form;
- const info = {};
- let showParentCategory = false;
- let showCategory = false;
- if (parentValue) {
- info.disabled = false;
- info.select = this.categoryList.filter(row => row.parentId === parentValue);
- info.select.push({ label: '其他', value: 0 });
- if (value === 0) {
- showCategory = true;
- } else if (!value) {
- setFieldsValue({ categoryId: null });
- }
- } else if (parentValue === 0) {
- showParentCategory = true;
- showCategory = true;
- info.disabled = true;
- info.select = [];
- setFieldsValue({ categoryId: null });
- } else {
- info.disabled = true;
- info.select = [];
- }
- this.setState({
- showParentCategory,
- showCategory,
- categoryId: info,
- });
- }
- initData() {
- if (!this.categoryList) return;
- const { id } = this.params;
- const { form } = this.props;
- let handler;
- if (id) {
- handler = Ready.getArticle({ id });
- } else {
- handler = Promise.resolve({});
- }
- handler
- .then(result => {
- form.setFieldsValue(result);
- this.onChangeSearch(result.parentCategoryId, result.categoryId);
- });
- }
- submit() {
- const { form } = this.props;
- form.validateFields((err) => {
- if (!err) {
- const data = form.getFieldsValue();
- let handler;
- if (data.id) {
- handler = Ready.editArticle(data);
- } else {
- handler = Ready.addArticle(data);
- }
- handler.then(() => {
- asyncSMessage('保存成功');
- goBack();
- }).catch((e) => {
- if (e.result) form.setFields(formatFormError(data, e.result));
- });
- }
- });
- }
- renderBase() {
- const { getFieldDecorator, getFieldValue } = this.props.form;
- return <Block>
- <Form>
- {getFieldDecorator('id')(<input hidden />)}
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='一级标题'>
- {getFieldDecorator('parentCategoryId', {
- rules: [
- { required: !this.state.showParentCategory, message: '请选择标题' },
- ],
- })(
- <Select {...this.state.parentCategoryId} placeholder='请选择标题' onChange={(value) => {
- this.onChangeSearch(value, null);
- }} />,
- )}
- {this.state.showParentCategory && getFieldDecorator('parentCategory', {
- rules: [
- { required: true, message: '请输入标题' },
- ],
- })(
- <Input placeholder='请输入标题' />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='二级标题'>
- {getFieldDecorator('categoryId', {
- rules: [
- { required: !this.state.showCategory, message: '请选择标题' },
- ],
- })(
- <Select {...this.state.categoryId} placeholder='请选择标题' onChange={(value) => {
- this.onChangeSearch(getFieldValue('parentCategoryId'), value);
- }} />,
- )}
- {this.state.showCategory && getFieldDecorator('category', {
- rules: [
- { required: true, message: '请输入标题' },
- ],
- })(
- <Input placeholder='请输入标题' />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='文章标题'>
- {getFieldDecorator('title', {
- rules: [
- { required: true, message: '请输入标题' },
- ],
- })(
- <Input placeholder='请输入标题' />,
- )}
- </Form.Item>
- <Form.Item label='正文'>
- {getFieldDecorator('content', {
- })(
- <Editor placeholder='请输入内容' onUpload={(file) => System.uploadImage(file)} />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderView() {
- return <div flex>
- {this.renderBase()}
- <Row type="flex" justify="center">
- <Col>
- <Button type="primary" onClick={() => {
- this.submit();
- }}>保存</Button>
- </Col>
- </Row>
- </div>;
- }
- }
|