Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. import React, { PropTypes } from 'react'
  2. import ReactCSSTransitionGroup from 'react/lib/ReactCSSTransitionGroup'
  3. import { connect } from 'react-redux'
  4. import { check, checkRestart } from '../actions/check'
  5. import { sendReport, closeReportMessage } from '../actions/report'
  6. import { CHECK_ITEM_LABELS, STATUS_LABELS, STATUS_PASS, MESSAGE_REPORT_SUCCESS, MESSAGE_REPORT_DESC_SUCCESS } from '../constants'
  7. import _ from 'lodash'
  8. import UI from './ui'
  9.  
  10. const { Row } = UI
  11.  
  12. export const Dashboard = React.createClass({
  13.  
  14. contextTypes: {
  15. router: PropTypes.object.isRequired
  16. },
  17.  
  18. childContextTypes: {
  19. dispatch: PropTypes.func.isRequired
  20. },
  21.  
  22. getChildContext: function() {
  23. return {
  24. dispatch: this.props.dispatch
  25. }
  26. },
  27.  
  28. render: function() {
  29. const { user, check, report } = this.props
  30.  
  31. return (
  32. <div>
  33. <ReactCSSTransitionGroup
  34. transitionName="dashboard-transition"
  35. transitionAppear={true}
  36. transitionAppearTimeout={500}
  37. transitionEnterTimeout={500}
  38. transitionLeaveTimeout={300}>
  39.  
  40. <div className="container dashboard">
  41. <Row>
  42. <DashboardCheckList
  43. user={user}
  44. check={check}
  45. />
  46. </Row>
  47. </div>
  48. <DashboardOver report={report} />
  49. </ReactCSSTransitionGroup>
  50. </div>
  51. )
  52. }
  53. })
  54.  
  55. const DashboardCheckList = React.createClass({
  56.  
  57. contextTypes: {
  58. dispatch: PropTypes.func.isRequired,
  59. },
  60.  
  61. handleSendReport: function() {
  62. const { dispatch } = this.context
  63. dispatch(sendReport())
  64. },
  65.  
  66. handleRestart: function() {
  67. const { dispatch } = this.context
  68. dispatch(checkRestart())
  69. },
  70.  
  71. componentDidMount: function() {
  72. const { dispatch } = this.context
  73. dispatch(check(this.state))
  74. },
  75.  
  76. handleReportMessageCLick: function() {
  77. const { dispatch } = this.context
  78. dispatch(closeReportMessage())
  79. },
  80.  
  81. render: function() {
  82. const { user, check } = this.props
  83. const items = _.transform(check.targets,
  84. (result, value, key) => {
  85. result['components'] = result['components'] || []
  86. result['count'] = result['count'] || {all: 0, pass: 0}
  87. result['components'].push(<DashboardCheckItem key={key} title={key} {...value}/>)
  88. result['count']['pass'] += value.status === STATUS_PASS
  89. result['count']['all']++
  90. return result
  91. }, [])
  92.  
  93. return (
  94. <div>
  95. <div className="dashboard-title">
  96. Идентификационный номер пользователя: {user.id}
  97. </div>
  98.  
  99. <h5>Операции проверки ({items.count.pass}/{items.count.all}):</h5>
  100.  
  101. {items.components}
  102.  
  103. <div className="dashboard-controls">
  104. <button
  105. className="button-primary"
  106. disabled={check.status !== STATUS_PASS}
  107. onClick={this.handleSendReport}>
  108. Отправить
  109. </button>
  110. <button
  111. onClick={this.handleRestart}>
  112. Перезапустить проверку
  113. </button>
  114. </div>
  115. </div>
  116. )
  117. }
  118. })
  119.  
  120. const DashboardCheckItem = React.createClass({
  121. render: function() {
  122. const { title, status, error } = this.props
  123. return (
  124. <div>
  125. <h5 className="dashboard-item__title">{CHECK_ITEM_LABELS[title]}</h5>
  126. <div className="dashboard-item__bar">
  127. <div className={"dashboard-item__percentage" + (status === STATUS_PASS ? " dashboard-item__percentage--100" : '')}></div>
  128. </div>
  129. <p>
  130. {STATUS_LABELS[status]}
  131. </p>
  132. {error ? <p>{error}</p> : false}
  133. </div>
  134. )
  135. }
  136. })
  137.  
  138. const DashboardOver = React.createClass({
  139.  
  140. contextTypes: {
  141. dispatch: PropTypes.func.isRequired,
  142. },
  143.  
  144. handleReportMessageCLick: function() {
  145. const { dispatch } = this.context
  146. dispatch(closeReportMessage())
  147. },
  148.  
  149. render: function() {
  150. const { report } = this.props
  151. return (
  152. <div className={"over" + (report.showOverMessage ? ' over--active' : '')}>
  153. <div className="over__content">
  154. <div className="over__close" onClick={this.handleReportMessageCLick}></div>
  155. <h1>{MESSAGE_REPORT_SUCCESS}</h1>
  156. <p>{MESSAGE_REPORT_DESC_SUCCESS}</p>
  157. </div>
  158. </div>
  159. )
  160. }
  161. })
  162.  
  163. const mapStateToProps = (store) => {
  164. const { user, check, report } = store
  165.  
  166. return { user, check, report }
  167. }
  168.  
  169. export default connect(mapStateToProps)(Dashboard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement