Move player logic back in store
This commit is contained in:
parent
da2ce4eb08
commit
14ce3b1450
@ -1,27 +1,40 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
import { useSelector, useDispatch } from 'react-redux'
|
import { useSelector, useDispatch } from 'react-redux'
|
||||||
import { hasNextSegment } from '../store/selectors'
|
import {
|
||||||
import { incrementSegment, resetSegment } from '../store/actions'
|
hasNextSegment,
|
||||||
import { Player } from './Player'
|
selectRunning,
|
||||||
|
selectInterval
|
||||||
|
} from '../store/selectors'
|
||||||
|
import { incrementSegment, stop, pause, start } from '../store/actions'
|
||||||
|
import { FiPlay, FiPause, FiSquare } from 'react-icons/fi'
|
||||||
|
import { useInterval } from './generics/useInterval'
|
||||||
|
import { IconButton } from '../styles/IconButton'
|
||||||
|
|
||||||
export const RsvpPlayer = () => {
|
export const RsvpPlayer = () => {
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
|
const running = useSelector(selectRunning)
|
||||||
const hasNext = useSelector(hasNextSegment)
|
const hasNext = useSelector(hasNextSegment)
|
||||||
const wpm = useSelector(state => state.wpm)
|
const interval = useSelector(selectInterval)
|
||||||
|
|
||||||
const onTick = stop => {
|
useInterval(
|
||||||
if (!hasNext) return stop()
|
() => {
|
||||||
dispatch(incrementSegment())
|
if (!hasNext) dispatch(pause())
|
||||||
}
|
else dispatch(incrementSegment())
|
||||||
|
},
|
||||||
|
running ? interval : null
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Player
|
<div>
|
||||||
delay={60000 / wpm}
|
<IconButton Icon={FiSquare} onClick={() => dispatch(stop())} />
|
||||||
onTick={onTick}
|
<IconButton
|
||||||
onStop={() => dispatch(resetSegment())}
|
Icon={running ? FiPause : FiPlay}
|
||||||
|
onClick={() => dispatch(running ? pause() : start())}
|
||||||
|
disabled={!hasNext}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ export const Player = ({ onTick, onStart, onStop, onPause, delay }) => {
|
|||||||
const handleStart = () => {
|
const handleStart = () => {
|
||||||
safeCall(onStart)
|
safeCall(onStart)
|
||||||
start()
|
start()
|
||||||
|
console.log('yay')
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleStop = () => {
|
const handleStop = () => {
|
22
src/components/generics/useInterval.js
Normal file
22
src/components/generics/useInterval.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { useRef, useEffect } from 'react'
|
||||||
|
|
||||||
|
// from: https://overreacted.io/making-setinterval-declarative-with-react-hooks/
|
||||||
|
export function useInterval(callback, delay) {
|
||||||
|
const savedCallback = useRef()
|
||||||
|
const x = 'asdf'
|
||||||
|
// Remember the latest callback.
|
||||||
|
useEffect(() => {
|
||||||
|
savedCallback.current = callback
|
||||||
|
}, [callback])
|
||||||
|
|
||||||
|
// Set up the interval.
|
||||||
|
useEffect(() => {
|
||||||
|
function tick() {
|
||||||
|
savedCallback.current()
|
||||||
|
}
|
||||||
|
if (delay !== null) {
|
||||||
|
let id = setInterval(tick, delay)
|
||||||
|
return () => clearInterval(id)
|
||||||
|
}
|
||||||
|
}, [delay])
|
||||||
|
}
|
@ -11,3 +11,6 @@ export const setMaxLength = length => ({
|
|||||||
payload: length
|
payload: length
|
||||||
})
|
})
|
||||||
export const setWpm = wpm => ({ type: 'SET_WPM', payload: wpm })
|
export const setWpm = wpm => ({ type: 'SET_WPM', payload: wpm })
|
||||||
|
export const start = () => ({ type: 'START' })
|
||||||
|
export const stop = () => ({ type: 'STOP' })
|
||||||
|
export const pause = () => ({ type: 'PAUSE' })
|
||||||
|
@ -10,7 +10,8 @@ export const initialState = {
|
|||||||
curIdx: 0,
|
curIdx: 0,
|
||||||
maxLength: 5,
|
maxLength: 5,
|
||||||
isPlaying: false,
|
isPlaying: false,
|
||||||
wpm: 300
|
wpm: 300,
|
||||||
|
running: false
|
||||||
}
|
}
|
||||||
|
|
||||||
const reducer = {
|
const reducer = {
|
||||||
@ -27,7 +28,10 @@ const reducer = {
|
|||||||
INC_SENTENCE: state => ({ ...state, curIdx: selectNextSentence(state) }),
|
INC_SENTENCE: state => ({ ...state, curIdx: selectNextSentence(state) }),
|
||||||
DEC_SENTENCE: state => ({ ...state, curIdx: selectPrevSentence(state) }),
|
DEC_SENTENCE: state => ({ ...state, curIdx: selectPrevSentence(state) }),
|
||||||
SET_MAX_LENGTH: (state, payload) => ({ ...state, maxLength: payload }),
|
SET_MAX_LENGTH: (state, payload) => ({ ...state, maxLength: payload }),
|
||||||
SET_WPM: (state, payload) => ({ ...state, wpm: payload })
|
SET_WPM: (state, payload) => ({ ...state, wpm: payload }),
|
||||||
|
START: state => ({ ...state, running: true }),
|
||||||
|
STOP: state => ({ ...state, running: false, curIdx: 0 }),
|
||||||
|
PAUSE: state => ({ ...state, running: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const reducerFn = (state, { type, payload }) => {
|
export const reducerFn = (state, { type, payload }) => {
|
||||||
|
@ -77,3 +77,7 @@ export const selectPrevSentence = createSelector(
|
|||||||
|
|
||||||
export const selectHasNextSentence = createSelector(selectNextSentence, Boolean)
|
export const selectHasNextSentence = createSelector(selectNextSentence, Boolean)
|
||||||
export const selectHasPrevSentence = createSelector(selectPrevSentence, Boolean)
|
export const selectHasPrevSentence = createSelector(selectPrevSentence, Boolean)
|
||||||
|
|
||||||
|
export const selectRunning = state => state.running
|
||||||
|
export const selectWpm = state => state.wpm
|
||||||
|
export const selectInterval = createSelector(selectWpm, wpm => 60000 / wpm)
|
||||||
|
@ -30,6 +30,15 @@ const SvgButton = styled.button`
|
|||||||
&::-moz-focus-inner {
|
&::-moz-focus-inner {
|
||||||
border: 0;
|
border: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&[disabled] {
|
||||||
|
color: '#c7c7c7';
|
||||||
|
svg {
|
||||||
|
transform: scale(1.5);
|
||||||
|
|
||||||
|
color: '#c7c7c7';
|
||||||
|
}
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const AccessLabel = styled.span`
|
const AccessLabel = styled.span`
|
||||||
@ -40,9 +49,9 @@ const AccessLabel = styled.span`
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
`
|
`
|
||||||
|
|
||||||
export const IconButton = ({ title, onClick, Icon }) => {
|
export const IconButton = ({ Icon, title, onClick, disabled }) => {
|
||||||
return (
|
return (
|
||||||
<SvgButton onClick={onClick} title={title} tabindex="0">
|
<SvgButton onClick={onClick} title={title} tabindex="0" disabled={disabled}>
|
||||||
{Icon && <Icon aria-hidden="true" focusable="false" />}
|
{Icon && <Icon aria-hidden="true" focusable="false" />}
|
||||||
<AccessLabel>{title}</AccessLabel>
|
<AccessLabel>{title}</AccessLabel>
|
||||||
</SvgButton>
|
</SvgButton>
|
||||||
|
Loading…
Reference in New Issue
Block a user