Some fixes, add basic preview logic for new problem
[vchess.git] / public / javascripts / components / problems.js
index 594d6e6..3bd13f6 100644 (file)
 Vue.component('my-problems', {
        data: function () {
                return {
-                       problems: problemArray //initial value
+                       problems: problemArray, //initial value
+                       newProblem: {
+                               fen: V.GenRandInitFen(),
+                               instructions: "",
+                               solution: "",
+                               stage: "nothing", //or "preview" after new problem is filled
+                       },
                };
        },
        template: `
                <div>
-                       <button>Previous</button>
-                       <button>Next</button>
+                       <button @click="fetchProblems('backward')">Previous</button>
+                       <button @click="fetchProblems('forward')">Next</button>
                        <button @click="showNewproblemModal">New</button>
-                       <my-problem-summary
-                               v-for="(p,idx) in sortedProblems",
-                               v-bind:prob="p",
-                               v-bind:key="idx",
-                               @click="showProblem(p)"
+                       <my-problem-summary v-on:show-problem="bubbleUp(p)"
+                               v-for="(p,idx) in sortedProblems" v-bind:prob="p" v-bind:key="idx">
                        </my-problem-summary>
                        <input type="checkbox" id="modal-newproblem" class="modal">
                        <div role="dialog" aria-labelledby="newProblemTxt">
-                               <div class="card newproblem">
+                               <div v-show="newProblem.stage=='nothing'" class="card newproblem-form">
                                        <label for="modal-newproblem" class="modal-close"></label>
                                        <h3 id="newProblemTxt">Add problem</h3>
-                                       <form @submit.prevent="postNewProblem">
+                                       <form @submit.prevent="previewNewProblem">
                                                <fieldset>
                                                        <label for="newpbFen">Fen</label>
-                                                       <input type="text" id="newpbFen" placeholder="Position [+ flags [+ turn]]"/>
+                                                       <input id="newpbFen" type="text" v-model="newProblem.fen"/>
                                                </fieldset>
                                                <fieldset>
-                                                       <p class="emphasis">
-                                                               Allowed HTML tags:
-                                                               &lt;p&gt;,&lt;br&gt;,&lt,ul&gt;,&lt;ol&gt;,&lt;li&gt;
-                                                       </p>
+                                                       <p class="emphasis">Safe HTML tags allowed</p>
                                                        <label for="newpbInstructions">Instructions</label>
-                                                       <textarea id="newpbInstructions" placeholder="Explain the problem here"/>
+                                                       <textarea id="newpbInstructions" v-model="newProblem.instructions"
+                                                               placeholder="Explain the problem here"/>
                                                        <label for="newpbSolution">Solution</label>
-                                                       <textarea id="newpbSolution" placeholder="How to solve the problem?"/>
-                                                       <button class="center-btn">Send</button>
+                                                       <textarea id="newpbSolution" v-model="newProblem.solution"
+                                                               placeholder="How to solve the problem?"/>
+                                                       <button class="center-btn">Preview</button>
                                                </fieldset>
-                                               <p class="mistake-newproblem">
-                                                       Note: if you made a mistake, please let me know at
-                                                       <a :href="mailErrProblem">contact@vchess.club</a>
-                                               </p>
                                        </form>
                                </div>
+                               <div v-show="newProblem.stage=='preview'" class="card newproblem-preview">
+                                       // TODO: we don't want exactly the same display (-date +solution)
+                                       <my-problem-summary v-bind:prob="newProblem"></my-problem-summary>
+                                       <button @click="sendNewProblem()" class="center-btn">Send</button>
+                               </div>
                        </div>
                </div>
        `,
        computed: {
                sortedProblems: function() {
                        // Newest problem first
-                       return problems.sort((p1,p2) => { return p2.added - p1.added; });
+                       return this.problems.sort((p1,p2) => { return p2.added - p1.added; });
                },
                mailErrProblem: function() {
                        return "mailto:contact@vchess.club?subject=[" + variant + " problems] error";
                },
        },
        methods: {
-               fetchProblems: function(direction) {
-                       // TODO: ajax call return list of max 10 problems
-                       // Do not do anything if no older problems (and store this result in cache!)
-                       // TODO: ajax call return list of max 10 problems
-                       // Do not do anything if no newer problems
+               // Propagate "show problem" event to parent component (my-variant)
+               bubbleUp: function(problem) {
+                       this.$emit('show-problem', JSON.stringify(problem));
                },
-               showProblem: function(prob) {
-                       //TODO: send event with object prob.fen, prob.instructions, prob.solution
-                       //Event should propagate to game, which set mode=="problem" + other variables
-                       //click on a problem ==> land on variant page with mode==friend, FEN prefilled... ok
-                       // click on problem ==> masque problems, affiche game tab, launch new game Friend with
-                       //   FEN + turn + flags + rappel instructions / solution on click sous l'échiquier
+               fetchProblems: function(direction) {
+                       return; //TODO: re-activate after server side is implemented (see routes/all.js)
+                       if (this.problems.length == 0)
+                               return; //what could we do?!
+                       // Search for newest date (or oldest)
+                       let last_dt = this.problems[0].added;
+                       for (let i=0; i<this.problems.length; i++)
+                       {
+                               if ((direction == "forward" && this.problems[i].added > last_dt) ||
+                                       (direction == "backward" && this.problems[i].added < last_dt))
+                               {
+                                       last_dt = this.problems[i].added;
+                               }
+                       }
+                       ajax("/problems/" + variant, "GET", {
+                               direction: direction,
+                               last_dt: last_dt,
+                       }, response => {
+                               if (response.problems.length > 0)
+                                       this.problems = response.problems;
+                       });
                },
                showNewproblemModal: function() {
                        document.getElementById("modal-newproblem").checked = true;
                },
-               postNewProblem: function() {
-                       const fen = document.getElementById("newpbFen").value;
-                       const instructions = document.getElementById("newpbInstructions").value;
-                       const solution = document.getElementById("newpbSolution").value;
-                       
+               previewNewProblem: function() {
+                       if (!V.IsGoodFen(this.newProblem.fen))
+                               return alert("Bad FEN string");
+                       this.newProblem.stage = "preview";
+               },
+               sendNewProblem: function() {
+                       // Send it to the server and close modal
+                       ajax("/problems/" + variant, "POST", {
+                               fen: this.newProblem.fen,
+                               instructions: this.newProblem.instructions,
+                               solution: this.newProblem.solution,
+                       }, response => {
+                               document.getElementById("modal-newproblem").checked = false;
+                               this.newProblem.stage = "nothing";
+                       });
                },
        },
 })