Mend news reporting

This commit is contained in:
Ilya Ig. Petrov 2017-05-31 19:32:03 +05:00
parent dc1371063b
commit 6ed04d680a
2 changed files with 157 additions and 103 deletions

View File

@ -38,12 +38,21 @@ export default function getApp(theState) {
}
async componentDidMount() {
setNewsStatusTo(newsArr) {
this.setStatusTo(
<ol>
{newsArr.map(([title, url]) => (<li><a href={url}>{title}</a></li>))}
</ol>
);
}
async showNews() {
console.log('Did mount!');
const uiComDate = 'ui-last-comment-date';
const uiComEtag = 'ui-last-comments-etag';
const uiLastNews = 'ui-last-news';
const uiLastNewsArr = 'ui-last-news-arr';
const statusFromHash = this.state.hashParams.get('status');
if (statusFromHash) {
@ -65,8 +74,10 @@ export default function getApp(theState) {
headers: new Headers(headers),
};
const ghUrl = `https://api.github.com/repos/edge-ware/edge-ware.github.io/issues/1/comments${query}`;
console.log(ghUrl);
const [comments, etag] = await fetch(
`https://api.github.com/repos/edge-ware/edge-ware.github.io/issues/1/comments${query}`,
ghUrl,
params
).then(
(res) => Promise.all([
@ -75,32 +86,73 @@ export default function getApp(theState) {
]),
(err) => {
this.showErrors({message: 'Что-то не так с сетью. Не удалось достать новости.'});
this.showErrors({message: 'Не удалось достать новости: что-то не так с сетью.', wrapped: err});
return [false, false];
}
);
console.log('RESP', comments, etag);
if (etag) {
localStorage[uiComEtag] = etag;
}
const ifNews = (() => {
if (!(comments && comments.length)) {
const news = localStorage[uiLastNews];
const news = JSON.parse(localStorage[uiLastNewsArr]);
if (news) {
this.setStatusTo(news);
} else {
this.setStatusTo('У нас ничего нового.');
this.setNewsStatusTo(news);
return true;
}
return;
return false;
}
const lastComment = comments.pop();
if (lastComment) {
const lastDate = lastComment.updated_at || lastComment.created_at;
localStorage[uiComDate] = lastDate;
const newsText = lastComment.body.split(/\r?\n/)[0].replace(/^\s*#+\s*/g, '');
localStorage[uiLastNews] = newsText;
this.setStatusTo(newsText);
let minDate;
const news = [];
console.log('we have', comments);
comments.forEach((comment) => {
const curDate = comment.updated_at || comment.created_at;
const newsTitle = this.getNewsHeadline( comment.body );
if (newsTitle) {
if (!minDate || curDate <= minDate) {
minDate = curDate;
}
news.push([newsTitle, comment.html_url]);
}
});
if (!news.length) {
return false;
}
localStorage[uiComDate] = minDate;
console.log('New date!', minDate);
localStorage[uiLastNewsArr] = JSON.stringify(news);
this.setNewsStatusTo(news);
return true;
})();
if (!ifNews) {
this.setStatusTo('Ничего нового.');
}
}
componentDidMount() {
this.showNews();
}
getNewsHeadline(comBody) {
const headline = comBody.split(/\r?\n/)[0];
const ifOver = /#+\s*$/.test(headline);
if (ifOver) {
return false;
}
return headline.replace(/^\s*#+\s*/g, '');
}
@ -199,6 +251,7 @@ export default function getApp(theState) {
setStatusTo: this.setStatusTo.bind(this),
conduct: this.conduct.bind(this),
showErrors: this.showErrors.bind(this),
showNews: this.showNews.bind(this),
},
ifInputsDisabled: this.state.ifInputsDisabled,
hashParams: this.state.hashParams,

View File

@ -60,11 +60,12 @@ export default function getPacChooser(theState) {
chosenPacName: 'none',
};
this.updatePac = function updatePac() {
this.updatePac = function updatePac(onSuccess) {
props.funs.conduct(
'Обновляем...',
(cb) => props.apis.antiCensorRu.syncWithPacProviderAsync(cb),
'Обновлено.'
'Обновлено.',
onSuccess
);
};
@ -155,7 +156,7 @@ export default function getPacChooser(theState) {
componentDidMount() {
if (this.props.apis.antiCensorRu.ifFirstInstall) {
this.updatePac();
this.updatePac( this.props.funs.showNews );
}
}