Skip to content

Commit

Permalink
feat(showcase): set interval to go through slides automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
KatvonRivia committed Nov 26, 2019
1 parent 4bb5a66 commit 12830cf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/scripts/components/story-pagination/story-pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {PreviousIcon} from '../icons/previous-icon';
import {NextIcon} from '../icons/next-icon';
import {PlayIcon} from '../icons/play-icon';
import {RemoveIcon} from '../icons/remove-icon';
import {useStoryNavigation} from '../../libs/get-navigation-links';

import {Slide} from '../../types/story';
import {StoryMode} from '../../types/story-mode';
Expand All @@ -19,26 +18,29 @@ interface Props {
storyId: string;
mode: StoryMode;
slides: Slide[];
previousLink?: string;
showPrevious?: boolean;
nextLink?: string;
showNext?: boolean;
}

const StoryPagination: FunctionComponent<Props> = ({
currentPage,
mode,
slides
slides,
previousLink,
showPrevious,
nextLink,
showNext
}) => {
const intl = useIntl();
const isPresenterMode = mode === StoryMode.Present;
const classes = cx(styles.pagination, isPresenterMode && styles.present);

const {previousLink, showPrevious, nextLink, showNext} = useStoryNavigation(
slides,
currentPage
);

return (
<div className={classes}>
<div className={styles.controls}>
{showPrevious ? (
{showPrevious && previousLink ? (
<Link to={previousLink} className={styles.icon}>
<PreviousIcon />
</Link>
Expand All @@ -50,7 +52,7 @@ const StoryPagination: FunctionComponent<Props> = ({
{currentPage + 1}/{slides.length}
</span>

{showNext ? (
{showNext && nextLink ? (
<Link to={nextLink} className={styles.icon}>
<NextIcon />
</Link>
Expand Down
27 changes: 26 additions & 1 deletion src/scripts/components/story/story.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {FunctionComponent, useEffect} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {useParams, Redirect} from 'react-router-dom';
import {useParams, Redirect, useLocation, useHistory} from 'react-router-dom';

import StoryPagination from '../story-pagination/story-pagination';
import fetchStory from '../../actions/fetch-story';
Expand All @@ -11,6 +11,8 @@ import Slide from '../slide/slide';
import {State} from '../../reducers';
import config from '../../config/main';
import StoryHeader from '../story-header/story-header';
import {useInterval} from '../../hooks/use-interval';
import {getStoryNavigation} from '../../libs/get-story-navigation';

import {StoryMode} from '../../types/story-mode';

Expand Down Expand Up @@ -39,6 +41,8 @@ const getStoryId = (params: Params, mode: StoryMode) => {

const Story: FunctionComponent<Props> = ({mode}) => {
const params = useParams<Params>();
const {pathname} = useLocation();
const history = useHistory();
const storyId = getStoryId(params, mode);
const story = useSelector((state: State) =>
selectedStorySelector(state, storyId)
Expand Down Expand Up @@ -71,6 +75,23 @@ const Story: FunctionComponent<Props> = ({mode}) => {
[dispatch]
);

const navigationData = getStoryNavigation({
pathname,
pageNumber,
slides: story?.slides
});

const showNext = navigationData?.showNext;
const showPrevious = navigationData?.showPrevious;
const nextLink = navigationData?.nextLink;
const previousLink = navigationData?.previousLink;

useInterval(() => {
if (mode === StoryMode.Showcase) {
showNext && history.replace(`${nextLink}`);
}
}, 5000);

// redirect to first slide when current slide does not exist
if (story && !slide) {
return <Redirect to={`/${mode}/${storyId}/0`} />;
Expand Down Expand Up @@ -101,6 +122,10 @@ const Story: FunctionComponent<Props> = ({mode}) => {
storyId={story.id}
mode={mode}
slides={story.slides}
previousLink={previousLink}
showPrevious={showPrevious}
nextLink={nextLink}
showNext={showNext}
/>
)}
</div>
Expand Down
22 changes: 22 additions & 0 deletions src/scripts/hooks/use-interval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {useEffect, useRef} from 'react';

export const useInterval = (callback: () => void, delay: number) => {
const savedCallback = useRef<() => void | undefined>();

// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current && savedCallback.current();
}

if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import {Slide} from '../types/story';
import {useLocation} from 'react-router-dom';

export const useStoryNavigation = (slides: Slide[], currentPage: number) => {
const location = useLocation();
const nextPageNumber = currentPage + 1;
const previousPageNumber = currentPage - 1;
interface Params {
pathname: string;
pageNumber: number;
slides?: Slide[];
}

export const getStoryNavigation = ({pathname, pageNumber, slides}: Params) => {
if (!slides) {
return null;
}

const nextPageNumber = pageNumber + 1;
const previousPageNumber = pageNumber - 1;
const showNextButton = nextPageNumber < slides.length;
const showPreviousButton = previousPageNumber >= 0;

const pathParts = location.pathname.split('/');
const pathParts = pathname.split('/');
pathParts.pop();
const nextPath = pathParts.concat(nextPageNumber.toString()).join('/');
const previousPath = pathParts
Expand Down

0 comments on commit 12830cf

Please sign in to comment.