2020-12-29 19:39:30 +00:00
|
|
|
import {RouteComponentProps} from 'react-router';
|
|
|
|
import React from 'react';
|
|
|
|
import VideoContainer from '../../elements/VideoContainer/VideoContainer';
|
2021-01-29 22:15:17 +00:00
|
|
|
import {APINode, callAPI} from '../../utils/Api';
|
2020-12-29 19:39:30 +00:00
|
|
|
import {withRouter} from 'react-router-dom';
|
2021-01-22 21:05:21 +00:00
|
|
|
import {VideoTypes} from '../../types/ApiTypes';
|
2021-01-26 19:14:57 +00:00
|
|
|
import PageTitle, {Line} from '../../elements/PageTitle/PageTitle';
|
|
|
|
import SideBar, {SideBarTitle} from '../../elements/SideBar/SideBar';
|
|
|
|
import Tag from '../../elements/Tag/Tag';
|
|
|
|
import {DefaultTags, GeneralSuccess} from '../../types/GeneralTypes';
|
|
|
|
import {Button} from '../../elements/GPElements/Button';
|
|
|
|
import SubmitPopup from '../../elements/Popups/SubmitPopup/SubmitPopup';
|
2020-12-29 19:39:30 +00:00
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
interface CategoryViewProps extends RouteComponentProps<{id: string}> {}
|
2020-12-29 19:39:30 +00:00
|
|
|
|
|
|
|
interface CategoryViewState {
|
2021-01-26 19:14:57 +00:00
|
|
|
loaded: boolean;
|
|
|
|
submitForceDelete: boolean;
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* plain class (for unit testing only)
|
|
|
|
*/
|
|
|
|
export class CategoryView extends React.Component<CategoryViewProps, CategoryViewState> {
|
2021-01-22 21:05:21 +00:00
|
|
|
private videodata: VideoTypes.VideoUnloadedType[] = [];
|
2020-12-29 19:39:30 +00:00
|
|
|
|
|
|
|
constructor(props: CategoryViewProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2021-01-26 19:14:57 +00:00
|
|
|
loaded: false,
|
|
|
|
submitForceDelete: false
|
2020-12-29 19:39:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(): void {
|
2021-03-14 14:51:53 +00:00
|
|
|
this.fetchVideoData(parseInt(this.props.match.params.id, 10));
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
componentDidUpdate(prevProps: Readonly<CategoryViewProps>): void {
|
2020-12-29 19:39:30 +00:00
|
|
|
// trigger video refresh if id changed
|
|
|
|
if (prevProps.match.params.id !== this.props.match.params.id) {
|
2021-03-14 14:51:53 +00:00
|
|
|
this.reloadVideoData();
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
reloadVideoData(): void {
|
|
|
|
this.setState({loaded: false});
|
|
|
|
this.fetchVideoData(parseInt(this.props.match.params.id, 10));
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:39:30 +00:00
|
|
|
render(): JSX.Element {
|
|
|
|
return (
|
|
|
|
<>
|
2021-03-14 14:51:53 +00:00
|
|
|
<PageTitle title='Categories' subtitle={this.videodata.length + ' Videos'} />
|
2021-01-26 19:14:57 +00:00
|
|
|
|
|
|
|
<SideBar>
|
|
|
|
<SideBarTitle>Default Tags:</SideBarTitle>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Tag tagInfo={DefaultTags.all} />
|
|
|
|
<Tag tagInfo={DefaultTags.fullhd} />
|
|
|
|
<Tag tagInfo={DefaultTags.hd} />
|
|
|
|
<Tag tagInfo={DefaultTags.lowq} />
|
2021-01-26 19:14:57 +00:00
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
<Line />
|
|
|
|
<Button
|
|
|
|
title='Delete Tag'
|
|
|
|
onClick={(): void => {
|
|
|
|
this.deleteTag(false);
|
|
|
|
}}
|
|
|
|
color={{backgroundColor: 'red'}}
|
|
|
|
/>
|
2021-01-26 19:14:57 +00:00
|
|
|
</SideBar>
|
2021-03-14 14:51:53 +00:00
|
|
|
{this.state.loaded ? <VideoContainer data={this.videodata} /> : null}
|
2020-12-29 19:39:30 +00:00
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
<button
|
|
|
|
data-testid='backbtn'
|
|
|
|
className='btn btn-success'
|
|
|
|
onClick={(): void => {
|
|
|
|
this.props.history.push('/categories');
|
|
|
|
}}>
|
|
|
|
Back to Categories
|
2020-12-29 19:39:30 +00:00
|
|
|
</button>
|
2021-01-26 19:14:57 +00:00
|
|
|
{this.handlePopups()}
|
2020-12-29 19:39:30 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:14:57 +00:00
|
|
|
private handlePopups(): JSX.Element {
|
|
|
|
if (this.state.submitForceDelete) {
|
2021-03-14 14:51:53 +00:00
|
|
|
return (
|
|
|
|
<SubmitPopup
|
|
|
|
onHide={(): void => this.setState({submitForceDelete: false})}
|
|
|
|
submit={(): void => {
|
|
|
|
this.deleteTag(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2021-01-26 19:14:57 +00:00
|
|
|
} else {
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:39:30 +00:00
|
|
|
/**
|
|
|
|
* fetch data for a specific tag from backend
|
|
|
|
* @param id tagid
|
|
|
|
*/
|
2021-01-26 19:14:57 +00:00
|
|
|
private fetchVideoData(id: number): void {
|
2021-05-22 21:33:32 +02:00
|
|
|
callAPI<VideoTypes.VideoUnloadedType[]>(APINode.Video, {action: 'getMovies', Tag: id}, (result) => {
|
2020-12-29 19:39:30 +00:00
|
|
|
this.videodata = result;
|
|
|
|
this.setState({loaded: true});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:14:57 +00:00
|
|
|
/**
|
|
|
|
* delete the current tag
|
|
|
|
*/
|
|
|
|
private deleteTag(force: boolean): void {
|
2021-03-14 14:51:53 +00:00
|
|
|
callAPI<GeneralSuccess>(
|
|
|
|
APINode.Tags,
|
|
|
|
{
|
|
|
|
action: 'deleteTag',
|
|
|
|
TagId: parseInt(this.props.match.params.id, 10),
|
|
|
|
Force: force
|
|
|
|
},
|
|
|
|
(result) => {
|
|
|
|
console.log(result.result);
|
|
|
|
if (result.result === 'success') {
|
|
|
|
this.props.history.push('/categories');
|
|
|
|
} else if (result.result === 'not empty tag') {
|
|
|
|
// show submisison tag to ask if really delete
|
|
|
|
this.setState({submitForceDelete: true});
|
|
|
|
}
|
2021-01-26 19:14:57 +00:00
|
|
|
}
|
2021-03-14 14:51:53 +00:00
|
|
|
);
|
2021-01-26 19:14:57 +00:00
|
|
|
}
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export with react Router wrapped (default use)
|
|
|
|
*/
|
|
|
|
export const CategoryViewWR = withRouter(CategoryView);
|