Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #504 from atom/mkt-handle-gql-edge-cases
Browse files Browse the repository at this point in the history
Handle some GraphQL API edge cases
  • Loading branch information
Michelle Tilley authored Feb 6, 2017
2 parents 739f1da + 86e40c0 commit b534809
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/containers/pr-list-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PrList extends React.Component {

render() {
const repo = this.props.query.repository;
if (!repo.pullRequests.edges.length) {
if (!repo || !repo.pullRequests.edges.length) {
return null; // TODO: no PRs
}
const pr = repo.pullRequests.edges[0].node;
Expand Down
42 changes: 32 additions & 10 deletions lib/controllers/pr-info-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import Relay from 'react-relay';
import {RemotePropType} from '../prop-types';
import RelayRootContainer from '../containers/relay-root-container';
import PrListContainer from '../containers/pr-list-container';
import GithubLoginView from '../views/github-login-view';

const environmentByGithubInstance = new Map();
const relayConfigByGithubInstance = new Map();

const getEnvironmentForInstance = (instance, endpoint, token) => {
let environment = environmentByGithubInstance.get(instance);
const config = relayConfigByGithubInstance.get(instance) || {};
let {environment, networkLayer} = config;
if (!environment) {
environment = new Relay.Environment();
environment.injectNetworkLayer(
new Relay.DefaultNetworkLayer(`${endpoint}/graphql`, {
headers: {
Authorization: `bearer ${token}`,
},
}),
);
environmentByGithubInstance.set(instance, environment);
networkLayer = new Relay.DefaultNetworkLayer(`${endpoint}/graphql`, {
headers: {
Authorization: `bearer ${token}`,
},
});
environment.injectNetworkLayer(networkLayer);
relayConfigByGithubInstance.set(instance, {environment, networkLayer});
} else {
networkLayer._init.headers = {
Authorization: `bearer ${token}`,
};
}
return environment;
};
Expand Down Expand Up @@ -49,6 +54,8 @@ export default class PrInfoController extends React.Component {
token: React.PropTypes.string.isRequired,
instance: React.PropTypes.string.isRequired,
currentBranch: React.PropTypes.string.isRequired,
loginModel: React.PropTypes.object.isRequired,
onLogin: React.PropTypes.func.isRequired,
remote: RemotePropType.isRequired,
}

Expand All @@ -68,6 +75,21 @@ export default class PrInfoController extends React.Component {
Component={PrListContainer}
route={route}
environment={environment}
renderLoading={() => {
return <div>Loading...</div>;
}}
renderFailure={(err, retry) => {
if (err.response && err.response.status === 401) {
return (
<div>
The API endpoint returned a unauthorized error. Please try to re-authenticate with the endpoint.
<GithubLoginView onLogin={this.props.onLogin} />
</div>
);
} else {
return <div>An unknown error occurred.</div>;
}
}}
/>
);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/controllers/remote-pr-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export default class RemotePrController extends React.Component {
}

render() {
const {instance, endpoint, remote, currentBranch, token} = this.props;
const {instance, endpoint, remote, currentBranch, token, loginModel} = this.props;
return (
<div className="github-RemotePrController">
{token && <PrInfoController {...{instance, endpoint, remote, currentBranch, token}} />}
{token && <PrInfoController
{...{instance, endpoint, remote, currentBranch, token, loginModel}}
onLogin={this.handleLogin}
/>
}
{!token && <GithubLoginView onLogin={this.handleLogin} />}
</div>
);
Expand Down

0 comments on commit b534809

Please sign in to comment.