mock平台

GroupSetting.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import React, { PureComponent as Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import { Input, Button, message, Icon, Card, Alert, Modal, Switch, Row, Col, Tooltip } from 'antd';
  5. import { fetchNewsData } from '../../../reducer/modules/news.js';
  6. import {
  7. changeGroupMsg,
  8. fetchGroupList,
  9. setCurrGroup,
  10. fetchGroupMsg,
  11. updateGroupList,
  12. deleteGroup
  13. } from '../../../reducer/modules/group.js';
  14. const { TextArea } = Input;
  15. import { trim } from '../../../common.js';
  16. import _ from 'underscore';
  17. import './GroupSetting.scss';
  18. const confirm = Modal.confirm;
  19. @connect(
  20. state => {
  21. return {
  22. groupList: state.group.groupList,
  23. currGroup: state.group.currGroup,
  24. curUserRole: state.user.role
  25. };
  26. },
  27. {
  28. changeGroupMsg,
  29. fetchGroupList,
  30. setCurrGroup,
  31. fetchGroupMsg,
  32. fetchNewsData,
  33. updateGroupList,
  34. deleteGroup
  35. }
  36. )
  37. class GroupSetting extends Component {
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. currGroupDesc: '',
  42. currGroupName: '',
  43. showDangerOptions: false,
  44. custom_field1_name: '',
  45. custom_field1_enable: false,
  46. custom_field1_rule: false
  47. };
  48. }
  49. static propTypes = {
  50. currGroup: PropTypes.object,
  51. curUserRole: PropTypes.string,
  52. changeGroupMsg: PropTypes.func,
  53. fetchGroupList: PropTypes.func,
  54. setCurrGroup: PropTypes.func,
  55. fetchGroupMsg: PropTypes.func,
  56. fetchNewsData: PropTypes.func,
  57. updateGroupList: PropTypes.func,
  58. deleteGroup: PropTypes.func,
  59. groupList: PropTypes.array
  60. };
  61. initState(props) {
  62. this.setState({
  63. currGroupName: props.currGroup.group_name,
  64. currGroupDesc: props.currGroup.group_desc,
  65. custom_field1_name: props.currGroup.custom_field1.name,
  66. custom_field1_enable: props.currGroup.custom_field1.enable
  67. });
  68. }
  69. // 修改分组名称
  70. changeName = e => {
  71. this.setState({
  72. currGroupName: e.target.value
  73. });
  74. };
  75. // 修改分组描述
  76. changeDesc = e => {
  77. this.setState({
  78. currGroupDesc: e.target.value
  79. });
  80. };
  81. // 修改自定义字段名称
  82. changeCustomName = e => {
  83. let custom_field1_rule = this.state.custom_field1_enable ? !e.target.value : false;
  84. this.setState({
  85. custom_field1_name: e.target.value,
  86. custom_field1_rule
  87. });
  88. };
  89. // 修改开启状态
  90. changeCustomEnable = e => {
  91. let custom_field1_rule = e ? !this.state.custom_field1_name : false;
  92. this.setState({
  93. custom_field1_enable: e,
  94. custom_field1_rule
  95. });
  96. };
  97. componentWillMount() {
  98. // console.log('custom_field1',this.props.currGroup.custom_field1)
  99. this.initState(this.props);
  100. }
  101. // 点击“查看危险操作”按钮
  102. toggleDangerOptions = () => {
  103. // console.log(this.state.showDangerOptions);
  104. this.setState({
  105. showDangerOptions: !this.state.showDangerOptions
  106. });
  107. };
  108. // 编辑分组信息
  109. editGroup = async () => {
  110. const id = this.props.currGroup._id;
  111. if (this.state.custom_field1_rule) {
  112. return;
  113. }
  114. const res = await this.props.changeGroupMsg({
  115. group_name: this.state.currGroupName,
  116. group_desc: this.state.currGroupDesc,
  117. custom_field1: {
  118. name: this.state.custom_field1_name,
  119. enable: this.state.custom_field1_enable
  120. },
  121. id: this.props.currGroup._id
  122. });
  123. if (!res.payload.data.errcode) {
  124. message.success('修改成功!');
  125. await this.props.fetchGroupList(this.props.groupList);
  126. this.props.updateGroupList(this.props.groupList);
  127. const currGroup = _.find(this.props.groupList, group => {
  128. return +group._id === +id;
  129. });
  130. this.props.setCurrGroup(currGroup);
  131. this.props.fetchGroupMsg(this.props.currGroup._id);
  132. this.props.fetchNewsData(this.props.currGroup._id, 'group', 1, 10);
  133. }
  134. };
  135. // 删除分组
  136. deleteGroup = async () => {
  137. const that = this;
  138. const { currGroup } = that.props;
  139. const res = await this.props.deleteGroup({ id: currGroup._id });
  140. if (!res.payload.data.errcode) {
  141. message.success('删除成功');
  142. await that.props.fetchGroupList();
  143. const currGroup = that.props.groupList[0] || { group_name: '', group_desc: '' };
  144. that.setState({ groupList: that.props.groupList });
  145. that.props.setCurrGroup(currGroup);
  146. }
  147. };
  148. // 删除分组的二次确认
  149. showConfirm = () => {
  150. const that = this;
  151. confirm({
  152. title: '确认删除 ' + that.props.currGroup.group_name + ' 分组吗?',
  153. content: (
  154. <div style={{ marginTop: '10px', fontSize: '13px', lineHeight: '25px' }}>
  155. <Alert
  156. message="警告:此操作非常危险,会删除该分组下面所有项目和接口,并且无法恢复!"
  157. type="warning"
  158. />
  159. <div style={{ marginTop: '16px' }}>
  160. <p>
  161. <b>请输入分组名称确认此操作:</b>
  162. </p>
  163. <Input id="group_name" />
  164. </div>
  165. </div>
  166. ),
  167. onOk() {
  168. const groupName = trim(document.getElementById('group_name').value);
  169. if (that.props.currGroup.group_name !== groupName) {
  170. message.error('分组名称有误');
  171. return new Promise((resolve, reject) => {
  172. reject('error');
  173. });
  174. } else {
  175. that.deleteGroup();
  176. }
  177. },
  178. iconType: 'delete',
  179. onCancel() {}
  180. });
  181. };
  182. componentWillReceiveProps(nextProps) {
  183. // 切换分组时,更新分组信息并关闭删除分组操作
  184. if (this.props.currGroup._id !== nextProps.currGroup._id) {
  185. this.initState(nextProps);
  186. this.setState({
  187. showDangerOptions: false
  188. });
  189. }
  190. }
  191. render() {
  192. return (
  193. <div className="m-panel card-panel card-panel-s panel-group">
  194. <Row type="flex" justify="space-around" className="row" align="middle">
  195. <Col span={4} className="label">
  196. 分组名:
  197. </Col>
  198. <Col span={20}>
  199. <Input
  200. size="large"
  201. placeholder="请输入分组名称"
  202. value={this.state.currGroupName}
  203. onChange={this.changeName}
  204. />
  205. </Col>
  206. </Row>
  207. <Row type="flex" justify="space-around" className="row" align="middle">
  208. <Col span={4} className="label">
  209. 简介:
  210. </Col>
  211. <Col span={20}>
  212. <TextArea
  213. size="large"
  214. rows={3}
  215. placeholder="请输入分组描述"
  216. value={this.state.currGroupDesc}
  217. onChange={this.changeDesc}
  218. />
  219. </Col>
  220. </Row>
  221. <Row type="flex" justify="space-around" className="row" align="middle">
  222. <Col span={4} className="label">
  223. 接口自定义字段&nbsp;
  224. <Tooltip title={'可以在接口中添加 额外字段 数据'}>
  225. <Icon type="question-circle-o" style={{ width: '10px' }} />
  226. </Tooltip> :
  227. </Col>
  228. <Col span={12} style={{ position: 'relative' }}>
  229. <Input
  230. placeholder="请输入自定义字段名称"
  231. style={{ borderColor: this.state.custom_field1_rule ? '#f5222d' : '' }}
  232. value={this.state.custom_field1_name}
  233. onChange={this.changeCustomName}
  234. />
  235. <div
  236. className="custom-field-rule"
  237. style={{ display: this.state.custom_field1_rule ? 'block' : 'none' }}
  238. >
  239. 自定义字段名称不能为空
  240. </div>
  241. </Col>
  242. <Col span={2} className="label">
  243. 开启:
  244. </Col>
  245. <Col span={6}>
  246. <Switch
  247. checked={this.state.custom_field1_enable}
  248. checkedChildren="开"
  249. unCheckedChildren="关"
  250. onChange={this.changeCustomEnable}
  251. />
  252. </Col>
  253. </Row>
  254. <Row type="flex" justify="center" className="row save">
  255. <Col span={4} className="save-button">
  256. <Button className="m-btn btn-save" icon="save" type="primary" onClick={this.editGroup}>
  257. 保 存
  258. </Button>
  259. </Col>
  260. </Row>
  261. {/* 只有超级管理员能删除分组 */}
  262. {this.props.curUserRole === 'admin' ? (
  263. <Row type="flex" justify="center" className="danger-container">
  264. <Col span={24} className="title">
  265. <h2 className="content">
  266. <Icon type="exclamation-circle-o" /> 危险操作
  267. </h2>
  268. <Button onClick={this.toggleDangerOptions}>
  269. 查 看<Icon type={this.state.showDangerOptions ? 'up' : 'down'} />
  270. </Button>
  271. </Col>
  272. {this.state.showDangerOptions ? (
  273. <Card hoverable={true} className="card-danger" style={{ width: '100%' }}>
  274. <div className="card-danger-content">
  275. <h3>删除分组</h3>
  276. <p>分组一旦删除,将无法恢复数据,请慎重操作!</p>
  277. <p>只有超级管理员有权限删除分组。</p>
  278. </div>
  279. <Button type="danger" ghost className="card-danger-btn" onClick={this.showConfirm}>
  280. 删除
  281. </Button>
  282. </Card>
  283. ) : null}
  284. </Row>
  285. ) : null}
  286. </div>
  287. );
  288. }
  289. }
  290. export default GroupSetting;