|
| 1 | +const containerDOM = document.getElementById('container') |
| 2 | +const create = React.createElement |
| 3 | + |
| 4 | +let styles = { |
| 5 | + |
| 6 | +} |
| 7 | + |
| 8 | +class Adjuster extends React.Component{ |
| 9 | + constructor(props){ |
| 10 | + super(props) |
| 11 | + } |
| 12 | + |
| 13 | + labelText = this.props.type + ' length' |
| 14 | + |
| 15 | + render(){ |
| 16 | + return( |
| 17 | + <div class='adjuster'> |
| 18 | + <h6 id={this.props.type+'-label'}>{this.labelText}</h6> |
| 19 | + <div class='adjuster_module'> |
| 20 | + <a |
| 21 | + id={this.props.type+'-decrement'} |
| 22 | + class="large waves-effect btn-floating" |
| 23 | + onClick={() => { |
| 24 | + this.props.adjust(this.props.type+'_length', -1) |
| 25 | + }} |
| 26 | + > |
| 27 | + <Icon id='adjuster_button_up'>keyboard_arrow_down</Icon> |
| 28 | + </a> |
| 29 | + <div class='adjuster-info'> |
| 30 | + <h5 id={this.props.type+'-length'}>{this.props.length}</h5> |
| 31 | + </div> |
| 32 | + <a |
| 33 | + id={this.props.type+'-increment'} |
| 34 | + class="large waves-effect btn-floating" |
| 35 | + onClick={() => { |
| 36 | + this.props.adjust(this.props.type+'_length', 1) |
| 37 | + }} |
| 38 | + > |
| 39 | + <Icon id='adjuster_button_down'>keyboard_arrow_up</Icon> |
| 40 | + </a> |
| 41 | + </div> |
| 42 | + |
| 43 | + </div> |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class Display extends React.Component{ |
| 49 | + constructor(props){ |
| 50 | + super(props) |
| 51 | + } |
| 52 | + |
| 53 | + render(){ |
| 54 | + return( |
| 55 | + <div id='display'> |
| 56 | + <h4 id='timer-label'>{this.props.mode}</h4> |
| 57 | + <h2 id='time-left'>{this.props.mins}:{this.props.secs}</h2> |
| 58 | + </div> |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +class MainTimer extends React.Component{ |
| 65 | + constructor(props){ |
| 66 | + super(props) |
| 67 | + } |
| 68 | + |
| 69 | + interval; |
| 70 | + |
| 71 | + state={ |
| 72 | + mode: 'Session', |
| 73 | + break_length: 5, |
| 74 | + session_length: 25, |
| 75 | + duration: 25*60, |
| 76 | + minutes: '25', |
| 77 | + seconds: '00', |
| 78 | + status: 'Stopped' |
| 79 | + } |
| 80 | + |
| 81 | + startTimer = () => { |
| 82 | + let timer = this.state.duration |
| 83 | + |
| 84 | + let minutes = parseInt(timer / 60, 10) |
| 85 | + let seconds = parseInt(timer % 60, 10); |
| 86 | + |
| 87 | + minutes = minutes < 10 ? "0" + minutes : minutes; |
| 88 | + seconds = seconds < 10 ? "0" + seconds : seconds; |
| 89 | + |
| 90 | + this.setState({ |
| 91 | + status: 'Running', |
| 92 | + minutes: minutes, |
| 93 | + seconds: seconds |
| 94 | + }) |
| 95 | + |
| 96 | + timer = timer - 1 |
| 97 | + |
| 98 | + this.interval = setInterval(function () { |
| 99 | + |
| 100 | + if(this.state.status === 'Stopped'){ |
| 101 | + clearInterval(this.interval) |
| 102 | + } |
| 103 | + |
| 104 | + minutes = parseInt(timer / 60, 10) |
| 105 | + seconds = parseInt(timer % 60, 10); |
| 106 | + |
| 107 | + minutes = minutes < 10 ? "0" + minutes : minutes; |
| 108 | + seconds = seconds < 10 ? "0" + seconds : seconds; |
| 109 | + |
| 110 | + this.setState({ |
| 111 | + minutes: minutes, |
| 112 | + seconds: seconds |
| 113 | + }) |
| 114 | + |
| 115 | + if (timer === 0) { |
| 116 | + document.getElementById('beep').play() |
| 117 | + clearInterval(this.interval) |
| 118 | + let timerElement = document.querySelector('#appTimer') |
| 119 | + if(!timerElement.style.display || timerElement.style.display === 'none'){ |
| 120 | + timerElement.style.display = 'flex' |
| 121 | + } |
| 122 | + setTimeout(() => { |
| 123 | + if(this.state.mode === 'Session'){ |
| 124 | + this.startBreak() |
| 125 | + }else{ |
| 126 | + this.startSession() |
| 127 | + } |
| 128 | + }, 100) |
| 129 | + }else{ |
| 130 | + timer = timer -1 |
| 131 | + } |
| 132 | + |
| 133 | + }.bind(this), 1000); |
| 134 | + } |
| 135 | + |
| 136 | + adjust = (type, value) => { |
| 137 | + if(this.state[type] === 1 && value < 0){ |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + if(this.state[type] === 60 && value >0){ |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + this.state[type] = this.state[type] + value |
| 146 | + this.state.duration = this.state.session_length * 60 |
| 147 | + |
| 148 | + let mins = parseInt(this.state.duration / 60, 10) |
| 149 | + let secs = parseInt(this.state.duration % 60, 10) |
| 150 | + |
| 151 | + mins= mins < 10 ? "0" + mins : mins |
| 152 | + secs = secs < 10 ? "0" + secs : secs |
| 153 | + |
| 154 | + this.setState({ |
| 155 | + minutes: mins, |
| 156 | + seconds: secs |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + startSession = () => { |
| 161 | + this.setState({ |
| 162 | + mode: 'Session', |
| 163 | + duration: this.state.session_length *60 |
| 164 | + }) |
| 165 | + this.startTimer() |
| 166 | + this.forceUpdate() |
| 167 | + } |
| 168 | + |
| 169 | + startBreak = () => { |
| 170 | + this.state.mode = 'Break' |
| 171 | + this.state.duration = this.state.break_length * 60 |
| 172 | + this.forceUpdate() |
| 173 | + this.startTimer() |
| 174 | + } |
| 175 | + |
| 176 | + reset = () => { |
| 177 | + document.getElementById('beep').pause(); |
| 178 | + document.getElementById('beep').currentTime = 0; |
| 179 | + clearInterval(this.interval) |
| 180 | + this.setState({ |
| 181 | + mode: 'Session', |
| 182 | + break_length: 5, |
| 183 | + session_length: 25, |
| 184 | + duration: 25 * 60, |
| 185 | + minutes: '25', |
| 186 | + seconds: '00', |
| 187 | + status: 'Stopped' |
| 188 | + }) |
| 189 | + } |
| 190 | + |
| 191 | + startStop = () => { |
| 192 | + if(this.state.status === 'Stopped'){ |
| 193 | + this.startSession() |
| 194 | + } |
| 195 | + if(this.state.status === 'Paused'){ |
| 196 | + this.state.duration = (this.state.minutes * 60) + this.state.seconds |
| 197 | + this.startTimer() |
| 198 | + } |
| 199 | + if(this.state.status === 'Running'){ |
| 200 | + this.state.status = 'Paused' |
| 201 | + clearInterval(this.interval) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + render(){ |
| 206 | + return( |
| 207 | + <div id='appTimer'> |
| 208 | + <audio id='beep' ref="audio_tag" src="../sfx/beep.wav"/> |
| 209 | + <div id='first'> |
| 210 | + <Display mode={this.state.mode} mins={this.state.minutes} secs={this.state.seconds}/> |
| 211 | + </div> |
| 212 | + <div id='second'> |
| 213 | + <a |
| 214 | + id='start_stop' |
| 215 | + class="large waves-effect btn-floating" |
| 216 | + onClick={() => { |
| 217 | + this.startStop() |
| 218 | + }} |
| 219 | + ><Icon id='timer_button_start'>play_arrow</Icon></a> |
| 220 | + <a |
| 221 | + id='reset' |
| 222 | + class="large waves-effect btn-floating" |
| 223 | + onClick={() => { |
| 224 | + this.reset() |
| 225 | + }} |
| 226 | + ><Icon id='timer_button_reset'>restore</Icon></a> |
| 227 | + </div> |
| 228 | + <div id='third'> |
| 229 | + <Adjuster type='session' length={this.state.session_length} adjust={this.adjust}/> |
| 230 | + <Adjuster type='break' length={this.state.break_length} adjust={this.adjust}/> |
| 231 | + </div> |
| 232 | + </div> |
| 233 | + ) |
| 234 | + } |
| 235 | +} |
| 236 | + |
0 commit comments