index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import React, { PureComponent } from 'react';
  2. import { routerRedux } from 'dva/router';
  3. import PropTypes from 'prop-types';
  4. import queryString from 'query-string';
  5. import { connect } from 'dva';
  6. import { Spin, Popover, Badge, Table, Radio, Card, Form, Input, Icon, Button, Select } from 'antd';
  7. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  8. import ResourceSelectSortModal from './modal';
  9. import { Codes, resourceType } from '../../../utils/config';
  10. const FormItem = Form.Item;
  11. const Option = Select.Option;
  12. const { TextArea } = Input;
  13. @Form.create()
  14. @connect(state => ({
  15. wareDetail: state.wareDetail,
  16. resource: state.resource,
  17. }))
  18. export default class WareDetail extends PureComponent {
  19. static propTypes = {
  20. wareDetail: PropTypes.object,
  21. };
  22. // 展示模态框 - 加载第一页数据
  23. handleModalShow = () => {
  24. const { dispatch } = this.props;
  25. dispatch({ type: 'wareDetail/showModal' });
  26. dispatch({
  27. type: 'resource/query',
  28. payload: {
  29. pageNo: 1,
  30. pageSize: 10,
  31. }
  32. });
  33. }
  34. // 取消/关闭 - 隐藏模态框
  35. handleModalCancel = () => {
  36. const { dispatch } = this.props;
  37. dispatch({ type: 'wareDetail/hideModal' });
  38. }
  39. // 提交 - 保存选择和排序完的数据到model中
  40. handleModalOk = (data) => {
  41. const { dispatch } = this.props;
  42. dispatch({
  43. type: 'wareDetail/saveSortResult',
  44. payload: { resourceList: data }
  45. });
  46. }
  47. // 搜索
  48. handleModalSearch = (data) => {
  49. const { dispatch } = this.props;
  50. const newData = { ...data };
  51. if (newData.keyword) {
  52. newData[newData.field] = newData.keyword;
  53. delete newData.field;
  54. delete newData.keyword;
  55. } else {
  56. delete newData.field;
  57. delete newData.keyword;
  58. }
  59. dispatch({
  60. type: 'resource/query',
  61. payload: { ...newData, pageNo: 1, pageSize: 10 },
  62. });
  63. }
  64. // 翻页 - 资源列表
  65. handleModalTableOnChange = (pagination, filterArgs, filters) => {
  66. const { dispatch } = this.props;
  67. const newFilters = { ...filters };
  68. if (newFilters.keyword) {
  69. newFilters[newFilters.field] = newFilters.keyword;
  70. delete newFilters.field;
  71. delete newFilters.keyword;
  72. } else {
  73. delete newFilters.field;
  74. delete newFilters.keyword;
  75. }
  76. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  77. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  78. const newObj = { ...obj };
  79. newObj[key] = getValue(filterArgs[key]);
  80. return newObj;
  81. }, {});
  82. const data = { ...newFilters, ...tableFilters, pageNo: pagination.current, pageSize: pagination.pageSize };
  83. Object.keys(data).map(key => data[key] ? null : delete data[key]);
  84. dispatch({ type: 'resource/query', payload: data });
  85. }
  86. handlePageSubmit = (e) => {
  87. e.preventDefault()
  88. const {
  89. dispatch,
  90. form: {
  91. validateFields,
  92. getFieldsValue,
  93. resetFields
  94. },
  95. wareDetail: {
  96. operType,
  97. currentItem,
  98. filters,
  99. }
  100. } = this.props;
  101. validateFields((errors) => {
  102. if (errors) { return; }
  103. const data = {
  104. ...currentItem,
  105. ...getFieldsValue(),
  106. };
  107. dispatch({
  108. type: `wareDetail/${operType}`,
  109. payload: data,
  110. callback: () => {
  111. dispatch(
  112. routerRedux.push({
  113. pathname: '/product/ware',
  114. search: queryString.stringify(filters),
  115. })
  116. );
  117. }
  118. })
  119. resetFields();
  120. });
  121. }
  122. handlePageCancel = () => {
  123. const { dispatch, wareDetail: { filters } } = this.props;
  124. dispatch({ type: 'wareDetail/clearPage' });
  125. dispatch(
  126. routerRedux.push({
  127. pathname: '/product/ware',
  128. search: queryString.stringify(filters),
  129. })
  130. );
  131. }
  132. render() {
  133. const { dispatch, form: { getFieldDecorator }, wareDetail, resource } = this.props;
  134. const { itemLoading, currentItem, filters, modalVisible } = wareDetail;
  135. const { resourceList, name, code, digest } = currentItem;
  136. const { list, listLoading, pagination } = resource;
  137. // 待选表格去掉分页的跳转及变换页码
  138. if (pagination) {
  139. delete pagination.showQuickJumper;
  140. delete pagination.showSizeChanger;
  141. }
  142. const subTableColumns = [{
  143. title: '缩略图',
  144. dataIndex: 'url',
  145. key: 'url',
  146. render: (text, record) => (
  147. record.type !== Codes.CODE_IMAGE ? <Icon type="video-camera" style={{ fontSize: 40}} /> :
  148. <Popover
  149. content={<img alt="" src={record.url} width={350} />}
  150. title={record.name}
  151. >
  152. <img alt="" src={record.url} width={70} />
  153. </Popover>
  154. ),
  155. },{
  156. title: '资源编号',
  157. dataIndex: 'code',
  158. key: 'code',
  159. },{
  160. title: '资源名称',
  161. dataIndex: 'name',
  162. key: 'name',
  163. },{
  164. title: '资源类型',
  165. dataIndex: 'type',
  166. key: 'type',
  167. render: (text, record) => (resourceType[record.type]),
  168. }];
  169. const formItemLayout = {
  170. labelCol: {
  171. span: 7,
  172. },
  173. wrapperCol: {
  174. span: 12,
  175. },
  176. };
  177. const submitFormLayout = {
  178. wrapperCol: {
  179. xs: { span: 24, offset: 0 },
  180. sm: { span: 10, offset: 7 },
  181. },
  182. };
  183. return (
  184. <PageHeaderLayout>
  185. <Spin spinning={itemLoading}>
  186. <Card>
  187. <Form layout="horizontal" onSubmit={this.handlePageSubmit}>
  188. <FormItem label="课件编号:" hasFeedback {...formItemLayout}>
  189. {getFieldDecorator('code', {
  190. rules: [{ required: true, type: 'string', message: "编号为必填项!" }],
  191. initialValue: code,
  192. })(<Input />)}
  193. </FormItem>
  194. <FormItem label="课件名称:" hasFeedback {...formItemLayout}>
  195. {getFieldDecorator('name', {
  196. rules: [{ required: true, type: 'string', message: "名称为必填项!" }],
  197. initialValue: name,
  198. })(<Input />)}
  199. </FormItem>
  200. <FormItem label="课件简述:" hasFeedback {...formItemLayout}>
  201. {getFieldDecorator('digest', {
  202. initialValue: digest,
  203. })(<TextArea />)}
  204. </FormItem>
  205. {/*内容提供商暂不用选择
  206. <FormItem label="提供商" hasFeedback {...formItemLayout}>
  207. {getFieldDecorator('groupId', {
  208. rules: [{ required: true, type: 'string', message: "标签组为必选项!" }],
  209. initialValue: groupId,
  210. })(
  211. <Select
  212. showSearch
  213. allowClear
  214. placeholder="请输入标签组编号或者名称进行筛选"
  215. optionFilterProp="children"
  216. filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
  217. >
  218. {list.map(item => <Option value={item.id} key={item.id}>{`${item.code}/${item.name}`}</Option>)}
  219. </Select>
  220. )}
  221. </FormItem>
  222. */}
  223. <FormItem label="选择资源" {...formItemLayout}>
  224. <Button onClick={this.handleModalShow} type="primary" size="small" icon="edit">编辑</Button>
  225. </FormItem>
  226. <FormItem wrapperCol={{ offset: 7, span: 12 }}>
  227. <Table
  228. locale={{
  229. emptyText: <span style={{ color: "#C6D0D6" }}>&nbsp;&nbsp;<Icon type="frown-o"/>
  230. 该课件下不包含任何内容,请选择图片或视频!</span>
  231. }}
  232. dataSource={resourceList}
  233. columns={subTableColumns}
  234. rowKey={record => record.id}
  235. bordered
  236. pagination={false}
  237. />
  238. </FormItem>
  239. <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
  240. <Button onClick={this.handlePageCancel}>取消</Button>
  241. <Button type="primary" style={{ marginLeft: 35 }} htmlType="submit">提交</Button>
  242. </FormItem>
  243. </Form>
  244. <ResourceSelectSortModal
  245. rowKeyName="id"
  246. modalVisible={modalVisible}
  247. style={{ top: 20 }}
  248. width={600}
  249. onCancel={this.handleModalCancel}
  250. onOk={this.handleModalOk}
  251. onSearch={this.handleModalSearch}
  252. selTableData={resourceList || []}
  253. fsTableDataSource={list}
  254. fsTableLoading={listLoading}
  255. fsTablePagination={pagination}
  256. fsTableOnChange={this.handleModalTableOnChange}
  257. />
  258. </Card>
  259. </Spin>
  260. </PageHeaderLayout>
  261. );
  262. }
  263. }