annotate src/account.html.luan @ 40:7ea33179592a

email notification
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 27 Feb 2025 16:44:20 -0700
parents 62d04ca486dd
children 818697418dbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
3 local Parsers = require "luan:Parsers.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
4 local json_string = Parsers.json_string or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local header = Shared.header or error()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
10 local Utils = require "site:/lib/Utils.luan"
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
11 local base_url = Utils.base_url or error()
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
12 local User = require "site:/lib/User.luan"
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
13 local current_user = User.current_required or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 return function()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
17 local user = current_user()
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
18 if user == nil then return end
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
19 local notify_email = user.notify_email
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 <% head() %>
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
26 <script>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
27 'use strict';
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
28
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
29 let notifyEmail = <%= json_string(notify_email or "") %>;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
30 let multiNotify = <%= user.multi_notify %>;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
31
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
32 function showNotify() {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
33 let span = document.querySelector('span[notify]');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
34 span.textContent = notifyEmail ? `Send notifications to ${notifyEmail}` : 'No notifications'
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
35 }
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
36
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
37 function editNotify() {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
38 let dialog = document.querySelector('dialog[edit_notify]');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
39 let input = dialog.querySelector('input[name=notify_email]');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
40 input.value = notifyEmail;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
41 let radio = dialog.querySelector(`input[type=radio][value=${multiNotify}]`);
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
42 radio.checked = true;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
43 openModal(dialog);
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
44 }
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
45
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
46 function saveNotify() {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
47 let dialog = document.querySelector('dialog[edit_notify]');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
48 let input = dialog.querySelector('input[name=notify_email]');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
49 notifyEmail = input.value;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
50 let radio = dialog.querySelector('input[type=radio]:checked');
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
51 multiNotify = radio.value === 'true';
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
52 closeModal(input);
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
53 showNotify();
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
54 ajax(`save_notify.js?email=${encodeURIComponent(notifyEmail)}&multi=${multiNotify}`);
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
55 }
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
56
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
57 function deleteUser() {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
58 let dialog = document.querySelector('dialog[delete_user]');
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
59 openModal(dialog);
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
60 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
61
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
62 function doDeleteUser(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
63 closeModal(el);
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
64 ajax('delete_user.js');
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
65 }
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
66
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
67 function init() {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
68 showNotify();
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
69 }
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
70 </script>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
71 <style>
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
72 div[content] {
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
73 margin-left: auto;
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
74 margin-right: auto;
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
75 width: fit-content;
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
76 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
77 h1 {
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
78 text-align: center;
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
79 }
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
80 input[name=notify_email] {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
81 width: 300px;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
82 }
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
83 span[note] {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
84 font-size: small;
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
85 }
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
86 </style>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 <div content>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
91 <h1>Your Account</h1>
21
5a56297713a3 move chat to home
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
92 <p><a href="about.html">About Web Chat</a></p>
5a56297713a3 move chat to home
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
93 <p>Your URL: <%= base_url() %>/?with=<%=user.email%></p>
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
94 <p><span notify></span> <a href="javascript:editNotify()">Edit</a></p>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
95 <p><a href="javascript:logout()">Logout</a></p>
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
96 <p><a href="javascript:deleteUser()">Delete account</a></p>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 </div>
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
98 <dialog edit_notify>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
99 <h2>Edit Notification</h2>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
100 <form action="javascript:saveNotify()">
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
101 <p>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
102 <label>Send notifications to</label><br>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
103 <input type=email name=notify_email><br>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
104 <span note>Leave blank for no notifications</span>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
105 </p>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
106 <p>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
107 <label clickable>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
108 <input type=radio name=multi_notify value=false>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
109 Notify only for first message
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
110 </label><br>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
111 <label clickable>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
112 <input type=radio name=multi_notify value=true>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
113 Notify for all messages
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
114 </label>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
115 </p>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
116 <div buttons>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
117 <button type=button cancel onclick="closeModal(this)">Cancel</button>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
118 <button type=submit go>Save</button>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
119 </div>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
120 </form>
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
121 </dialog>
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
122 <dialog delete_user>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
123 <h2>Delete Account</h2>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
124 <p>Are you sure that you want to delete your account?</p>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
125 <div buttons>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
126 <button cancel onclick="closeModal(this)">Cancel</button>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
127 <button go onclick="doDeleteUser(this)">Delete</button>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
128 </div>
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
129 </dialog>
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
130 <script> init(); </script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 end