Started code review + some fixes (unfinished)
[vchess.git] / client / src / views / News.vue
CommitLineData
604b951e
BA
1<template lang="pug">
2main
3 input#modalNews.modal(type="checkbox")
4 div#newnewsDiv(role="dialog" data-checkbox="modalNews")
5 .card
6 label.modal-close(for="modalNews")
7 textarea#newsContent(
8 v-model="curnews.content"
9 :placeholder="st.tr['News go here']"
10 @input="adjustHeight"
11 )
12 button(@click="sendNews()") {{ st.tr["Send"] }}
13 #dialog.text-center {{ st.tr[infoMsg] }}
14 .row
15 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
0c76fa56 16 button#writeNews(
604b951e
BA
17 v-if="devs.includes(st.user.id)"
18 @click="showModalNews"
19 )
20 | {{ st.tr["Write news"] }}
6808d7a1 21 .news(v-for="n,idx in newsList" :class="{margintop:idx>0}")
bd76b456 22 span.ndt {{ formatDatetime(n.added) }}
604b951e
BA
23 div(v-if="devs.includes(st.user.id)")
24 button(@click="editNews(n)") {{ st.tr["Edit"] }}
25 button(@click="deleteNews(n)") {{ st.tr["Delete"] }}
bd76b456 26 p(v-html="parseHtml(n.content)")
604b951e
BA
27 button(v-if="hasMore" @click="loadMore()")
28 | {{ st.tr["Load more"] }}
29</template>
30
31<script>
32import { store } from "@/store";
33import { ajax } from "@/utils/ajax";
34import { getDate, getTime } from "@/utils/datetime";
35import { processModalClick } from "@/utils/modalClick";
36export default {
37 name: "my-news",
38 data: function() {
39 return {
40 devs: [1], //for now the only dev is me
41 st: store.state,
42 cursor: 0, //ID of last showed news
43 hasMore: true, //a priori there could be more news to load
6808d7a1 44 curnews: { id: 0, content: "" },
604b951e 45 newsList: [],
6808d7a1 46 infoMsg: ""
604b951e
BA
47 };
48 },
49 created: function() {
6808d7a1
BA
50 ajax("/news", "GET", { cursor: this.cursor }, res => {
51 this.newsList = res.newsList.sort((n1, n2) => n1.added - n2.added);
604b951e 52 const L = res.newsList.length;
6808d7a1 53 if (L > 0) this.cursor = this.newsList[0].id;
604b951e
BA
54 });
55 },
56 mounted: function() {
6808d7a1
BA
57 document
58 .getElementById("newnewsDiv")
59 .addEventListener("click", processModalClick);
604b951e
BA
60 },
61 methods: {
62 formatDatetime: function(dt) {
63 const dtObj = new Date(dt);
bd76b456
BA
64 const timePart = getTime(dtObj);
65 // Show minutes but not seconds:
6808d7a1
BA
66 return (
67 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
68 );
604b951e
BA
69 },
70 parseHtml: function(txt) {
71 return !txt.match(/<[/a-zA-Z]+>/)
72 ? txt.replace(/\n/g, "<br/>") //no HTML tag
73 : txt;
74 },
75 adjustHeight: function() {
76 const newsContent = document.getElementById("newsContent");
77 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
78 newsContent.style.height = "1px";
6808d7a1 79 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
604b951e
BA
80 },
81 resetCurnews: function() {
82 this.curnews.id = 0;
83 this.curnews.content = "";
84 // No need for added and uid fields: never updated
85 },
86 showModalNews: function() {
87 this.resetCurnews();
6808d7a1 88 window.doClick("modalNews");
604b951e
BA
89 },
90 sendNews: function() {
91 const edit = this.curnews.id > 0;
92 this.infoMsg = "Processing... Please wait";
6808d7a1
BA
93 ajax("/news", edit ? "PUT" : "POST", { news: this.curnews }, res => {
94 if (edit) {
95 let n = this.newsList.find(n => n.id == this.curnews.id);
96 if (n) n.content = this.curnews.content;
97 } else {
98 const newNews = {
99 content: this.curnews.content,
100 added: Date.now(),
101 uid: this.st.user.id,
102 id: res.id
103 };
104 this.newsList = [newNews].concat(this.newsList);
604b951e 105 }
6808d7a1
BA
106 document.getElementById("modalNews").checked = false;
107 this.infoMsg = "";
108 this.resetCurnews();
109 });
604b951e
BA
110 },
111 editNews: function(n) {
112 this.curnews.content = n.content;
113 this.curnews.id = n.id;
114 // No need for added and uid fields: never updated
6808d7a1 115 window.doClick("modalNews");
604b951e
BA
116 },
117 deleteNews: function(n) {
6808d7a1 118 if (confirm(this.st.tr["Are you sure?"])) {
604b951e 119 this.infoMsg = "Processing... Please wait";
6808d7a1 120 ajax("/news", "DELETE", { id: n.id }, () => {
604b951e
BA
121 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
122 this.newsList.splice(nIdx, 1);
123 this.infoMsg = "";
124 document.getElementById("modalNews").checked = false;
125 });
126 }
127 },
128 loadMore: function() {
6808d7a1
BA
129 ajax("/news", "GET", { cursor: this.cursor }, res => {
130 if (res.newsList.length > 0) {
604b951e
BA
131 this.newsList = this.newsList.concat(res.newsList);
132 const L = res.newsList.length;
6808d7a1
BA
133 if (L > 0) this.cursor = res.newsList[L - 1].id;
134 } else this.hasMore = false;
604b951e 135 });
6808d7a1
BA
136 }
137 }
604b951e
BA
138};
139</script>
140
141<style lang="sass" scoped>
142[type="checkbox"].modal+div .card
143 max-width: 767px
144 max-height: 100%
145textarea#newsContent
bd76b456 146 margin: 0
604b951e
BA
147 width: 100%
148 min-height: 200px
149 max-height: 100%
150#dialog
151 padding: 5px
152 color: blue
0c76fa56
BA
153button#writeNews
154 margin-top: 0
155 margin-bottom: 0
bd76b456
BA
156span.ndt
157 color: darkblue
feae89d3 158 padding: 0 5px 0 var(--universal-margin)
bd76b456
BA
159.margintop
160 margin-top: 25px
bd76b456 161 border-top: 1px solid grey
feae89d3
BA
162.news
163 padding-top: 10px
bd76b456
BA
164 & > div
165 display: inline-block
166@media screen and (max-width: 767px)
167 .margintop
168 margin-top: 10px
604b951e 169</style>