X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FNews.vue;h=0e3c3c7ae08d18811849d5ee6d0898d25ebd902e;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hp=ad0561a63aa1a147d6bae7188c6a99b8429e9bad;hpb=910d631b73cad5ffef1b4461157b704e7e7057d8;p=vchess.git diff --git a/client/src/views/News.vue b/client/src/views/News.vue index ad0561a6..0e3c3c7a 100644 --- a/client/src/views/News.vue +++ b/client/src/views/News.vue @@ -5,7 +5,7 @@ main role="dialog" data-checkbox="modalNews" ) - .card + .card#writeNews label.modal-close(for="modalNews") textarea#newsContent( v-model="curnews.content" @@ -16,7 +16,7 @@ main #dialog.text-center {{ st.tr[infoMsg] }} .row .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 - button#writeNews( + button#writeNewsBtn( v-if="devs.includes(st.user.id)" @click="showModalNews" ) @@ -30,7 +30,7 @@ main button(@click="editNews(n)") {{ st.tr["Edit"] }} button(@click="deleteNews(n)") {{ st.tr["Delete"] }} p(v-html="parseHtml(n.content)") - button( + button#loadMoreBtn( v-if="hasMore" @click="loadMore()" ) @@ -48,21 +48,35 @@ export default { return { devs: [1], //for now the only dev is me st: store.state, - cursor: 0, //ID of last showed news - hasMore: true, //a priori there could be more news to load + // timestamp of oldest showed news: + cursor: Number.MAX_SAFE_INTEGER, + // hasMore == TRUE: a priori there could be more news to load + hasMore: true, curnews: { id: 0, content: "" }, newsList: [], infoMsg: "" }; }, created: function() { - ajax("/news", "GET", { cursor: this.cursor }, res => { - this.newsList = res.newsList.sort((n1, n2) => n1.added - n2.added); - const L = res.newsList.length; - if (L > 0) this.cursor = this.newsList[0].id; - }); + ajax( + "/news", + "GET", + { + data: { cursor: this.cursor }, + success: (res) => { + // The returned list is sorted from most recent to oldest + this.newsList = res.newsList; + const L = res.newsList.length; + if (L > 0) this.cursor = res.newsList[L - 1].added; + } + } + ); }, mounted: function() { + // Mark that I've read the news: + localStorage.setItem("newsRead", Date.now()); + if (this.st.user.id > 0) ajax("/newsread", "PUT"); + document.getElementById("newsMenu").classList.remove("somenews"); document .getElementById("newnewsDiv") .addEventListener("click", processModalClick); @@ -99,23 +113,30 @@ export default { sendNews: function() { const edit = this.curnews.id > 0; this.infoMsg = "Processing... Please wait"; - ajax("/news", edit ? "PUT" : "POST", { news: this.curnews }, res => { - if (edit) { - let n = this.newsList.find(n => n.id == this.curnews.id); - if (n) n.content = this.curnews.content; - } else { - const newNews = { - content: this.curnews.content, - added: Date.now(), - uid: this.st.user.id, - id: res.id - }; - this.newsList = [newNews].concat(this.newsList); + ajax( + "/news", + edit ? "PUT" : "POST", + { + data: { news: this.curnews }, + success: (res) => { + if (edit) { + let n = this.newsList.find(n => n.id == this.curnews.id); + if (n) n.content = this.curnews.content; + } else { + const newNews = { + content: this.curnews.content, + added: Date.now(), + uid: this.st.user.id, + id: res.id + }; + this.newsList = [newNews].concat(this.newsList); + } + document.getElementById("modalNews").checked = false; + this.infoMsg = ""; + this.resetCurnews(); + } } - document.getElementById("modalNews").checked = false; - this.infoMsg = ""; - this.resetCurnews(); - }); + ); }, editNews: function(n) { this.curnews.content = n.content; @@ -126,22 +147,36 @@ export default { deleteNews: function(n) { if (confirm(this.st.tr["Are you sure?"])) { this.infoMsg = "Processing... Please wait"; - ajax("/news", "DELETE", { id: n.id }, () => { - const nIdx = this.newsList.findIndex(nw => nw.id == n.id); - this.newsList.splice(nIdx, 1); - this.infoMsg = ""; - document.getElementById("modalNews").checked = false; - }); + ajax( + "/news", + "DELETE", + { + data: { id: n.id }, + success: () => { + const nIdx = this.newsList.findIndex(nw => nw.id == n.id); + this.newsList.splice(nIdx, 1); + this.infoMsg = ""; + document.getElementById("modalNews").checked = false; + } + } + ); } }, loadMore: function() { - ajax("/news", "GET", { cursor: this.cursor }, res => { - if (res.newsList.length > 0) { - this.newsList = this.newsList.concat(res.newsList); - const L = res.newsList.length; - if (L > 0) this.cursor = res.newsList[L - 1].id; - } else this.hasMore = false; - }); + ajax( + "/news", + "GET", + { + data: { cursor: this.cursor }, + success: (res) => { + if (res.newsList.length > 0) { + this.newsList = this.newsList.concat(res.newsList); + const L = res.newsList.length; + if (L > 0) this.cursor = res.newsList[L - 1].added; + } else this.hasMore = false; + } + } + ); } } }; @@ -162,7 +197,10 @@ textarea#newsContent padding: 5px color: blue -button#writeNews +#writeNews + padding-top: 50px + +button#writeNewsBtn, button#loadMoreBtn margin-top: 0 margin-bottom: 0