mock平台

InterfaceContent.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { PureComponent as Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import { Tabs, Modal, Button } from 'antd';
  5. import Edit from './Edit.js';
  6. import View from './View.js';
  7. import { Prompt } from 'react-router';
  8. import { fetchInterfaceData } from '../../../../reducer/modules/interface.js';
  9. import { withRouter } from 'react-router-dom';
  10. import Run from './Run/Run.js';
  11. const plugin = require('client/plugin.js');
  12. const TabPane = Tabs.TabPane;
  13. @connect(
  14. state => {
  15. return {
  16. curdata: state.inter.curdata,
  17. list: state.inter.list,
  18. editStatus: state.inter.editStatus
  19. };
  20. },
  21. {
  22. fetchInterfaceData
  23. }
  24. )
  25. class Content extends Component {
  26. static propTypes = {
  27. match: PropTypes.object,
  28. list: PropTypes.array,
  29. curdata: PropTypes.object,
  30. fetchInterfaceData: PropTypes.func,
  31. history: PropTypes.object,
  32. editStatus: PropTypes.bool
  33. };
  34. constructor(props) {
  35. super(props);
  36. this.title = 'YApi-高效、易用、功能强大的可视化接口管理平台';
  37. this.state = {
  38. curtab: 'view',
  39. visible: false,
  40. nextTab: ''
  41. };
  42. }
  43. componentWillMount() {
  44. const params = this.props.match.params;
  45. this.actionId = params.actionId;
  46. this.handleRequest(this.props);
  47. }
  48. componentWillUnmount() {
  49. document.getElementsByTagName('title')[0].innerText = this.title;
  50. }
  51. componentWillReceiveProps(nextProps) {
  52. const params = nextProps.match.params;
  53. if (params.actionId !== this.actionId) {
  54. this.actionId = params.actionId;
  55. this.handleRequest(nextProps);
  56. }
  57. }
  58. handleRequest(nextProps) {
  59. const params = nextProps.match.params;
  60. this.props.fetchInterfaceData(params.actionId);
  61. this.setState({
  62. curtab: 'view'
  63. });
  64. }
  65. switchToView = () => {
  66. this.setState({
  67. curtab: 'view'
  68. });
  69. };
  70. onChange = key => {
  71. if (this.state.curtab === 'edit' && this.props.editStatus) {
  72. this.showModal();
  73. } else {
  74. this.setState({
  75. curtab: key
  76. });
  77. }
  78. this.setState({
  79. nextTab: key
  80. });
  81. };
  82. // 确定离开页面
  83. handleOk = () => {
  84. this.setState({
  85. visible: false,
  86. curtab: this.state.nextTab
  87. });
  88. };
  89. // 离开编辑页面的提示
  90. showModal = () => {
  91. this.setState({
  92. visible: true
  93. });
  94. };
  95. // 取消离开编辑页面
  96. handleCancel = () => {
  97. this.setState({
  98. visible: false
  99. });
  100. };
  101. render() {
  102. if (this.props.curdata.title) {
  103. document.getElementsByTagName('title')[0].innerText =
  104. this.props.curdata.title + '-' + this.title;
  105. }
  106. let InterfaceTabs = {
  107. view: {
  108. component: View,
  109. name: '预览'
  110. },
  111. edit: {
  112. component: Edit,
  113. name: '编辑'
  114. },
  115. run: {
  116. component: Run,
  117. name: '运行'
  118. }
  119. };
  120. plugin.emitHook('interface_tab', InterfaceTabs);
  121. const tabs = (
  122. <Tabs
  123. className="tabs-large"
  124. onChange={this.onChange}
  125. activeKey={this.state.curtab}
  126. defaultActiveKey="view"
  127. >
  128. {Object.keys(InterfaceTabs).map(key => {
  129. let item = InterfaceTabs[key];
  130. return <TabPane tab={item.name} key={key} />;
  131. })}
  132. </Tabs>
  133. );
  134. let tabContent = null;
  135. if (this.state.curtab) {
  136. let C = InterfaceTabs[this.state.curtab].component;
  137. tabContent = <C switchToView={this.switchToView} />;
  138. }
  139. return (
  140. <div className="interface-content">
  141. <Prompt
  142. when={this.state.curtab === 'edit' && this.props.editStatus ? true : false}
  143. message={() => {
  144. // this.showModal();
  145. return '离开页面会丢失当前编辑的内容,确定要离开吗?';
  146. }}
  147. />
  148. {tabs}
  149. {tabContent}
  150. {this.state.visible && (
  151. <Modal
  152. title="你即将离开编辑页面"
  153. visible={this.state.visible}
  154. onCancel={this.handleCancel}
  155. footer={[
  156. <Button key="back" onClick={this.handleCancel}>
  157. 取 消
  158. </Button>,
  159. <Button key="submit" onClick={this.handleOk}>
  160. 确 定
  161. </Button>
  162. ]}
  163. >
  164. <p>离开页面会丢失当前编辑的内容,确定要离开吗?</p>
  165. </Modal>
  166. )}
  167. </div>
  168. );
  169. }
  170. }
  171. export default withRouter(Content);