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

View File

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