Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. import React, { Component, PropTypes } from 'react';
  2. import cx from 'classnames';
  3. import { calculateTeamcastingResult } from 'utils';
  4.  
  5. const drawCircles = (canvas, data) => {
  6. const context = canvas.getContext('2d');
  7. const centerX = canvas.width / 2;
  8. const centerY = canvas.height / 2;
  9. context.clearRect(0, 0, canvas.width, canvas.height);
  10. context.fillStyle = '#f0f0f0';
  11. context.arc(centerX, centerY, centerX * 2, 0, Math.PI * 2, false);
  12. context.fill();
  13. data.forEach(({ enabled, width, color }) => {
  14. if (enabled) {
  15. context.beginPath();
  16. context.moveTo(centerX, centerY);
  17. context.fillStyle = color;
  18. context.arc(centerX, centerY, width * centerX, 0, Math.PI * 2, false);
  19. context.lineTo(centerX, centerY);
  20. context.closePath();
  21. context.fill();
  22. }
  23. });
  24. };
  25.  
  26. class RoleResultCircle extends Component {
  27. shouldComponentUpdate(nextProps) {
  28. return this.props.isActive !== nextProps.isActive ||
  29. this.props.naturalStrengthSelected !== nextProps.naturalStrengthSelected ||
  30. this.props.naturalPotentialSelected !== nextProps.naturalPotentialSelected ||
  31. this.props.fragileStrengthSelected !== nextProps.fragileStrengthSelected ||
  32. this.props.allRolesSelected !== nextProps.allRolesSelected;
  33. }
  34.  
  35. render() {
  36. const {
  37. allRolesSelected,
  38. assignRoles,
  39. candidateAssignRoleToggle,
  40. candidateId,
  41. checkSelectVisible,
  42. teamcastingData,
  43. isActive,
  44. roleName,
  45. rolesChecked,
  46. naturalStrengthSelected,
  47. naturalPotentialSelected,
  48. fragileStrengthSelected,
  49. } = this.props;
  50.  
  51. const dataSet = calculateTeamcastingResult(teamcastingData);
  52.  
  53. const green = naturalStrengthSelected ? dataSet.green : 0;
  54. const orange = naturalPotentialSelected ? dataSet.orange : 0;
  55. const blue = fragileStrengthSelected ? dataSet.blue : 0;
  56.  
  57. const greenStyle = {
  58. width: green,
  59. height: green,
  60. };
  61.  
  62. const orangeStyle = {
  63. width: green + orange,
  64. height: green + orange,
  65. };
  66.  
  67. const blueStyle = {
  68. width: green + orange + blue,
  69. height: green + orange + blue,
  70. };
  71.  
  72. const isDisabled = !rolesChecked[roleName] && !allRolesSelected;
  73. const isClickable = assignRoles;
  74.  
  75. const assignCandidate = (cId, rName) => {
  76. if (isDisabled || !isClickable) return null;
  77.  
  78. return candidateAssignRoleToggle(cId, rName);
  79. };
  80.  
  81. const colors = {
  82. green: '#30b365',
  83. orange: '#ff9b34',
  84. blue: '#3097cc',
  85. gray: '#bfbfbf',
  86. };
  87.  
  88. return (
  89. <button
  90. className={cx(
  91. 'rr-circle',
  92. { 'rr-circle--active': isActive && !checkSelectVisible },
  93. { 'rr-circle--clickable': isClickable },
  94. )}
  95. onClick={() => assignCandidate(candidateId, roleName, isActive)}
  96. >
  97. <i className="fa fa-check rr-circle__check" />
  98. <canvas
  99. width={70}
  100. height={70}
  101. className="rr-circle__wrapper"
  102. ref={elem => elem && drawCircles(elem,
  103. [
  104. {
  105. enabled: blue,
  106. color: isDisabled ? colors.gray : colors.blue,
  107. width: blueStyle.width / 100,
  108. },
  109. {
  110. enabled: orange,
  111. color: isDisabled ? colors.gray : colors.orange,
  112. width: orangeStyle.width / 100,
  113. },
  114. {
  115. enabled: green,
  116. color: isDisabled ? colors.gray : colors.green,
  117. width: greenStyle.width / 100,
  118. },
  119. ],
  120. )}
  121. />
  122. <div className="rr-circle__number">
  123. {green + orange + blue}
  124. </div>
  125. </button>
  126. );
  127. }
  128. }
  129.  
  130. RoleResultCircle.propTypes = {
  131. assignRoles: PropTypes.bool.isRequired,
  132. candidateId: PropTypes.number.isRequired,
  133. checkSelectVisible: PropTypes.bool,
  134. teamcastingData: PropTypes.shape({
  135. name: PropTypes.string.isRequired,
  136. maximum_number: PropTypes.number.isRequired,
  137. data: PropTypes.arrayOf(PropTypes.shape({
  138. key: PropTypes.string,
  139. value: PropTypes.number.isRequired,
  140. color: PropTypes.string.isRequired,
  141. })),
  142. }),
  143. isActive: PropTypes.bool.isRequired,
  144. roleName: PropTypes.string.isRequired,
  145. rolesChecked: PropTypes.shape({
  146. pioneer: PropTypes.bool.isRequired,
  147. strategist: PropTypes.bool.isRequired,
  148. stimulator: PropTypes.bool.isRequired,
  149. networker: PropTypes.bool.isRequired,
  150. coach: PropTypes.bool.isRequired,
  151. coordinator: PropTypes.bool.isRequired,
  152. producer: PropTypes.bool.isRequired,
  153. controller: PropTypes.bool.isRequired,
  154. }),
  155. candidateAssignRoleToggle: PropTypes.func.isRequired,
  156. naturalStrengthSelected: PropTypes.bool.isRequired,
  157. naturalPotentialSelected: PropTypes.bool.isRequired,
  158. fragileStrengthSelected: PropTypes.bool.isRequired,
  159. allRolesSelected: PropTypes.bool,
  160. };
  161.  
  162. RoleResultCircle.defaultProps = {
  163. assignRoles: false,
  164. candidateId: 0,
  165. checkSelectVisible: false,
  166. teamcastingData: {
  167. name: '',
  168. maximum_number: 1,
  169. data: [
  170. {
  171. key: '',
  172. value: 0,
  173. color: '',
  174. },
  175. ],
  176. },
  177. isActive: false,
  178. roleName: '',
  179. rolesChecked: {
  180. pioneer: false,
  181. strategist: false,
  182. stimulator: false,
  183. networker: false,
  184. coach: false,
  185. coordinator: false,
  186. producer: false,
  187. controller: false,
  188. },
  189. showModalFor: '',
  190. naturalStrengthSelected: true,
  191. naturalPotentialSelected: true,
  192. fragileStrengthSelected: true,
  193. allRolesSelected: true,
  194. candidateAssignRoleToggle: () => {},
  195. };
  196.  
  197. export default RoleResultCircle;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement