torrents.csv/server/ui/src/components/navbar.tsx

82 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Component, linkEvent } from 'inferno';
import { SearchParams } from '../interfaces';
2018-11-29 04:57:35 +00:00
import { repoUrl } from '../utils';
interface State {
searchParams: SearchParams;
}
export class Navbar extends Component<any, State> {
state: State = {
searchParams: {
page: 1,
q: ""
}
}
constructor(props, context) {
super(props, context);
this.fillSearchField();
}
render() {
return (
<div class="sticky-top">{this.navbar()}</div>
)
}
navbar() {
return (
<nav class="navbar navbar-dark navbar-purple p-2 p-sm-1 shadow">
2018-11-30 18:41:16 +00:00
<a class="navbar-brand mx-1" href="#">
<i class="fas fa-fw fa-database mr-2"></i>
2018-11-29 04:57:35 +00:00
Torrents.csv
</a>
<div class="navbar-nav ml-auto mr-2">
<a class="nav-item nav-link" href={repoUrl}><i class="fab fa-fw fa-github"></i></a>
</div>
2018-11-30 18:41:16 +00:00
{this.searchForm()}
</nav>
);
}
searchForm() {
return (
<form class="form-inline col-12 col-sm-8 col-md-9 col-xl-10 mx-0 mx-sm-1 my-1 my-sm-0 p-0" onSubmit={linkEvent(this, this.search)}>
2018-11-30 18:41:16 +00:00
<div class="input-group w-100">
<input class="form-control border-0 no-outline" type="search" placeholder="Search..." aria-label="Search..." required
2018-11-30 18:41:16 +00:00
value={this.state.searchParams.q}
onInput={linkEvent(this, this.searchChange)}></input>
<button class="btn btn-secondary border-0 rounded-right no-outline" type="submit">
<i className="fas fa-fw fa-search"></i>
</button>
</div>
</form>
);
}
search(i: Navbar, event) {
event.preventDefault();
i.context.router.history.push(`/search/${i.state.searchParams.q}/${i.state.searchParams.page}`);
}
searchChange(i: Navbar, event) {
let searchParams: SearchParams = {
q: event.target.value,
page: 1
}
i.setState({ searchParams: searchParams });
}
fillSearchField() {
let splitPath: Array<string> = this.context.router.route.location.pathname.split("/");
if (splitPath.length == 4 && splitPath[1] == 'search')
this.state.searchParams = {
page: Number(splitPath[3]),
q: splitPath[2]
};
}
2018-11-29 04:36:12 +00:00
}