-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add advanced board switcher functionality #45
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const formatTime = (timeInSec) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parenthesis are not necessary here, but it is convention. Decided style can be included in prettier config in the future There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
const seconds = timeInSec % 60; | ||
const minutes = Math.floor(timeInSec / 60); | ||
const leadingZero = seconds < 10 ? 0 : ''; | ||
|
||
return `${minutes}:${leadingZero}${seconds}`; | ||
}; | ||
|
||
export const getPrevAndNextIndex = (array, currentIndex) => { | ||
const lastIndex = array.length - 1; | ||
const nextIndex = currentIndex < lastIndex ? currentIndex + 1 : 0; | ||
const prevIndex = currentIndex > 0 ? currentIndex - 1 : lastIndex; | ||
|
||
return [prevIndex, nextIndex]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { navigate } from '@reach/router'; | ||
|
||
import { getSwitcherBoards, getSwitchInterval, getBoardTitle, getCurrentBoardId } from '../../selectors'; | ||
import { getPrevAndNextIndex } from './helpers'; | ||
|
||
export const useBoardSwitching = () => { | ||
const switcherBoards = useSelector(getSwitcherBoards); | ||
const currentBoardId = useSelector(getCurrentBoardId); | ||
const initialBoardIndex = switcherBoards.includes(currentBoardId) ? switcherBoards.indexOf(currentBoardId) : 0; | ||
const [boardIndex, setIndex] = useState(initialBoardIndex); | ||
const [isPlaying, setIsPlaying] = useState(false); | ||
const [timeElapsed, setTimeElapsed] = useState(0); | ||
const [prevBoardIndex, nextBoardIndex] = getPrevAndNextIndex(switcherBoards, boardIndex); | ||
const switchInterval = useSelector(state => getSwitchInterval(state, switcherBoards[boardIndex])); | ||
const prevBoardTitle = useSelector(state => getBoardTitle(state, switcherBoards[prevBoardIndex])); | ||
const nextBoardTitle = useSelector(state => getBoardTitle(state, switcherBoards[nextBoardIndex])); | ||
|
||
const switchBoard = useCallback( | ||
(direction) => { | ||
const switchDirection = { | ||
next: nextBoardIndex, | ||
prev: prevBoardIndex | ||
}; | ||
const currentBoardIndex = switchDirection[direction]; | ||
|
||
setIndex(currentBoardIndex); | ||
}, | ||
[nextBoardIndex, prevBoardIndex] | ||
); | ||
|
||
const handleBoardsSwitch = (direction) => () => { | ||
if (switcherBoards.length <= 1) { | ||
return; | ||
} | ||
|
||
switchBoard(direction); | ||
setTimeElapsed(0); | ||
}; | ||
|
||
const handlePlayToggle = () => { | ||
setIsPlaying(prevState => !prevState); | ||
}; | ||
|
||
useEffect( | ||
() => { | ||
navigate(switcherBoards[boardIndex]); | ||
}, | ||
[boardIndex, switcherBoards] | ||
); | ||
|
||
useEffect( | ||
() => { | ||
if (isPlaying) { | ||
const interval = setInterval(() => { | ||
setTimeElapsed(prevState => prevState + 1); | ||
|
||
if (timeElapsed >= switchInterval) { | ||
switchBoard('next'); | ||
setTimeElapsed(0); | ||
} | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
} | ||
}, | ||
[isPlaying, timeElapsed, switchInterval, switchBoard] | ||
); | ||
|
||
return { handleBoardsSwitch, handlePlayToggle, timeElapsed, switchInterval, isPlaying, prevBoardTitle, nextBoardTitle }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I would put here each prop in separate line, but it's also convention and configurable by prettier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createSelector } from 'reselect'; | ||
|
||
const getBoardsById = ({ boards }) => boards.boardsById; | ||
|
||
const getAllBoards = ({ boards }) => boards.allBoards; | ||
|
||
const getUi = ({ ui }) => ui; | ||
|
||
const getBoardId = (_, boardId) => boardId; | ||
|
||
export const getCurrentBoardId = createSelector( | ||
[getUi], | ||
ui => ui.currentBoard | ||
); | ||
|
||
const getBoard = createSelector( | ||
[getBoardsById, getBoardId], | ||
(boardsById, boardId) => boardsById[boardId] | ||
); | ||
|
||
export const getBoardTitle = createSelector( | ||
[getBoard], | ||
(board) => board.title | ||
); | ||
|
||
export const getSwitchInterval = createSelector( | ||
[getBoard], | ||
(board) => board.switchInterval | ||
); | ||
|
||
export const getSwitcherBoards = createSelector( | ||
[getAllBoards, getBoardsById], | ||
(allBoards, boardsById) => allBoards.filter(boardId => boardsById[boardId].autoSwitch) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If invoking
handleChange
on initial render is not intentional, you can use two different ways of binding parameter:() => handleChange('switchInterval')
handleChange.bind(this, 'switchInterval')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bind is definitely slower than arrow, but it is intentional, check the implementation of handler and you will notice a function returning a function.