ProductAttrList.js 13.7 KB
Newer Older
1
import React, { PureComponent, Fragment, Component } from 'react';
2 3 4 5 6 7 8 9 10 11 12 13 14 15
import {
  Row,
  Col,
  Form,
  Card,
  Table,
  Button,
  Divider,
  Modal,
  Input,
  message,
  Switch,
  Select,
} from 'antd';
16 17 18 19 20 21 22
import moment from 'moment';
import { connect } from 'dva';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import PaginationHelper from '../../../helpers/PaginationHelper';
import styles from './ProductAttrList.less';

const FormItem = Form.Item;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
const Option = Select.Option;

const ValueCreateForm = Form.create()(props => {
  const {
    valueModalVisible,
    form,
    handleValueAdd,
    handleValueModalVisible,
    modalType,
    initValues,
    tree,
  } = props;

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      let pid = fieldsValue.pid;
      if (fieldsValue.pid) {
        pid = pid.split('-')[1];
        fieldsValue.pid = pid;
      }
      form.resetFields();
      handleValueAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const selectStyle = {
    width: 200,
  };

  function onTypeChange(event) {
    initValues.type = parseInt(event.target.value);
  }

  const title = modalType === 'add' ? '添加规格值' : '编辑规格值';
  const okText = modalType === 'add' ? '添加' : '编辑';
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={valueModalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleValueModalVisible()}
    >
      {modalType === 'add' ? (
        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="规格">
          {form.getFieldDecorator('attrId', {
            // initialValue: template.durationHour ? template.durationHour : '3',
            rules: [
              {
                required: true,
                message: '请选择规格',
              },
            ],
          })(
            <Select placeholder="请选择规格" style={{ width: 120 }}>
              {tree.map(item => (
                <Option value={item.id}>{item.name}</Option>
              ))}
              {/* <Option value="1">1</Option> */}
            </Select>
          )}
        </FormItem>
      ) : null}
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="规格值">
        {form.getFieldDecorator('name', {
          initialValue: initValues ? initValues.name : null,
          rules: [{ required: true, message: '请输入规格值!', min: 2 }],
        })(<Input placeholder="规格值" />)}
      </FormItem>
    </Modal>
  );
});

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
const CreateForm = Form.create()(props => {
  const { modalVisible, form, handleAdd, handleModalVisible, modalType, initValues } = props;

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      let pid = fieldsValue.pid;
      if (fieldsValue.pid) {
        pid = pid.split('-')[1];
        fieldsValue.pid = pid;
      }
      form.resetFields();
      handleAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const selectStyle = {
    width: 200,
  };

  function onTypeChange(event) {
    initValues.type = parseInt(event.target.value);
  }

  const title = modalType === 'add' ? '添加规格' : '编辑规格';
  const okText = modalType === 'add' ? '添加' : '编辑';
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={modalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleModalVisible()}
    >
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="规格名称">
        {form.getFieldDecorator('name', {
          initialValue: initValues ? initValues.name : null,
          rules: [{ required: true, message: '请输入规格名称!', min: 2 }],
        })(<Input placeholder="规格名称" />)}
      </FormItem>
    </Modal>
  );
});

@connect(({ productAttrList, loading }) => ({
  productAttrList,
  attrData: productAttrList.attrData,
154
  tree: productAttrList.tree,
155 156 157 158 159 160
  loading: loading.models.productAttrList,
}))
@Form.create()
export default class ProductAttrList extends PureComponent {
  state = {
    modalVisible: false,
161
    valueModalVisible: false,
162 163
    modalType: 'add', //add or update
    initValues: {},
164 165 166
    current: 1,
    pageSize: 10,
    name: null,
167 168 169
  };

  componentDidMount() {
170 171 172 173
    this.initFetch();
  }

  initFetch = () => {
174
    const { dispatch } = this.props;
175
    const { current, pageSize, name } = this.state;
176 177 178
    dispatch({
      type: 'productAttrList/page',
      payload: {
179 180 181
        pageNo: current,
        pageSize,
        name,
182 183
      },
    });
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    // const { dispatch } = this.props;
    dispatch({
      type: 'productAttrList/tree',
      payload: {
        ...PaginationHelper.defaultPayload,
      },
    });
  };

  expandedRowRender = record => {
    const columns = [
      {
        title: '规格值',
        dataIndex: 'name',
      },
      {
        title: '状态',
        // dataIndex: 'status',
        render: (text, record) => (
          <Switch
            checked={record.status === 1}
            onChange={checked => this.switchValueChange(checked, record)}
          />
        ),
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.handleValueModalVisible(true, 'update', record)}>编辑</a>
            <Divider type="vertical" />
            {/* <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
              删除
            </a> */}
          </Fragment>
        ),
      },
    ];

    return <Table columns={columns} dataSource={record.values} pagination={false} />;
  };

  handleAdd = ({ fields, modalType, initValues }) => {
    const { dispatch } = this.props;
    if (modalType === 'add') {
      dispatch({
        type: 'productAttrList/add',
        payload: {
          body: {
            ...fields,
          },
          onSuccess: () => {
            message.success('添加成功');
            this.handleModalVisible();
            this.initFetch();
          },
          onFail: response => {
            message.warn('添加失败' + response.message);
          },
        },
      });
    } else {
      dispatch({
        type: 'productAttrList/update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          onSuccess: () => {
            message.success('更新成功');
            this.handleModalVisible();
            this.initFetch();
          },
          onFail: response => {
            message.warn('更新失败' + response.message);
          },
        },
      });
    }
  };

  handleValueAdd = ({ fields, modalType, initValues }) => {
    const { dispatch } = this.props;
    if (modalType === 'add') {
      dispatch({
        type: 'productAttrList/value_add',
        payload: {
          body: {
            ...fields,
          },
          onSuccess: () => {
            message.success('添加成功');
            this.handleValueModalVisible();
            this.initFetch();
          },
          onFail: response => {
            message.warn('添加失败' + response.message);
          },
        },
      });
    } else {
      dispatch({
        type: 'productAttrList/value_update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          onSuccess: () => {
            message.success('更新成功');
            this.handleValueModalVisible();
            this.initFetch();
          },
          onFail: response => {
            message.warn('更新失败' + response.message);
          },
        },
      });
    }
  };
311 312 313 314 315 316 317 318 319

  handleModalVisible = (flag, modalType, initValues) => {
    this.setState({
      modalVisible: !!flag,
      initValues: initValues || {},
      modalType: modalType || 'add',
    });
  };

320 321 322 323 324 325 326 327
  handleValueModalVisible = (flag, modalType, initValues) => {
    this.setState({
      valueModalVisible: !!flag,
      initValues: initValues || {},
      modalType: modalType || 'add',
    });
  };

328 329 330 331 332 333 334 335 336 337 338 339 340
  handleTableChange = pagination => {
    const { pageSize, current, index } = pagination;
    this.setState(
      {
        current,
        pageSize,
      },
      function() {
        this.initFetch();
      }
    );
  };

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  switchValueChange = (checked, record) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'productAttrList/value_update_status',
      payload: {
        body: {
          id: record.id,
          status: checked ? 1 : 2,
        },
        onSuccess: () => {
          message.success('修改状态成功');
          this.initFetch();
        },
      },
    });
  };

  switchChange = (checked, record) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'productAttrList/update_status',
      payload: {
        body: {
          id: record.id,
          status: checked ? 1 : 2,
        },
        onSuccess: () => {
          message.success('修改状态成功');
          this.initFetch();
        },
      },
    });
  };

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState(
      {
        name: null,
      },
      function() {
        this.initFetch();
      }
    );
  };

  handleCondition = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      const values = {
        ...fieldsValue,
      };

      if (values.name) {
        this.setState(
          {
            searched: true,
            name: values.name,
          },
          function() {
            this.initFetch();
          }
        );
      } else {
        this.initFetch();
      }

      // dispatch({
      //   type: 'fenfa/getCategoryList',
      //   payload: {
      //     key: values.name
      //   },
      // });
    });
  };

  renderSimpleForm() {
    const { form } = this.props;
    const { getFieldDecorator } = form;
    return (
      <Form onSubmit={this.handleCondition} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="规格名称">
              {getFieldDecorator('name')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>

          <Col md={8} sm={24}>
            <span className={styles.submitButtons}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

449
  render() {
450
    const { attrData, productAttrList, loading, tree } = this.props;
451 452 453 454 455 456 457
    const columns = [
      {
        title: '规格名称',
        dataIndex: 'name',
      },
      {
        title: '状态',
458 459 460 461 462 463 464
        // dataIndex: 'status',
        render: (text, record) => (
          <Switch
            checked={record.status === 1}
            onChange={checked => this.switchChange(checked, record)}
          />
        ),
465 466 467 468 469 470 471 472 473 474 475 476 477
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.handleModalVisible(true, 'update', record)}>编辑</a>
            <Divider type="vertical" />
478 479
            <a onClick={() => this.handleValueModalVisible(true, 'add', {})}>新建规格值</a>
            {/* <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
480
              删除
481
            </a> */}
482 483 484 485 486
          </Fragment>
        ),
      },
    ];

487
    const { modalVisible, modalType, initValues, valueModalVisible } = this.state;
488 489 490 491 492 493 494 495

    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };

496 497 498 499 500 501 502 503
    const valueFormParentMethods = {
      handleValueAdd: this.handleValueAdd,
      handleValueModalVisible: this.handleValueModalVisible,
      modalType,
      initValues,
      tree: tree,
    };

504 505 506 507 508 509
    const pagination = {
      total: attrData.count,
      index: this.state.current,
      pageSize: this.state.pageSize,
    };

510 511 512 513 514
    return (
      <PageHeaderWrapper>
        <Card>
          <div className={styles.tableList}>
            <div className={styles.tableListOperator}>
515 516 517 518 519 520 521 522 523 524 525 526 527 528
              <Row>
                <Col span={8}>
                  <Button
                    icon="plus"
                    type="primary"
                    onClick={() => this.handleModalVisible(true, 'add', {})}
                  >
                    新建规格
                  </Button>
                </Col>
                <Col span={16}>
                  <div>{this.renderSimpleForm()}</div>
                </Col>
              </Row>
529 530 531 532 533 534 535 536 537
            </div>
          </div>
          <Table
            defaultExpandAllRows={true}
            columns={columns}
            dataSource={attrData.attrs ? attrData.attrs : []}
            rowKey="id"
            loading={loading}
            pagination={pagination}
538
            expandedRowRender={this.expandedRowRender}
539 540
            defaultExpandAllRows={false}
            onChange={pagination => this.handleTableChange(pagination)}
541 542 543
          />
        </Card>
        {modalVisible ? <CreateForm {...parentMethods} modalVisible={modalVisible} /> : null}
544 545 546
        {valueModalVisible ? (
          <ValueCreateForm {...valueFormParentMethods} valueModalVisible={valueModalVisible} />
        ) : null}
547 548 549 550
      </PageHeaderWrapper>
    );
  }
}