Files
OpenMediaCenter/src/pages/RandomPage/RandomPage.tsx
T

130 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-10-25 18:48:23 +00:00
import React from 'react';
import style from './RandomPage.module.css';
import SideBar, {SideBarTitle} from '../../elements/SideBar/SideBar';
import Tag from '../../elements/Tag/Tag';
import PageTitle from '../../elements/PageTitle/PageTitle';
import VideoContainer from '../../elements/VideoContainer/VideoContainer';
import {APINode, callAPI} from '../../utils/Api';
2021-01-22 21:05:21 +00:00
import {TagType} from '../../types/VideoTypes';
import {VideoTypes} from '../../types/ApiTypes';
2021-01-28 19:50:26 +00:00
import {addKeyHandler, removeKeyHandler} from '../../utils/ShortkeyHandler';
2020-12-29 19:39:30 +00:00
interface state {
2021-01-22 21:05:21 +00:00
videos: VideoTypes.VideoUnloadedType[];
2020-12-29 19:39:30 +00:00
tags: TagType[];
}
interface GetRandomMoviesType {
2021-02-23 16:01:29 +00:00
Videos: VideoTypes.VideoUnloadedType[];
Tags: TagType[];
2020-12-29 19:39:30 +00:00
}
2020-06-01 20:46:28 +02:00
2020-08-12 17:50:25 +00:00
/**
* Randompage shuffles random viedeopreviews and provides a shuffle btn
*/
2020-12-29 19:39:30 +00:00
class RandomPage extends React.Component<{}, state> {
2021-04-16 22:44:56 +02:00
readonly LoadNR = 3;
// random seed to load videos, remains page reload.
seed = this.genRandInt();
2020-12-29 19:39:30 +00:00
constructor(props: {}) {
super(props);
2020-06-01 20:46:28 +02:00
// get previously stored location from localstorage
const storedseed = global.localStorage.getItem('randpageseed');
if (storedseed != null) {
this.seed = parseInt(storedseed, 10);
}
2020-06-01 20:46:28 +02:00
this.state = {
2020-06-03 12:26:10 +02:00
videos: [],
tags: []
2020-06-01 20:46:28 +02:00
};
2021-01-28 19:50:26 +00:00
this.keypress = this.keypress.bind(this);
2020-06-01 20:46:28 +02:00
}
genRandInt(): number {
return Math.floor(Math.random() * 2147483647) + 1;
}
2020-12-29 19:39:30 +00:00
componentDidMount(): void {
2021-01-28 19:50:26 +00:00
addKeyHandler(this.keypress);
2021-04-16 22:44:56 +02:00
this.loadShuffledvideos(this.LoadNR);
2020-06-01 20:46:28 +02:00
}
2021-01-28 19:50:26 +00:00
componentWillUnmount(): void {
removeKeyHandler(this.keypress);
}
2020-12-29 19:39:30 +00:00
render(): JSX.Element {
2020-06-02 22:52:28 +02:00
return (
<div>
2021-03-14 14:51:53 +00:00
<PageTitle title='Random Videos' subtitle='4pc' />
2020-06-02 22:52:28 +02:00
<SideBar>
<SideBarTitle>Visible Tags:</SideBarTitle>
2020-06-03 12:26:10 +02:00
{this.state.tags.map((m) => (
2021-03-14 14:51:53 +00:00
<Tag key={m.TagId} tagInfo={m} />
2020-06-03 12:26:10 +02:00
))}
2020-06-02 22:52:28 +02:00
</SideBar>
2020-06-01 20:46:28 +02:00
2021-03-14 14:51:53 +00:00
{this.state.videos.length !== 0 ? (
<VideoContainer data={this.state.videos}>
2020-07-22 19:00:55 +02:00
<div className={style.Shufflebutton}>
2021-03-14 14:51:53 +00:00
<button onClick={(): void => this.shuffleclick()} className={style.btnshuffle}>
Shuffle
</button>
2020-07-22 19:00:55 +02:00
</div>
</VideoContainer>
2021-03-14 14:51:53 +00:00
) : (
<div>No Data found!</div>
)}
2020-06-02 22:52:28 +02:00
</div>
);
2020-06-01 20:46:28 +02:00
}
2020-08-12 17:50:25 +00:00
/**
* click handler for shuffle btn
*/
2020-12-29 19:39:30 +00:00
shuffleclick(): void {
this.genSeed();
2021-04-16 22:44:56 +02:00
this.loadShuffledvideos(this.LoadNR);
2020-06-01 20:46:28 +02:00
}
genSeed(): void {
this.seed = this.genRandInt();
global.localStorage.setItem('randpageseed', this.seed.toString());
}
2020-08-12 17:50:25 +00:00
/**
* load random videos from backend
* @param nr number of videos to load
*/
2020-12-29 19:39:30 +00:00
loadShuffledvideos(nr: number): void {
callAPI<GetRandomMoviesType>(APINode.Video, {action: 'getRandomMovies', Number: nr, Seed: this.seed}, (result) => {
2020-12-17 20:53:22 +00:00
this.setState({videos: []}); // needed to trigger rerender of main videoview
this.setState({
2021-02-23 16:01:29 +00:00
videos: result.Videos,
tags: result.Tags
2020-06-01 20:46:28 +02:00
});
2020-12-17 20:53:22 +00:00
});
2020-06-01 20:46:28 +02:00
}
2021-01-28 19:50:26 +00:00
/**
* key event handling
* @param event keyevent
*/
private keypress(event: KeyboardEvent): void {
// bind s to shuffle
if (event.key === 's') {
this.genSeed();
this.loadShuffledvideos(this.LoadNR);
2021-01-28 19:50:26 +00:00
}
}
2020-06-01 20:46:28 +02:00
}
2020-06-01 21:36:55 +02:00
export default RandomPage;