Fix news behavior on 304 and depletion

This commit is contained in:
Ilya Ig. Petrov 2017-06-08 21:12:07 +05:00
parent 532b0b969f
commit fd978c5e94

View File

@ -57,6 +57,7 @@ export default function getApp(theState) {
const uiComDate = 'ui-last-comment-date'; const uiComDate = 'ui-last-comment-date';
const uiComEtag = 'ui-last-comments-etag'; const uiComEtag = 'ui-last-comments-etag';
const uiLastNewsArr = 'ui-last-news-arr';
const statusFromHash = this.state.hashParams.get('status'); const statusFromHash = this.state.hashParams.get('status');
if (statusFromHash) { if (statusFromHash) {
@ -78,13 +79,14 @@ export default function getApp(theState) {
headers: new Headers(headers), headers: new Headers(headers),
}; };
const ghUrl = `https://api.github.com/repos/anticensorship-russia/for-testing-github-api/issues/1/comments${query}`; const ghUrl = `https://api.github.com/repos/anticensorship-russia/for-testing/issues/1/comments${query}`;
//const ghUrl = `http://httpstat.us/418`;
const [error, comments, etag] = await fetch( const [error, comments, etag] = await fetch(
ghUrl, ghUrl,
params params
).then( ).then(
(res) => !( res.status >= 200 && res.status < 300 || res.status === 304 ) (res) => !( res.status >= 200 && res.status < 300 || res.status === 304 )
? Promise.reject(new Error(`Получен ответ с неудачным кодом ${res.status}.`)) ? Promise.reject({message: `Получен ответ с неудачным кодом ${res.status}.`, data: res})
: res : res
).then( ).then(
(res) => Promise.all([ (res) => Promise.all([
@ -94,8 +96,14 @@ export default function getApp(theState) {
]), ]),
(err) => { (err) => {
const statusCode = err.data && err.data.status;
const ifCritical = null; const ifCritical = null;
this.showErrors(ifCritical, {message: 'Не удалось достать новости: что-то не так с сетью.', wrapped: err}); this.showErrors(ifCritical, {
message: statusCode === 403
? 'Слишком много запросов :-( Сообщите разработчику, как вам удалось всё истратить.'
: 'Не удалось достать новости: что-то не так с сетью.',
wrapped: err,
});
return [err, false, false]; return [err, false, false];
} }
@ -108,16 +116,21 @@ export default function getApp(theState) {
return; // Let the user see the error message and contemplate. return; // Let the user see the error message and contemplate.
} }
const ifNews = (() => { const ifNewsWasSet = (() => {
if (!(comments && comments.length)) { if (comments === false) {
// Don't show stale news here. // 304
const json = localStorage[uiLastNewsArr];
const news = json && JSON.parse(json);
if (news && news.length) {
this.setNewsStatusTo(news);
return true;
}
return false; return false;
} }
let minDate; let minDate; // Minimal date among all news-comments.
const news = []; const news = comments.reduce((acc, comment) => {
comments.forEach((comment) => {
const curDate = comment.updated_at || comment.created_at; const curDate = comment.updated_at || comment.created_at;
const newsTitle = this.getNewsHeadline( comment.body ); const newsTitle = this.getNewsHeadline( comment.body );
@ -125,19 +138,24 @@ export default function getApp(theState) {
if (!minDate || curDate <= minDate) { if (!minDate || curDate <= minDate) {
minDate = curDate; minDate = curDate;
} }
news.push([newsTitle, comment.html_url]); acc.push([newsTitle, comment.html_url]);
} }
return acc;
}); }, []);
if (!news.length) { // Response with empty news is cached too.
return false; localStorage[uiLastNewsArr] = JSON.stringify(news);
} if (news.length) {
if (minDate) {
localStorage[uiComDate] = minDate; localStorage[uiComDate] = minDate;
}
this.setNewsStatusTo(news); this.setNewsStatusTo(news);
return true; return true;
}
return false;
})(); })();
if (!ifNews) { if (!ifNewsWasSet) {
this.setStatusTo('Хорошего настроения Вам!'); this.setStatusTo('Хорошего настроения Вам!');
} }