Skip to content

Commit 9f3fa5d

Browse files
committed
Fix error msg crashing the component
1 parent b00e637 commit 9f3fa5d

3 files changed

Lines changed: 24 additions & 30 deletions

File tree

src/components/diff-container.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class DiffContainer extends React.Component {
5454
const { timestampA, timestampB, showDiff, error } = this.state;
5555

5656
if (error) {
57-
return (<ErrorMessage url={url} timestamp={timestampA} code={error}/>);
57+
return (<ErrorMessage url={url} timestamp={timestampA} code={error} />);
5858
}
5959
if (!timestampA && !timestampB) {
6060
return (

src/components/diff-view.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,15 @@ export default class DiffView extends React.Component {
208208
url.searchParams.append('b', b);
209209
fetchWithTimeout(url, { credentials: 'include' })
210210
.then(checkResponse)
211-
.then(response => response.json())
212-
.then((data) => {
213-
this.setState({
214-
diffData: data
215-
});
211+
.then((response) => {
212+
if (response.status == 200) {
213+
const data = response.json();
214+
this.setState({
215+
diffData: data
216+
});
217+
} else {
218+
this._errorHandled(error.status);
219+
}
216220
})
217221
.catch(error => { this._errorHandled(error.message); });
218222
}

src/components/errors.jsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
import PropTypes from 'prop-types';
2-
import React, { useCallback } from 'react';
3-
import { checkResponse, fetchWithTimeout } from '../js/utils';
2+
import React from 'react';
43

54
/**
65
* Display an error message depending on props
76
*/
8-
const ErrorMessage = ({ code, url, timestamp, conf, errorHandledCallback }) => {
9-
7+
const ErrorMessage = ({ code, url, timestamp }) => {
108
let msg = 'We are sorry but there is a problem comparing these captures. Please try two different ones.';
119
let simhash = false;
1210
let year = timestamp ? timestamp.substring(0, 4) : '';
1311

14-
const calculateSimhash = useCallback(() => {
15-
const targetUrl = `${conf.waybackDiscoverDiff}/calculate-simhash?url=${encodeURIComponent(url)}&year=${timestamp.substring(0, 4)}`;
16-
fetchWithTimeout(targetUrl).then(checkResponse)
17-
.then(() => { setTimeout(function () { window.location.reload(true); }, 10000); })
18-
.catch(error => { errorHandledCallback(error.message); });
19-
}, [conf, url, timestamp]);
20-
2112
switch (code) {
2213
case '404':
2314
msg = `The Wayback Machine has not archived ${url}.`;
24-
simhash = false;
2515
break;
2616
case '422':
2717
// https://github.com/edgi-govdata-archiving/web-monitoring-diff/blob/be748a7f0bbdd4251f680e22d3e433d1be93f858/web_monitoring_diff/server/server.py#L568
2818
msg = `The captures of ${url} cannot be compared because we support only HTML comparison.`;
29-
simhash = false;
3019
break;
3120
case 'CAPTURE_NOT_FOUND':
3221
msg = `There are no data available for ${url} at ${timestamp}.`;
@@ -38,39 +27,40 @@ const ErrorMessage = ({ code, url, timestamp, conf, errorHandledCallback }) => {
3827
break;
3928
case 'NO_CAPTURES':
4029
msg = `The Wayback Machine has not archived ${url} for year ${year}.`;
41-
simhash = false;
4230
break;
4331
case 'NO_DIFFERENT_CAPTURES':
4432
msg = `There aren't any different captures for ${url} for year ${year} to display their similarity.`;
45-
simhash = false;
4633
break;
4734
// Occurs when AJAX fetch for CDX is canceled.
4835
case '_this4.errorHandled is not a function': // Chrome
4936
case 'NetworkError when attempting to fetch resource.': // FF
5037
case 'Load failed': // Safari
5138
msg = `The capture of ${url} at ${timestamp} cannot be used for comparisons. Its possible it is a redirect.`;
52-
simhash = false;
5339
break;
5440
}
5541

42+
const changesUrl = `/web/changes/${url}`;
43+
5644
return (
5745
<>
5846
<div className='alert alert-warning' role='alert'>{ msg }</div>
59-
{ simhash &&
60-
<button className="btn btn-sm" id="calcButton"
61-
onClick={ calculateSimhash }>Calculate now</button>
62-
}
63-
<button className="btn btn-sm" onClick={() => window.history.back()}>&laquo; Go back</button>
47+
<div className="btn-group">
48+
<button className="btn btn-sm" onClick={() => window.history.back()}>&laquo; Go back</button>
49+
{ simhash &&
50+
<>
51+
<span>&nbsp;</span>
52+
<a href={changesUrl} className="btn btn-default btn-sm" id="calcButton">Visit Wayback Changes to calculate now</a>
53+
</>
54+
}
55+
</div>
6456
</>
6557
);
6658
};
6759

6860
ErrorMessage.propTypes = {
6961
code: PropTypes.string,
7062
url: PropTypes.string,
71-
timestamp: PropTypes.string,
72-
conf: PropTypes.object,
73-
errorHandledCallback: PropTypes.func
63+
timestamp: PropTypes.string
7464
};
7565

7666
export default ErrorMessage;

0 commit comments

Comments
 (0)