-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const formatTime = timeInSec => { | ||
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 hasBoardsToSwitch = switcherBoards.length > 1; | ||
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 (!hasBoardsToSwitch) { | ||
return; | ||
} | ||
|
||
switchBoard(direction); | ||
setTimeElapsed(0); | ||
}; | ||
|
||
const handlePlayToggle = () => { | ||
setIsPlaying(prevState => !prevState); | ||
}; | ||
|
||
useEffect( | ||
() => { | ||
navigate(switcherBoards[boardIndex] || switcherBoards[0]); | ||
}, | ||
[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, | ||
hasBoardsToSwitch, | ||
isPlaying, | ||
nextBoardTitle, | ||
prevBoardTitle, | ||
switchInterval, | ||
timeElapsed | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 && board.title | ||
); | ||
|
||
export const getSwitchInterval = createSelector( | ||
[getBoard], | ||
(board) => board && board.switchInterval | ||
); | ||
|
||
export const getSwitcherBoards = createSelector( | ||
[getAllBoards, getBoardsById], | ||
(allBoards, boardsById) => allBoards.filter(boardId => boardsById[boardId].autoSwitch) | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.