Skip to content
Permalink
Browse files

display a fallback post if disconnected or failed to get posts json f…

…rom website
  • Loading branch information...
illwieckz committed Aug 19, 2019
1 parent 084fd6f commit 6223758a2ed347cbfbce2e4d0728eb804a6b3b9d
Showing with 29 additions and 1 deletion.
  1. +27 −1 News.qml
  2. +1 −0 qml.qrc
  3. +1 −0 resources/disconnected_posts.json
@@ -19,31 +19,57 @@ Item {
var news = new XMLHttpRequest();
news.onreadystatechange = function() {
if (news.readyState === XMLHttpRequest.DONE) {
var newsObj = JSON.parse(news.responseText);
var newsObj = null;

if (news.status == 200) {
try {
newsObj = JSON.parse(news.responseText);
} catch (error) {
console.log("failed to parse posts json")
}
}
else {
console.log("failed to fetch posts json")
}

if (newsObj === null) {
console.log("fetching fallback posts json")
news.open('GET', 'qrc:/resources/disconnected_posts.json')
news.send();
return;
}

var component = Qt.createComponent("qrc:/NewsCard.qml");
for (var i = 0; i < newsObj['posts'].length; ++i) {
var object = component.createObject(swipe);
var post = newsObj['posts'][i];
var found = false;

if (post['thumbnail_images']) {
if (post['thumbnail_images']['full']) {
object.fullThumbSrc = Qt.resolvedUrl(post['thumbnail_images']['full']['url']);
found = true;
}

if (post['thumbnail_images']['thumbnail']) {
object.smallThumbSrc = Qt.resolvedUrl(post['thumbnail_images']['thumbnail']['url']);
found = true;
}
}

if (!found) {
console.log("thumbnail not found, use fallback thumbnail")
object.fullThumbSrc = "qrc:/resources/unvanquished.png"
}

object.cardTitle = post['title_plain'];
object.summary = post['excerpt'];
object.url = post['url'];
}
}
}

console.log("fetching recent posts JSON")
news.open('GET', 'http://www.unvanquished.net/?json=get_recent_posts');
news.send();
}
@@ -17,5 +17,6 @@
<file>resources/logo.png</file>
<file>resources/tyrant.png</file>
<file>resources/unvanquished.desktop</file>
<file>resources/disconnected_posts.json</file>
</qresource>
</RCC>
@@ -0,0 +1 @@
{"status":"ok","count":1,"count_total":1,"pages":1,"posts":[{"id":0,"type":"post","slug":"so-you-want-to-be-a-hero","url":"https:\/\/unvanquished.net","status":"publish","title":"So you want to be a hero?","title_plain":"So you want to be a hero?","content":"Play the game and save your species from the invasion! Become a marine, handle your big weapon and dress that impressive exoskeleton to save the human race like a boss. Or if you prefer, be a monstrous beast and save those cute grangers from those two-legged invaders who only know how to turn beautiful organic equilibrium into lifeless machines and dead concreted worlds.","excerpt":"Play the game and save your species from the invasion! Become a marine, handle your big weapon and dress that impressive exoskeleton to save the human race like a boss. Or if you prefer, be a monstrous beast and save those cute grangers from those two-legged invaders who only know how to turn beautiful organic equilibrium into lifeless machines and dead concreted worlds &hellip; <a href=\"https:\/\/unvanquished.net\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unvanquished website<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","date":"2019-08-19 18:39:01","modified":"2019-08-19 18:39:01","categories":[{"id":3,"slug":"news","title":"News","description":"Daily development activity can be tracked there: <a href=\"\/activity\/\">Development Activity<\/a>.","parent":0,"post_count":1}],"tags":[],"author":{"id":9,"slug":"illwieckz","name":"illwieckz","first_name":"illwieckz","last_name":"","nickname":"illwieckz","url":"","description":""},"comments":[],"attachments":[{"id":2,"url":"qrc:\/resources\/unvanquished.png","slug":"unvanquished-png","title":"Unvanquished icon","description":"Unvanquished icon","caption":"Unvanquished icon","parent":1,"mime_type":"image\/png","images":{"full":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"thumbnail":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"medium":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"post-thumbnail":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256}}}],"comment_count":0,"comment_status":"open","thumbnail":"qrc:\/resources\/unvanquished.png","custom_fields":{},"thumbnail_size": "post-thumbnail","thumbnail_images":{"full":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"thumbnail":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"medium":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256},"post-thumbnail":{"url":"qrc:\/resources\/unvanquished.png","width":256,"height":256}}}]}

0 comments on commit 6223758

Please sign in to comment.
You can’t perform that action at this time.