Skip to content

Commit

Permalink
Fix navigation after searching (loic-sharma#186)
Browse files Browse the repository at this point in the history
* Fix navigation after the user has inputted search content
* Fixed some issues where `setState` was called after the component was unmounted
* Cleaned up routing

This was reported and originally fixed by @vitaliy-orlov in loic-sharma#183.
  • Loading branch information
loic-sharma authored Dec 30, 2018
1 parent abdf20a commit 03262b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
38 changes: 21 additions & 17 deletions src/BaGet.UI/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import * as React from 'react';
import { BrowserRouter as Router, NavLink, Route, RouteComponentProps } from 'react-router-dom';
import './App.css';
import DisplayPackage from './DisplayPackage/DisplayPackage';

import { Action, Location } from 'history';
import { withRouter } from 'react-router';
import { NavLink, Route, RouteComponentProps } from 'react-router-dom';

import SearchResults from './SearchResults';
import Upload from './Upload';

import './App.css';

interface IAppState {
input: string;
selected?: string;
}

class App extends React.Component<{}, IAppState> {
class App extends React.Component<RouteComponentProps, IAppState> {

constructor(props: {}) {
constructor(props: RouteComponentProps) {
super(props);

this.state = { input: "" };

this.props.history.listen(this.onRouteChange);
}

public render() {
return (
<Router>
<div>
{this._renderNavigationBar()}
<div>
{this._renderNavigationBar()}

{this._renderContent()}
</div>
</Router>
{this._renderContent()}
</div>
);
}

private onRouteChange = (location: Location, action: Action) =>
this.setState({ input: "" });

private _renderNavigationBar() {
return (
<nav className="navbar navbar-inverse" role="navigation">
Expand Down Expand Up @@ -65,9 +70,8 @@ class App extends React.Component<{}, IAppState> {
return (
<section role="main" className="container main-container">
<Route exact={true} path="/" render={this.renderSearch} />
<Route path="/packages/:id" component={DisplayPackage} />

<Route path="/upload" component={Upload} />
{this.props.children}
</section>
);
}
Expand All @@ -86,7 +90,7 @@ class App extends React.Component<{}, IAppState> {
);

private handleChange = (input: React.ChangeEvent<HTMLInputElement>) =>
this.setState({ input: input.target.value, selected: undefined });
this.setState({ input: input.target.value });
}

export default App;
export default withRouter(App);
17 changes: 14 additions & 3 deletions src/BaGet.UI/src/DisplayPackage/DisplayPackage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,30 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
private parser: Parser;
private htmlRenderer: HtmlRenderer;

private registrationController: AbortController;
private readmeController: AbortController;

constructor(props: IDisplayPackageProps) {
super(props);

this.parser = new Parser();
this.htmlRenderer = new HtmlRenderer();
this.registrationController = new AbortController();
this.readmeController = new AbortController();

this.id = props.match.params.id;
this.state = {package: undefined};
}

public componentWillUnmount() {
this.registrationController.abort();
this.readmeController.abort();
}

public componentDidMount() {
const url = `/v3/registration/${this.id}/index.json`;

fetch(url).then(response => {
fetch(url, {signal: this.registrationController.signal}).then(response => {
return response.json();
}).then(json => {
const results = json as Registration.IRegistrationIndex;
Expand Down Expand Up @@ -118,7 +128,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
if (latestItem.catalogEntry.hasReadme) {
const readmeUrl = `/v3/package/${this.id}/${latestVersion}/readme`;

fetch(readmeUrl).then(response => {
fetch(readmeUrl, {signal: this.readmeController.signal}).then(response => {
return response.text();
}).then(result => {
this.setState(prevState => {
Expand All @@ -132,7 +142,8 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
});
}
}
});
// tslint:disable-next-line:no-console
}).catch((e) => console.log("Failed to load package.", e));
}

public render() {
Expand Down
12 changes: 11 additions & 1 deletion src/BaGet.UI/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { initializeIcons } from '@uifabric/icons';
import 'bootstrap/dist/css/bootstrap.css';
import { BrowserRouter as Router, Route } from 'react-router-dom';

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';

import DisplayPackage from './DisplayPackage/DisplayPackage';
import Upload from './Upload';

import './index.css';

// import registerServiceWorker from './registerServiceWorker';

initializeIcons();

ReactDOM.render(
<App />,
<Router>
<App>
<Route path="/packages/:id" component={DisplayPackage} />

<Route path="/upload" component={Upload} />
</App>
</Router>,
document.getElementById('root') as HTMLElement
);

Expand Down

0 comments on commit 03262b4

Please sign in to comment.