Very basic anti-bot strategy for problems upload: do not pre-generate FEN
[vchess.git] / public / javascripts / components / problems.js
1 Vue.component('my-problems', {
2 data: function () {
3 return {
4 problems: problemArray, //initial value
5 newProblem: {
6 fen: "",
7 instructions: "",
8 solution: "",
9 stage: "nothing", //or "preview" after new problem is filled
10 },
11 };
12 },
13 template: `
14 <div>
15 <button @click="fetchProblems('backward')">Previous</button>
16 <button @click="fetchProblems('forward')">Next</button>
17 <button @click="showNewproblemModal">New</button>
18 <my-problem-summary v-on:show-problem="bubbleUp(p)"
19 v-for="(p,idx) in sortedProblems"
20 v-bind:prob="p" v-bind:preview="false" v-bind:key="idx">
21 </my-problem-summary>
22 <input type="checkbox" id="modal-newproblem" class="modal">
23 <div role="dialog" aria-labelledby="newProblemTxt">
24 <div v-show="newProblem.stage=='nothing'" class="card newproblem-form">
25 <label for="modal-newproblem" class="modal-close"></label>
26 <h3 id="newProblemTxt">Add problem</h3>
27 <form @submit.prevent="previewNewProblem">
28 <fieldset>
29 <label for="newpbFen">Fen</label>
30 <input id="newpbFen" type="text" v-model="newProblem.fen"
31 placeholder="Full FEN string"/>
32 </fieldset>
33 <fieldset>
34 <p class="emphasis">Safe HTML tags allowed</p>
35 <label for="newpbInstructions">Instructions</label>
36 <textarea id="newpbInstructions" v-model="newProblem.instructions"
37 placeholder="Explain the problem here"></textarea>
38 <label for="newpbSolution">Solution</label>
39 <textarea id="newpbSolution" v-model="newProblem.solution"
40 placeholder="How to solve the problem?"></textarea>
41 <button class="center-btn">Preview</button>
42 </fieldset>
43 </form>
44 </div>
45 <div v-show="newProblem.stage=='preview'" class="card newproblem-preview">
46 <label for="modal-newproblem" class="modal-close"></label>
47 <my-problem-summary v-bind:prob="newProblem" v-bind:preview="true">
48 </my-problem-summary>
49 <div class="col-sm-12 col-md-6 col-lg-3 col-lg-offset-3 topspace">
50 <button @click="sendNewProblem()">Send</button>
51 <button @click="newProblem.stage='nothing'">Cancel</button>
52 </div>
53 </div>
54 </div>
55 </div>
56 `,
57 computed: {
58 sortedProblems: function() {
59 // Newest problem first
60 return this.problems.sort((p1,p2) => { return p2.added - p1.added; });
61 },
62 mailErrProblem: function() {
63 return "mailto:contact@vchess.club?subject=[" + variant + " problems] error";
64 },
65 },
66 methods: {
67 // Propagate "show problem" event to parent component (my-variant)
68 bubbleUp: function(problem) {
69 this.$emit('show-problem', JSON.stringify(problem));
70 },
71 fetchProblems: function(direction) {
72 return; //TODO: re-activate after server side is implemented (see routes/all.js)
73 if (this.problems.length == 0)
74 return; //what could we do?!
75 // Search for newest date (or oldest)
76 let last_dt = this.problems[0].added;
77 for (let i=0; i<this.problems.length; i++)
78 {
79 if ((direction == "forward" && this.problems[i].added > last_dt) ||
80 (direction == "backward" && this.problems[i].added < last_dt))
81 {
82 last_dt = this.problems[i].added;
83 }
84 }
85 ajax("/problems/" + variant, "GET", {
86 direction: direction,
87 last_dt: last_dt,
88 }, response => {
89 if (response.problems.length > 0)
90 this.problems = response.problems;
91 });
92 },
93 showNewproblemModal: function() {
94 document.getElementById("modal-newproblem").checked = true;
95 },
96 previewNewProblem: function() {
97 if (!V.IsGoodFen(this.newProblem.fen))
98 return alert("Bad FEN string");
99 this.newProblem.stage = "preview";
100 },
101 sendNewProblem: function() {
102 // Send it to the server and close modal
103 ajax("/problems/" + variant, "POST", {
104 fen: this.newProblem.fen,
105 instructions: this.newProblem.instructions,
106 solution: this.newProblem.solution,
107 }, response => {
108 this.newProblem.added = Date.now();
109 this.problems.push(JSON.parse(JSON.stringify(this.newProblem)));
110 document.getElementById("modal-newproblem").checked = false;
111 this.newProblem.stage = "nothing";
112 });
113 },
114 },
115 })