OpenMediaCenter/src/pages/HomePage/SearchHandling.tsx

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-12-29 19:39:30 +00:00
import {RouteComponentProps} from 'react-router';
import React from 'react';
import {withRouter} from 'react-router-dom';
import {APINode, callAPI} from '../../utils/Api';
2020-12-29 19:39:30 +00:00
import VideoContainer from '../../elements/VideoContainer/VideoContainer';
import PageTitle from '../../elements/PageTitle/PageTitle';
import SideBar from '../../elements/SideBar/SideBar';
import {VideoTypes} from '../../types/ApiTypes';
2020-12-29 19:39:30 +00:00
interface params {
name: string;
}
interface Props extends RouteComponentProps<params> {}
2020-12-29 19:39:30 +00:00
interface state {
data: VideoTypes.VideoUnloadedType[];
2020-12-29 19:39:30 +00:00
}
export class SearchHandling extends React.Component<Props, state> {
constructor(props: Props) {
2020-12-29 19:39:30 +00:00
super(props);
this.state = {
data: []
};
}
componentDidMount(): void {
this.searchVideos(this.props.match.params.name);
}
render(): JSX.Element {
return (
<>
<PageTitle title='Search' subtitle={this.props.match.params.name + ': ' + this.state.data.length} />
<SideBar hiddenFrame />
2020-12-29 19:39:30 +00:00
{this.getVideoData()}
</>
);
}
/**
* get videocontainer if data loaded
*/
getVideoData(): JSX.Element {
if (this.state.data.length !== 0) {
return <VideoContainer data={this.state.data} />;
2020-12-29 19:39:30 +00:00
} else {
return <div>No Data found!</div>;
2020-12-29 19:39:30 +00:00
}
}
/**
* search for a keyword in db and update previews
*
* @param keyword The keyword to search for
*/
searchVideos(keyword: string): void {
2021-05-25 22:34:29 +02:00
callAPI(APINode.Video, {action: 'getSearchKeyWord', KeyWord: keyword}, (result: VideoTypes.VideoUnloadedType[]) => {
2020-12-29 19:39:30 +00:00
this.setState({
data: result
});
});
}
}
export default withRouter(SearchHandling);