summaryrefslogtreecommitdiff
path: root/code/index.tmpl.html
blob: f60ecf6a2050d79e00cb29815ffbd63c58bf2769 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
{{ if false }}
<!--
	Documentation

This is the web UI of the noelan app.
Every "page" is just setting the whole "<body>"'s innerHTML (see setPage* functions).  This way
we can avoid creating a network request for each page.

Data is cached to local storage so that we only need to send requests to sync the data.
The `global_all_data` object has all the data necessary for the application to work.

TODO(luca): Get rid of PicoCSS, because it does not make a good responsive UI.

-->
{{ end }}


<!DOCTYPE html>
<html lang="fr" data-theme="dark">
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta property="og:title" content="Cadeaux de noël 2025"/>
  <meta property="og:description" content="À qui offrir le cadeau de noël?"/>
  <meta property="og:image" content="/static/favicon.ico" />

  <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
  <style>

h1 {
 display: flex;
 justify-content: center;
}

.centered {
 display: flex;
 justify-content: center;
 align-items: center;
 height: 50vh;
}

button {
 font-size: 1rem;
}

.buttons {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

span.name {
 color: #87bfcf;
}

textarea {
 display: block !important;
}

.fade {
    animation: fadeOut 1s forwards;
}
        
@keyframes fadeOut {
    0% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}

   </style>
   <script>
"use strict";

//- Globals
let people =
[{{ range .People }}
 {"name": "{{.Name}}", "has_picked": {{.HasPicked}}}, {{ end }}
];

let local_storage_key = "{{.Key}}";

let global_all_data = {
 initialized: false,
 token: null,
 thisName: null,
 thisWishlist: null,
 otherName: null,
 otherWishlist: null,
};

//- Helpers
function findPersonByName(picking_person_name) {
 let result = null;

 for(let peopleIndex = 0; peopleIndex < people.length; peopleIndex += 1)
 {
  if(people[peopleIndex].name === picking_person_name)
  {
   result = people[peopleIndex];
   break;
  }
 }

 return result;
}

//- Pages
function setPageChoose() {
 document.body.innerHTML = `
		<div class="centered">
			<div class="container">
				<h1>Qui es-tu?</h1>
				<div class="buttons"></div>
			</div>
		</div>
  `;

 let buttons = document.querySelector("div.buttons");
 for(let index = 0; index < people.length; index += 1)
 {
  let person = people[index];
  if(!person.has_picked)
  {
   let button = document.createElement('button');
   button.textContent = people[index].name;
   buttons.appendChild(button);
   button.addEventListener("click", function(event) {
    event.preventDefault();

				history.pushState({home: "/"}, "", "/");

    let name = person.name;
    let thisPerson = findPersonByName(name);

    document.body.innerHTML = `
					<div class="centered">
						<div class="container">
							<h1>Tu es bien ${thisPerson.name}?</h1>
							<div class="buttons">
								<button id="yes">Oui</button>
								<button id="no">Non</button>
							</div>
						</div>
					</div>`;

    let yes_button = document.getElementById("yes");
    let no_button = document.getElementById("no");

    yes_button.addEventListener("click", async function(event) {
     event.preventDefault();

					history.pushState({home: "/"}, "", "/");

     await fetch(`/api/choose/?name=${thisPerson.name}`)
      .then(function(response) {
          if (!response.ok) {
              console.error('Network response was not ok');
          }
          return response.json();
      })
      .then(function(response) {
       global_all_data.token = response.Token;
       global_all_data.thisName = thisPerson.name;
       global_all_data.thisWishlist = response.ThisWishlist;
       global_all_data.otherName = response.OtherName;
       global_all_data.otherWishlist = response.OtherWishlist;
       global_all_data.initialized = true;

       localStorage.setItem(local_storage_key, JSON.stringify(global_all_data));
       })
      .catch(function(error) {
          console.error('There was a problem with the fetch operation:', error);
      });

     setPageThisPerson();
    });

    no_button.addEventListener("click", function(event) {
     event.preventDefault();

     setPageChoose();
    });

   }); // button click even listener
  } // has_picked
 } // for loop
}

function setPageOtherPerson() {
	history.pushState({home: "/"}, "", "/");

 let wishlistID = "wishlist";
	let backButtonID = "back";

 document.body.innerHTML = `
  <div class="container">
			<h1>Tu as&nbsp;<span class="name">${global_all_data.otherName}</span>.</h1>
			<h4>Sa liste des souhaits:</h4>
			<textarea readonly rows="15" cols="40" id="${wishlistID}" placeholder="Vide..."></textarea>
			<div class="buttons">
				<button id="${backButtonID}">retour</button>
			</div>
  </div>
  `;

	let backButton = document.getElementById(backButtonID);
	backButton.addEventListener("click", function(event) {
		event.preventDefault();

		setPageThisPerson();
	});

 let wishlist = document.getElementById(wishlistID);
 wishlist.textContent = global_all_data.otherWishlist;

	fetch(`/api/list/?user=${global_all_data.thisName}&name=${global_all_data.otherName}&token=${global_all_data.token}`)
 .then(function(response) {
     if (!response.ok) {
         console.error('Network response was not ok');
     }
     return response.text();
 })
 .then(function(response) {
  if(wishlist.textContent != response)
  {
   global_all_data.otherWishlist = response;
   wishlist.textContent = response;
   localStorage.setItem(local_storage_key, JSON.stringify(global_all_data));
  }
 })
 .catch(function(error) {
     console.error('There was a problem with the fetch operation:', error);
 });

}

function setPageThisPerson() {
 let showPersonButtonID = "show-person";
 let writeLetterButtonID = "write-letter";

 document.body.innerHTML = `
  <div class="centered">
			<div class="container">
				<h1>Noël-An de&nbsp;<span class="name">${global_all_data.thisName}</span></h1>
				<div class="buttons">
					<button id="${showPersonButtonID}">Voir qui j'ai choisi</button>
					<button id="${writeLetterButtonID}">Ma liste des souhaits</button>
				</div>
				<h4><b>Infos:</b></h4>
				<ul>
					<li>Ne dévoilez à persone qui vous avez tiré au sort!</li>
					<li>Vous pouvez écrire une liste de souhaits, la personne ayant tiré votre nom y a accès.</li>
					<li>Il n'est pas obligé de respecter la liste ou d'en écrire une. Elle sert pour inspirer des idées de cadeaux.</li>
					<li>Si vous avez un soucis n'hésitez-pas à me contacter !</li>
				</ul>
			</div>
  </div>
  `;

 let showPersonButton = document.getElementById(showPersonButtonID);
 showPersonButton.addEventListener("click", function(event) {
  event.preventDefault();

  setPageOtherPerson();
 });

 let writeLetterButton = document.getElementById(writeLetterButtonID);
 writeLetterButton.addEventListener("click", function(event) {
  event.preventDefault();

		history.pushState({home: "/"}, "", "/");

  let sendLetterButtonID = "send-letter";
  let letterTextAreaID = "letter-text";
		let backButtonID = "back";

  document.body.innerHTML = `
   <div class="container">
			<h1><span class="name">Ma</span>&nbsp;liste des souhaits</h1>
    <textarea id="${letterTextAreaID}" rows="15" cols="40" placeholder="Vide..."></textarea>
    <div class="buttons">
			  <button id="${backButtonID}">retour</button>
     <button id="${sendLetterButtonID}">sauvegarder</button>
    </div>
   </div>
   `;

   let backButton = document.getElementById(backButtonID);
			backButton.addEventListener("click", function(event) {
				event.preventDefault();

				setPageThisPerson();
			});

   let letterTextArea = document.getElementById(letterTextAreaID);
   letterTextArea.textContent = global_all_data.thisWishlist;

			fetch(`/api/list/?user=${global_all_data.thisName}&name=${global_all_data.thisName}&token=${global_all_data.token}`)
			.then(function(response) {
							if (!response.ok) {
											console.error('Network response was not ok');
							}
							return response.text();
			})
			.then(function(response) {
				if(wishlist.textContent != response)
				{
					global_all_data.thisWishlist = response;
					letterTextArea.textContent = response;
					localStorage.setItem(local_storage_key, JSON.stringify(global_all_data));
				}
			})
			.catch(function(error) {
							console.error('There was a problem with the fetch operation:', error);
			});


   let sendLetterButton = document.getElementById(sendLetterButtonID);
   sendLetterButton.addEventListener("click", function(event) {
    event.preventDefault();

    console.log("Sending list of", global_all_data.thisName);

    global_all_data.thisWishlist = letterTextArea.value
    localStorage.setItem(local_storage_key, JSON.stringify(global_all_data));

    let messageBody = {
     name: global_all_data.thisName,
     token: global_all_data.token,
     text: letterTextArea.value,
    };
    const postBody = new URLSearchParams(messageBody).toString();

    fetch('/api/list/', {
     method: 'POST',
     headers:
     {
      'Content-Type': 'application/x-www-form-urlencoded'
     },
     body: postBody,
    })
    .then(function(response) {
     response.text().
      then(function(text_response) {
        const textElement = document.createElement('div');
        textElement.textContent = 'Sauvegardé';
        textElement.classList.add('fade');
        let buttons = document.querySelector("div.buttons");
        buttons.appendChild(textElement);
        setTimeout(function() {
           textElement.remove();
        }, 1000);
      })
     })
     .catch(function(error) { console.error('Error:', error); });

   }); // add event listener send button
 }); // add event listener write letter
}

//- Main
window.onload = function() {
	{{ if false }}
 // NOTE(luca): Users will expect going back to go to the home page so we do this manually.
	{{ end }}
 window.addEventListener('popstate', function(event) {
    window.location.reload();
 });

 let all_data = localStorage.getItem(local_storage_key);
 if(!all_data)
 {
  setPageChoose();
 }
 else
 {
  let data_parsed = JSON.parse(all_data);
  global_all_data = data_parsed;
  console.log(global_all_data);

  setPageThisPerson();
 }
};
  </script>
 </head>
 <body>
 </body>

</html>