header.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React from 'react'
  2. import { Button, Popconfirm } from 'antd'
  3. import { connect } from 'dva'
  4. import './header.less'
  5. class DataSourceDetailHeader extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. visibleConfirm: false,
  10. visibleSaveConfirm: false,
  11. }
  12. }
  13. handleVisibleChange = (visible) => {
  14. this.setState({ visibleConfirm: visible });
  15. }
  16. handleVisibleSaveConfirmChange = (visible) => {
  17. if(this.isValid()) {
  18. this.setState({ visibleSaveConfirm: visible });
  19. }
  20. }
  21. isValid = () => {
  22. const { dataSourceDetail } = this.props;
  23. const { type, name, columns, targetDirty, description } = dataSourceDetail;
  24. return type === 'database' ? (
  25. !!name && name.length < 50 && !targetDirty && !!columns && columns.length > 0 && (description === undefined || description === null || description.length <= 150)
  26. ) : ( type === 'file' ? (
  27. +1 === 2
  28. ) : false )
  29. }
  30. render() {
  31. const { dataSourceDetail, dispatch } = this.props;
  32. const { name } = dataSourceDetail;
  33. return (
  34. <div className='dataSourcedetail-header'>
  35. <div style={{ maxWidth: '400px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
  36. <span className='title-label' title={name || '未命名'}>{name || '未命名'}</span>
  37. </div>
  38. <div className='buttons'>
  39. <Popconfirm
  40. overlayClassName={`close-popconfirm${this.isValid() ? '' : ' confirm-disabled'}`}
  41. placement="bottomLeft"
  42. title="离开前保存修改吗?"
  43. visible={this.state.visibleConfirm}
  44. onVisibleChange={this.handleVisibleChange}
  45. onConfirm={() => {
  46. if(this.isValid()) {
  47. dispatch({ type: 'dataSource/remoteModify' });
  48. dispatch({ type: 'main/redirect', path: '/workshop/datasource' });
  49. }
  50. this.setState({
  51. visibleConfirm: false
  52. });
  53. }}
  54. onCancel={() => {
  55. this.setState({
  56. visibleConfirm: false
  57. }, () => {
  58. dispatch({ type: 'main/redirect', path: '/workshop/datasource' });
  59. });
  60. }}
  61. okText="保存"
  62. cancelText="不保存"
  63. >
  64. <Button onClick={(e) => {
  65. if(!dataSourceDetail.dirty) {
  66. dispatch({ type: 'main/redirect', path: '/workshop/datasource' });
  67. }
  68. }}>
  69. 返回
  70. </Button>
  71. </Popconfirm>
  72. <Popconfirm
  73. placement="bottomLeft"
  74. title={<span style={{ color: 'red' }}>修改数据源可能会对已创建的图表/报表产生影响,是否确认修改?</span>}
  75. visible={this.state.visibleSaveConfirm}
  76. onVisibleChange={this.handleVisibleSaveConfirmChange}
  77. onConfirm={() => {
  78. this.setState({
  79. visibleSaveConfirm: false
  80. }, () => {
  81. dispatch({ type: 'dataSource/remoteModify' });
  82. });
  83. }}
  84. onCancel={() => {
  85. this.setState({
  86. visibleSaveConfirm: false
  87. });
  88. }}
  89. okText="确定"
  90. cancelText="取消"
  91. >
  92. <Button disabled={!this.isValid()} onClick={() => {
  93. this.setState({
  94. visibleSaveConfirm: true
  95. });
  96. }}>
  97. 保存
  98. </Button>
  99. </Popconfirm>
  100. </div>
  101. </div>
  102. );
  103. }
  104. }
  105. export default connect(({ present: { dataSourceDetail } }) => ({ dataSourceDetail }))(DataSourceDetailHeader);