/* eslint-disable no-trailing-spaces */ import React, { Component } from 'react'; import pathToRegexp from 'path-to-regexp'; import { routerRedux } from 'dva/router'; import { connect } from 'dva'; import { Card, Table, Modal, Form, Tooltip, Popconfirm, Switch, Button, Input } from 'antd'; import Selector from '../../../components/AXTableSelector/Selector'; import AXDragSortTable from '../../../components/AXDragSortTable'; import FooterToolbar from '../../../components/FooterToolbar'; import { renderStatus, statusToBool, boolToStatus, renderProductType } from '../../../utils/utils'; import { Hotax } from '../../../utils/config'; import styles from './PersonalizeEdit.less'; const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 2 }, md: { span: 2 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 14 }, md: { span: 22 }, }, }; @Form.create() @connect(({ loading, terminal, shelves, tagType, tag }) => ({ tag, shelves, terminal, tagType, loading, sLoading: loading.models.shelves, tLoading: loading.models.tagType, mtLoading: loading.models.tag, })) export default class PersonalizeEditPage extends Component { constructor(props) { super(props); const { location } = props; const { state } = location; const match = pathToRegexp('/frontend/personalize/edit/:id').exec(location.pathname); this.state = { uid: match[1], mid: state.merchantId, tagModalDestroy: true, tagModalName: '新建用户标签', tagTypeSelecting: false, // 标签类型处于选择状态 recModalDestroy: true, productSelecting: false, // 产品处于选择状态 merchantTagModalDestroy: true, targetRow: {}, }; } componentDidMount() { // 加载用户标签数据 this.props.dispatch({ type: 'terminal/fetchTerminalTagList', payload: { uid: this.state.uid }, }); // 加载用户推荐课程 this.props.dispatch({ type: 'terminal/fetchTerminalRecommendCourse', payload: { uid: this.state.uid }, }); // 加载全部 this.props.dispatch({ type: 'tagType/fetchTagTypeList', payload: { pageSize: 1000 }, }); } /** * 用户标签创建/编辑模态框展现及数据加载控制 * @param userTagId */ handleUserTagModalShow = (userTagId) => { // 展现模态框前清空下currentUserTagItem内容 this.props.dispatch({ type: 'terminal/resetUserTagItem', }); // 展现模态框 this.setState({ tagModalDestroy: false }); // 如果编辑标签则发获取详情请求 if (userTagId) { this.props.dispatch({ type: 'terminal/fetchTerminalTagItem', payload: { userTagId }, }); this.setState({ tagModalName: '编辑用户标签' }); } }; /** * 用户推荐课程模态框的展现 */ handleUserRecModalShow = () => { this.setState({ recModalDestroy: false }); }; handleMerchantTagModalShow = (record) => { this.setState({ targetRow: record, merchantTagModalDestroy: false, }); this.props.dispatch({ type: 'tag/fetchTagList', payload: { merchantId: this.state.mid }, }); }; /** * 模态框的取消操作 * @param modalName */ handleModalHide = (modalName) => { this.setState({ [modalName]: true }); }; /** * 用户标签内卡片切换操作 * @param name */ handleCardSwitch = (name) => { if (name === 'tagType') { this.props.dispatch({ type: 'tagType/fetchTagTypeList', payload: {}, }); this.setState({ tagTypeSelecting: true }); } if (name === 'product') { this.props.dispatch({ type: 'shelves/fetchItemList', payload: { merchantId: this.state.mid }, }); this.setState({ productSelecting: true }); } if (name === 'course') { this.props.dispatch({ type: 'shelves/fetchCourseItemList', payload: { merchantId: this.state.mid }, }); this.setState({ productSelecting: true }); } }; /** * 标签类型/关联产品取消选择操作 * @param name */ handleSelectingCardCancel = (name) => { if (name === 'tagType') { this.setState({ tagTypeSelecting: false }); } if (name === 'product' || name === 'course') { this.setState({ productSelecting: false }); } }; /** * 标签类型/关联产品筛选操作 * @param name * @param data */ handleSelectingCardChange = (name, data) => { if (name === 'tagType') { this.props.dispatch({ type: 'tagType/fetchTagTypeList', payload: data, }); } if (name === 'product') { this.props.dispatch({ type: 'shelves/fetchItemList', payload: { merchantId: this.state.mid, ...data }, }); } if (name === 'course') { this.props.dispatch({ type: 'shelves/fetchCourseItemList', payload: { merchantId: this.state.mid, ...data }, }); } if (name === 'tag') { this.props.dispatch({ type: 'tag/fetchTagList', payload: { merchantId: this.state.uid, ...data }, }); } }; /** * 标签类型/关联产品完成操作 * @param cardName * @param data */ handleSelectingCardFinish = (cardName, data) => { if (cardName === 'tagType') { const tagType = data[0] || {}; const { code, name } = tagType; this.props.dispatch({ type: 'terminal/fixCurrentUserTagItem', payload: { typeCode: code, typeName: name, }, }); this.setState({ tagTypeSelecting: false }); } if (cardName === 'product') { this.props.dispatch({ type: 'terminal/fixCurrentUserTagItem', payload: { productList: data }, }); this.setState({ productSelecting: false }); } if (cardName === 'course') { this.props.dispatch({ type: 'terminal/fixUserRecCourse', payload: (data || []).slice(0, 5), }); this.setState({ productSelecting: false }); } }; /** * 用户标签内关联产品/推荐课程排序操作 * @param rows * @param tabName */ handleDragSortTableChange = (rows, tabName) => { if (tabName === 'tag') { this.props.dispatch({ type: 'terminal/fixCurrentUserTagItem', payload: { productList: rows }, }); } if (tabName === 'rec') { this.props.dispatch({ type: 'terminal/fixUserRecCourse', payload: rows, }); } }; handlePageBack = () => { this.props.dispatch(routerRedux.push({ pathname: '/frontend/personalize/list', state: this.props.location.state, })); }; handleUserTagSubmit = () => { this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { const { sort, name, status } = values; const { uid } = this.state; const { terminal } = this.props; const { currentUserTagItem } = terminal; const { id, typeCode, productList } = currentUserTagItem; const pidList = (productList || []).map(product => product.pid); const newStatus = boolToStatus(status); if (!id) { this.props.dispatch({ type: 'terminal/createTerminalTagItem', payload: { sort, uid, name, typeCode, status: newStatus, productList: pidList }, }); } else { this.props.dispatch({ type: 'terminal/updateTerminalTagItem', payload: { id, sort, uid, name, typeCode, status: newStatus, productList: pidList }, }); } this.setState({ tagModalDestroy: true }); } }); }; handleUserTagCopyOperation = (data) => { const { targetRow } = this.state; const merchantTag = (data || [])[0] || {}; this.props.dispatch({ type: 'terminal/copyMerchantTagToUser', payload: { uid: this.state.uid, userTagId: targetRow.id, tagId: merchantTag.id }, }); this.setState({ merchantTagModalDestroy: true }); }; handleUserTagDelete = (userTagId) => { this.props.dispatch({ type: 'terminal/deleteTerminalTagItem', payload: { uid: this.state.uid, userTagId }, }); }; handleUserRecCourseSubmit = () => { const { uid } = this.state; const { terminal } = this.props; const { userRecCourse } = terminal; const idList = (userRecCourse || []).map(product => product.pid); this.props.dispatch({ type: 'terminal/updateTerminalRecommendCourse', payload: { uid, idList }, }); this.setState({ recModalDestroy: true }); }; render() { const { tagModalDestroy, tagModalName, tagTypeSelecting, productSelecting, recModalDestroy, merchantTagModalDestroy, } = this.state; const { sLoading, tLoading, mtLoading, loading, terminal, shelves, tagType, tag, form, } = this.props; const { getFieldDecorator } = form; const { userTagList, userRecCourse, currentUserTagItem } = terminal; const { sort, name, status, productList, typeCode, typeName } = currentUserTagItem; // 用户标签列表表头 const tagColumns = [{ title: '标签位置', dataIndex: 'sort', width: '10%', }, { title: '标签名称', dataIndex: 'name', width: '25%', }, { title: '标签类型', dataIndex: 'typeCode', width: '25%', filters: (tagType.list || []).map(item => ({ text: item.name, value: item.code })), onFilter: (value, record) => record.typeCode.indexOf(value) === 0, }, { title: '标签状态', dataIndex: 'status', width: '15%', render: text => renderStatus(text), filters: [{ text: '正常', value: Hotax.STATUS_NORMAL, }, { text: '删除', value: Hotax.STATUS_DELETE, }], onFilter: (value, record) => record.status === value, align: 'center', }, { title: '操作', width: '25%', align: 'right', render: (_, record) => (