Skip to content
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 3 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cogboard-webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cogboard-webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"@emotion/styled": "^10.0.14",
"@material-ui/core": "^4.2.1",
"@material-ui/icons": "^4.2.1",
"http-proxy-middleware": "^0.20.0",
"@reach/router": "^1.2.1",
"http-proxy-middleware": "^0.20.0",
"moment-timezone": "^0.5.26",
"react": "^16.8.6",
"react-dnd": "^9.3.4",
Expand All @@ -19,6 +19,7 @@
"react-scripts": "3.0.1",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"uuid": "^3.3.2"
},
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions cogboard-webapp/src/components/BoardCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const BoardCard = ({ boardData, className }) => {
autoSwitch,
columns,
id,
switchInterval,
title
} = boardData;
const [open, openDialog, handleDialogClose] = useDialogToggle();
Expand Down Expand Up @@ -78,10 +79,11 @@ const BoardCard = ({ boardData, className }) => {
>
<EditBoard
closeDialog={handleDialogClose}
autoSwitch={autoSwitch}
columns={columns}
id={id}
switchInterval={switchInterval}
title={title}
columns={columns}
autoSwitch={autoSwitch}
/>
</AppDialog>
</div>
Expand Down
16 changes: 16 additions & 0 deletions cogboard-webapp/src/components/BoardForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { StyledFieldset } from './styled';

const BoardForm = ({ renderActions, ...initialFormValues }) => {
const { values, handleChange } = useFormData(initialFormValues);
const { autoSwitch } = values;

return (
<>
Expand Down Expand Up @@ -51,6 +52,19 @@ const BoardForm = ({ renderActions, ...initialFormValues }) => {
label="Auto switch"
/>
</FormControl>
{autoSwitch &&
<TextField
onChange={handleChange('switchInterval')}
Copy link
Collaborator

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:

  1. () => handleChange('switchInterval')
  2. handleChange.bind(this, 'switchInterval')

Copy link
Collaborator Author

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.

id="switchInterval"
InputLabelProps={{
shrink: true
}}
label="Switch interval [s]"
margin="normal"
value={values.switchInterval}
type="number"
/>
}
</StyledFieldset>
{renderActions(values)}
</>
Expand All @@ -60,12 +74,14 @@ const BoardForm = ({ renderActions, ...initialFormValues }) => {
BoardForm.propTypes = {
autoSwitch: bool,
columns: number,
switchInterval: number,
title: string,
};

BoardForm.defaultProps = {
autoSwitch: true,
columns: 8,
switchInterval: 60,
title: 'Board',
};

Expand Down
15 changes: 15 additions & 0 deletions cogboard-webapp/src/components/BoardSwitcher/helpers.js
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];
};
82 changes: 82 additions & 0 deletions cogboard-webapp/src/components/BoardSwitcher/hooks.js
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
};
};
90 changes: 47 additions & 43 deletions cogboard-webapp/src/components/BoardSwitcher/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
import React from 'react';
import { useSelector, shallowEqual } from 'react-redux';
import { navigate } from '@reach/router';

import { IconButton } from '@material-ui/core';
import { SkipPrevious, PlayArrow, SkipNext } from '@material-ui/icons';
import { useBoardSwitching } from './hooks';
import { formatTime } from './helpers';

import { IconButton, Tooltip } from '@material-ui/core';
import { SkipPrevious, PlayArrow, SkipNext, Pause } from '@material-ui/icons';
import { StyledTimer } from './styled';

const BoardSwitcher = ({ className }) => {
const switcherBoards = useSelector(
({ boards }) => boards.allBoards.filter(boardId => boards.boardsById[boardId].autoSwitch),
shallowEqual
);
const { currentBoard } = useSelector(({ ui }) => ui);
const currentBoardIndex = switcherBoards.includes(currentBoard) ? switcherBoards.indexOf(currentBoard) : 0;
const lastBoardIndex = switcherBoards.length - 1;

const handleBoardsSwitch = (direction) => () => {
if (switcherBoards.length <= 1) {
return;
}
const {
handleBoardsSwitch,
handlePlayToggle,
hasBoardsToSwitch,
isPlaying,
nextBoardTitle,
prevBoardTitle,
switchInterval,
timeElapsed
} = useBoardSwitching();
const timeLeft = switchInterval - timeElapsed;

const switchDirection = {
next: currentBoardIndex < lastBoardIndex ? currentBoardIndex + 1 : 0,
prev: currentBoardIndex > 0 ? currentBoardIndex - 1 : lastBoardIndex,
};
const boardIndex = switchDirection[direction];

navigate(switcherBoards[boardIndex]);
};
if (!hasBoardsToSwitch) {
return null;
}

return (
<div className={className}>
<StyledTimer>
0:48
</StyledTimer>
<IconButton
onClick={handleBoardsSwitch('prev')}
color="inherit"
aria-label="Open drawer"
edge="start"
<StyledTimer>{formatTime(timeLeft)}</StyledTimer>
<Tooltip
title={prevBoardTitle}
placement="bottom-end"
>
<SkipPrevious />
</IconButton>
<IconButton
onClick={handleBoardsSwitch('prev')}
color="inherit"
aria-label="Next board"
edge="start"
>
<SkipPrevious />
</IconButton>
</Tooltip>
<IconButton
onClick={handlePlayToggle}
color="inherit"
aria-label="Open drawer"
aria-label="Auto switch boards"
edge="start"
>
<PlayArrow />
{isPlaying ? <Pause /> : <PlayArrow />}
</IconButton>
<IconButton
onClick={handleBoardsSwitch('next')}
color="inherit"
aria-label="Open drawer"
edge="start"
<Tooltip
title={nextBoardTitle}
placement="bottom-end"
>
<SkipNext />
</IconButton>
<IconButton
onClick={handleBoardsSwitch('next')}
color="inherit"
aria-label="Next board"
edge="start"
>
<SkipNext />
</IconButton>
</Tooltip>
</div>
);
};
Expand Down
34 changes: 34 additions & 0 deletions cogboard-webapp/src/selectors/index.js
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)
);