NotificationCardRender.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, {Component} from 'react';
  2. import {FormattedMessage} from 'react-intl';
  3. import {Input, Tooltip, Icon, Button} from 'antd';
  4. import {graphqlUrl, manageUsers} from "../../../../../config";
  5. import {ADD_NOTIFICATION, UPDATE_NOTIFICATION, UPDATE_PROJECT_ONLY_STATUS} from "../../../../../gql";
  6. import {request} from 'graphql-request'
  7. import {idGen} from "../../../../../func";
  8. const valueToKey = {
  9. 'dingding webhook': 'dingding webhook',
  10. 'notification name': 'requestMethod'
  11. };
  12. const toolTipTitle = {
  13. 'dingding webhook': 'its dingding webhook',
  14. 'notification name': 'its notification name'
  15. };
  16. const youMustFill = {
  17. 'dingding webhook': false,
  18. 'notification name': false
  19. };
  20. class NotificationCardFetch extends Component {
  21. constructor(props) {
  22. super(props);
  23. if(props.notification !== '' && props.notification !== null) {
  24. let {type, webhook, name} = props.notification;
  25. this.state = {
  26. type,
  27. webhook,
  28. name
  29. }
  30. } else {
  31. this.state = {
  32. type: 'dingding',
  33. webhook: '',
  34. name: props.defaultName
  35. }
  36. }
  37. }
  38. componentWillReceiveProps(next) {
  39. if(next.notification !== '' && next.notification !== null) {
  40. let {type, webhook, name} = next.notification;
  41. this.setState({
  42. type,
  43. webhook,
  44. name
  45. })
  46. } else {
  47. this.setState({
  48. type: 'dingding',
  49. webhook: '',
  50. name: next.defaultName
  51. })
  52. }
  53. }
  54. ok = (id, userID, projectID, notification) => {
  55. let {type, webhook, name} = this.state;
  56. let varObj = {
  57. id,
  58. user_id: userID,
  59. type,
  60. webhook,
  61. name,
  62. createdAt: new Date().getTime(),
  63. updatedAt: ''
  64. };
  65. if (notification === '') {
  66. request(graphqlUrl, ADD_NOTIFICATION, varObj).then(
  67. data => {
  68. if (data.create_notification !== null) {
  69. this.setState({
  70. showOK: true
  71. });
  72. // 写回 project
  73. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  74. id: projectID,
  75. updatedAt: new Date().getTime(),
  76. projectStatus: 'notificationed'
  77. }).then(data => {
  78. if (data.update_project !== null)
  79. this.props.stepByStep(4);
  80. })
  81. }
  82. setTimeout(() => {
  83. this.setState({
  84. showOK: false
  85. })
  86. }, 1500)
  87. }
  88. )
  89. } else {
  90. let varObj = {
  91. id: notification.id,
  92. type,
  93. webhook,
  94. name,
  95. updatedAt: new Date().getTime()
  96. };
  97. request(graphqlUrl, UPDATE_NOTIFICATION, varObj).then(
  98. data => {
  99. if (data.update_notification !== null) {
  100. this.setState({
  101. showOK: true
  102. });
  103. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  104. id: projectID,
  105. updatedAt: new Date().getTime(),
  106. projectStatus: 'updated'
  107. }).then(data => {
  108. if (data.update_project !== null)
  109. this.props.stepByStep(4);
  110. })
  111. }
  112. setTimeout(() => {
  113. this.setState({
  114. showOK: false
  115. })
  116. }, 1500)
  117. }
  118. )
  119. }
  120. };
  121. render() {
  122. return (
  123. <div>
  124. <div className='kind'>
  125. <p style={{marginBottom: 10}}><b><FormattedMessage id="Ding Talk"/></b></p>
  126. <div className='item'>
  127. <p style={{marginBottom: 10}}>
  128. <span className='item-title'>
  129. <span>
  130. {
  131. youMustFill['dingding webhook'] ?
  132. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  133. :
  134. ''
  135. }
  136. <FormattedMessage id="Web hook"/>
  137. &nbsp;
  138. </span>
  139. <Tooltip placement="top" title={toolTipTitle['dingding webhook']}>
  140. <Icon type="question-circle"/>
  141. </Tooltip>
  142. </span>
  143. <Input style={{width: 400}} value={this.state.webhook} onChange={(e) => {
  144. this.setState({
  145. webhook: e.target.value
  146. })
  147. }}/>
  148. </p>
  149. <p style={{marginBottom: 10}}>
  150. <span className='item-title'>
  151. <span>
  152. {
  153. youMustFill['notification name'] ?
  154. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  155. :
  156. ''
  157. }
  158. <FormattedMessage id="Notification Name"/>
  159. &nbsp;
  160. </span>
  161. <Tooltip placement="top" title={toolTipTitle['notification name']}>
  162. <Icon type="question-circle"/>
  163. </Tooltip>
  164. </span>
  165. <Input style={{width: 300}} value={this.state.name} onChange={(e) => {
  166. this.setState({
  167. name: e.target.value
  168. })
  169. }}/></p>
  170. </div>
  171. </div>
  172. {/*<div className='kind'>*/}
  173. {/*<p><b>QQ</b></p>*/}
  174. {/*<div className='item'>*/}
  175. {/*<p>*/}
  176. {/*<span className='item-title'>*/}
  177. {/*<span>Web hook </span>*/}
  178. {/*<Tooltip placement="top" title='hahahha'>*/}
  179. {/*<Icon type="question-circle"/>*/}
  180. {/*</Tooltip>*/}
  181. {/*</span>*/}
  182. {/*<Input style={{width: 600}}/>*/}
  183. {/*</p>*/}
  184. {/*<p>*/}
  185. {/*<span className='item-title'>*/}
  186. {/*<span>Notification Name </span>*/}
  187. {/*<Tooltip placement="top" title='heheheheh'>*/}
  188. {/*<Icon type="question-circle"/>*/}
  189. {/*</Tooltip>*/}
  190. {/*</span>*/}
  191. {/*<Input style={{width: 300}}/></p>*/}
  192. {/*</div>*/}
  193. {/*</div>*/}
  194. <div>
  195. {
  196. manageUsers.includes(this.props.userID) ?
  197. <Button type="primary" onClick={() => {
  198. const id = idGen('notification');
  199. this.ok(id, this.props.userID, this.props.projectID, this.props.notification);
  200. }}><FormattedMessage id="save"/></Button>
  201. :
  202. this.props.trialcase ?
  203. ''
  204. :
  205. <Button type="primary" onClick={() => {
  206. const id = idGen('notification');
  207. this.ok(id, this.props.userID, this.props.projectID, this.props.notification);
  208. }}><FormattedMessage id="save"/></Button>
  209. }
  210. </div>
  211. </div>
  212. )
  213. }
  214. }
  215. export default NotificationCardFetch;