Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. case START_TIMER: {
  2. const todo = { ...state.todos.find(item => item.id === action.id) };
  3. todo.startTime = new Date();
  4. const todos = [...state.todos].map(item => item.id === action.id ? todo : item);
  5. return { ...state, timerActive: true, timerTodo: action.id, todos };
  6. }
  7. case STOP_TIMER: {
  8. return { ...state, timerActive: false, timerTodo: null }
  9. }
  10. case UPDATE_TODO_TOTAL: {
  11. const todo = { ...state.todos.find(item => item.id === action.id) };
  12. todo.total += 1;
  13. const todos = [...state.todos].map(item => item.id === action.id ? todo : item);
  14. const total = state.total || 0;
  15. return { ...state, todos, total: total + 1 };
  16. }
  17.  
  18. import React, { Component, PropTypes } from 'react'
  19. import classnames from 'classnames'
  20. import moment from 'react-moment'
  21.  
  22.  
  23. let interval;
  24.  
  25. export default class TodoItem extends Component {
  26. static propTypes = {
  27. todo: PropTypes.object.isRequired,
  28. deleteTodo: PropTypes.func.isRequired,
  29. completeTodo: PropTypes.func.isRequired
  30. }
  31.  
  32. componentWillUnmount() {
  33. clearInterval(interval);
  34. }
  35.  
  36. handleDeleteClick = () => {
  37. this.props.deleteTodo(this.props.todo.id);
  38. }
  39.  
  40. handleCompleteClick = () => {
  41. this.props.completeTodo(this.props.todo.id);
  42. }
  43.  
  44. handleStartClick = () => {
  45. this.props.startTimer(this.props.todo.id);
  46. interval = setInterval(() => {
  47. this.props.updateTimer(this.props.todo.id);
  48. }, 1000);
  49. }
  50.  
  51.  
  52. handleStopClick = () => {
  53. this.props.stopTimer(this.props.todo.id);
  54. clearInterval(interval);
  55. }
  56.  
  57.  
  58. render() {
  59. const { todo, timerActive, timerTodo } = this.props
  60. const {total} = todo.total;
  61. const timerDisplay = ('0' + ((total/3600)|0)%60) + ':' + ('0'+ ((total/60)|0)%60).substr(-2) + ':'+('0' + total%60).substr(-2);
  62.  
  63. return (
  64. <li className={classnames({
  65. completed: todo.completed
  66. })}>
  67. <div className="view" style={{ display: 'flex', alignItems: 'center' }} onClick={this.handleSelectToDo}>
  68. <input
  69. className="toggle"
  70. type="checkbox"
  71. checked={todo.completed}
  72. onChange={this.handleCompleteClick}
  73. />
  74. <label style={{ width: '50%' }}>
  75. {todo.text}
  76. </label>
  77. <span style={{ display: 'block', fontSize: 20 }}>{timerDisplay}</span>
  78. {(!timerActive || timerTodo === todo.id) && (
  79. <button
  80. style={{
  81. background: 'transparent',
  82. border: 0,
  83. outline: 0,
  84. fontSize: 12,
  85. cursor: 'pointer',
  86. marginLeft: 30
  87. }}
  88. disabled={timerActive && timerTodo !== todo.id}
  89. onClick={timerActive ? this.handleStopClick : this.handleStartClick}
  90. >{timerActive ? 'Stop' : 'Start'}</button>
  91. )}
  92. <button className="destroy" onClick={this.handleDeleteClick} />
  93. </div>
  94. </li>
  95. )
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement