Skip to content

Commit 7e8c3b4

Browse files
Improve stopwatch
1 parent ddab8c2 commit 7e8c3b4

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

app/components/Stopwatch/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
*/
66

7-
import React from 'react';
7+
import React, { PropTypes } from 'react';
88

99
import { FormattedMessage } from 'react-intl';
1010

@@ -21,7 +21,6 @@ const formattedSeconds = (sec) => {
2121

2222

2323
function Stopwatch(props) {
24-
2524
return (
2625
<CenteredSection>
2726
<TimeDisplay>
@@ -31,14 +30,19 @@ function Stopwatch(props) {
3130
</TimeDisplay>
3231

3332
<ControlGroup>
34-
{(props.intervalsElapsed !== 0
33+
{(props.intervalsElapsed !== 0 && props.isPaused
3534
? <Control onClick={props.resetTimer}><FormattedMessage {...messages.reset} /></Control>
3635
: null
3736
)}
3837

39-
{(props.intervalsElapsed === 0
38+
{(props.isPaused || props.intervalsElapsed === 0
4039
? <Control onClick={props.startTimer}><FormattedMessage {...messages.start} /></Control>
41-
: <Control onClick={props.stopTimer}><FormattedMessage {...messages.stop} /></Control>
40+
: ''
41+
)}
42+
43+
{(!props.isPaused && props.intervalsElapsed !== 0
44+
? <Control onClick={props.pauseTimer}><FormattedMessage {...messages.stop} /></Control>
45+
: ''
4246
)}
4347

4448
</ControlGroup>
@@ -48,7 +52,11 @@ function Stopwatch(props) {
4852
}
4953

5054
Stopwatch.propTypes = {
51-
55+
resetTimer: PropTypes.func.isRequired,
56+
startTimer: PropTypes.func.isRequired,
57+
pauseTimer: PropTypes.func.isRequired,
58+
intervalsElapsed: PropTypes.number,
59+
isPaused: PropTypes.bool,
5260
};
5361

5462
export default Stopwatch;

app/containers/Timer/actions.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import {
88
RESET,
99
START,
10-
STOP,
11-
ADDLAP,
10+
PAUSE,
1211
TICK,
1312
} from './constants';
1413

@@ -23,10 +22,10 @@ export function startTimer(dispatch) {
2322
type: START,
2423
};
2524
}
26-
export function stopTimer() {
25+
export function pauseTimer() {
2726
clearInterval(timer);
2827
return {
29-
type: STOP,
28+
type: PAUSE,
3029
};
3130
}
3231

@@ -36,11 +35,6 @@ export function resetTimer() {
3635
};
3736
}
3837

39-
export function addLap() {
40-
return {
41-
type: ADDLAP,
42-
};
43-
}
4438

4539
function tick() {
4640
return {

app/containers/Timer/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
export const RESET = 'app/Timer/RESET';
88
export const START = 'app/Timer/START';
9-
export const STOP = 'app/Timer/STOP';
9+
export const PAUSE = 'app/Timer/PAUSE';
1010
export const ADDLAP = 'app/Timer/ADDLAP';
1111
export const TICK = 'app/Timer/TICK';

app/containers/Timer/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { connect } from 'react-redux';
99
import { createStructuredSelector } from 'reselect';
1010

1111
import Stopwatch from 'components/Stopwatch';
12-
import { resetTimer, startTimer, stopTimer, addLap } from './actions';
12+
import { resetTimer, startTimer, pauseTimer } from './actions';
1313

1414

15-
import { makeSelectIntervalsElapsed } from './selectors';
15+
import { makeSelectIsPaused, makeSelectIntervalsElapsed } from './selectors';
1616

1717
class Timer extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
1818

@@ -22,8 +22,9 @@ class Timer extends React.PureComponent { // eslint-disable-line react/prefer-st
2222
<Stopwatch
2323
intervalsElapsed={this.props.intervalsElapsed}
2424
startTimer={this.props.startTimer}
25-
stopTimer={this.props.stopTimer}
25+
pauseTimer={this.props.pauseTimer}
2626
resetTimer={this.props.resetTimer}
27+
isPaused={this.props.isPaused}
2728
/>
2829
</div>
2930
);
@@ -34,21 +35,22 @@ class Timer extends React.PureComponent { // eslint-disable-line react/prefer-st
3435
Timer.propTypes = {
3536
resetTimer: PropTypes.func.isRequired,
3637
startTimer: PropTypes.func.isRequired,
37-
stopTimer: PropTypes.func.isRequired,
38+
pauseTimer: PropTypes.func.isRequired,
3839
intervalsElapsed: PropTypes.number,
40+
isPaused: PropTypes.bool,
3941
};
4042

4143
function mapDispatchToProps(dispatch) {
4244
return {
4345
startTimer: () => dispatch(startTimer(dispatch)),
44-
stopTimer: () => dispatch(stopTimer()),
46+
pauseTimer: () => dispatch(pauseTimer()),
4547
resetTimer: () => dispatch(resetTimer()),
46-
lapTimer: () => dispatch(addLap()),
4748
};
4849
}
4950

5051
const mapStateToProps = createStructuredSelector({
5152
intervalsElapsed: makeSelectIntervalsElapsed(),
53+
isPaused: makeSelectIsPaused(),
5254
});
5355

5456

app/containers/Timer/reducer.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import { fromJS } from 'immutable';
88
import {
99
RESET,
10-
ADDLAP,
1110
TICK,
11+
START,
12+
PAUSE,
1213
} from './constants';
1314

1415
const initialState = fromJS({
1516
intervalsElapsed: 0,
16-
laps: [],
17+
isPaused: false,
1718
lastClearedIncrementer: null,
1819
});
1920

@@ -23,14 +24,15 @@ function timerReducer(state = initialState, action) {
2324
case RESET: {
2425
return state.set('intervalsElapsed', 0);
2526
}
27+
case START: {
28+
return state.set('isPaused', false);
29+
}
30+
case PAUSE: {
31+
return state.set('isPaused', true);
32+
}
2633
case TICK: {
2734
return state.set('intervalsElapsed', state.get('intervalsElapsed') + 1);
2835
}
29-
case ADDLAP: {
30-
const laps = state.laps.slice();
31-
laps.push(this.state.intervalsElapsed);
32-
return state.set('laps', laps);
33-
}
3436
default: {
3537
return state;
3638
}

app/containers/Timer/selectors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ const makeSelectIntervalsElapsed = () => createSelector(
1111
(timerState) => timerState.get('intervalsElapsed')
1212
);
1313

14+
const makeSelectIsPaused = () => createSelector(
15+
selectTimer,
16+
(timerState) => timerState.get('isPaused')
17+
);
18+
1419
export {
1520
selectTimer,
1621
makeSelectIntervalsElapsed,
22+
makeSelectIsPaused,
1723
};

0 commit comments

Comments
 (0)