111 lines
3.1 KiB
TypeScript
Raw Normal View History

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';
import {TagType} from '../../types/VideoTypes';
import {VideoTypes} from '../../types/ApiTypes';
import {addKeyHandler, removeKeyHandler} from '../../utils/ShortkeyHandler';
2020-12-29 19:39:30 +00:00
interface state {
videos: VideoTypes.VideoUnloadedType[];
2020-12-29 19:39:30 +00:00
tags: TagType[];
}
interface GetRandomMoviesType {
Videos: VideoTypes.VideoUnloadedType[];
Tags: TagType[];
2020-12-29 19:39:30 +00:00
}
2020-06-01 20:46:28 +02:00
/**
* Randompage shuffles random viedeopreviews and provides a shuffle btn
*/
2020-12-29 19:39:30 +00:00
class RandomPage extends React.Component<{}, state> {
readonly LoadNR = 3;
2020-12-29 19:39:30 +00:00
constructor(props: {}) {
super(props);
2020-06-01 20:46:28 +02:00
this.state = {
videos: [],
tags: []
2020-06-01 20:46:28 +02:00
};
this.keypress = this.keypress.bind(this);
2020-06-01 20:46:28 +02:00
}
2020-12-29 19:39:30 +00:00
componentDidMount(): void {
addKeyHandler(this.keypress);
this.loadShuffledvideos(this.LoadNR);
2020-06-01 20:46:28 +02:00
}
componentWillUnmount(): void {
removeKeyHandler(this.keypress);
}
2020-12-29 19:39:30 +00:00
render(): JSX.Element {
return (
<div>
<PageTitle title='Random Videos' subtitle='4pc' />
<SideBar>
<SideBarTitle>Visible Tags:</SideBarTitle>
{this.state.tags.map((m) => (
<Tag key={m.TagId} tagInfo={m} />
))}
</SideBar>
2020-06-01 20:46:28 +02:00
{this.state.videos.length !== 0 ? (
<VideoContainer data={this.state.videos}>
<div className={style.Shufflebutton}>
<button onClick={(): void => this.shuffleclick()} className={style.btnshuffle}>
Shuffle
</button>
</div>
</VideoContainer>
) : (
<div>No Data found!</div>
)}
</div>
);
2020-06-01 20:46:28 +02:00
}
/**
* click handler for shuffle btn
*/
2020-12-29 19:39:30 +00:00
shuffleclick(): void {
this.loadShuffledvideos(this.LoadNR);
2020-06-01 20:46:28 +02: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}, (result) => {
console.log(result);
this.setState({videos: []}); // needed to trigger rerender of main videoview
this.setState({
videos: result.Videos,
tags: result.Tags
2020-06-01 20:46:28 +02:00
});
});
2020-06-01 20:46:28 +02:00
}
/**
* key event handling
* @param event keyevent
*/
private keypress(event: KeyboardEvent): void {
// bind s to shuffle
if (event.key === 's') {
this.loadShuffledvideos(4);
}
}
2020-06-01 20:46:28 +02:00
}
2020-06-01 21:36:55 +02:00
export default RandomPage;