changeset 24:af41be2dcdec

add edit_post
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Nov 2024 19:45:08 -0700
parents c54c806fcc6e
children 13f4d2c8b8fe
files src/chat.css src/chat.js src/edit_post.js.luan src/index.html.luan src/lib/Post.luan src/lib/Shared.luan src/save_post.js.luan
diffstat 7 files changed, 100 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.css	Tue Nov 05 16:09:10 2024 -0700
+++ b/src/chat.css	Tue Nov 05 19:45:08 2024 -0700
@@ -103,3 +103,8 @@
 	max-height: 150px;
 	resize: none;
 }
+
+dialog textarea {
+	width: 600px;
+	max-width: 80vw;
+}
--- a/src/chat.js	Tue Nov 05 16:09:10 2024 -0700
+++ b/src/chat.js	Tue Nov 05 19:45:08 2024 -0700
@@ -50,7 +50,7 @@
 	let text = textarea.value;
 	if( text.trim() === '' )
 		return;
-	ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`);
+	ajax(`add_post.js?chat=${currentChatId}`,`content=${encodeURIComponent(text)}`);
 	textarea.value = '';
 }
 
@@ -103,6 +103,32 @@
 		div.outerHTML = '';
 }
 
+function editPost(postId) {
+	ajax(`edit_post.js?post=${postId}`);
+}
+
+function openEditPost(postId,text) {
+	currentPostId = postId;
+	let dialog = document.querySelector('dialog[edit_post]');
+	let textarea = dialog.querySelector('textarea');
+	textarea.value = text;
+	openModal(dialog);
+}
+
+function savePost(el) {
+	let text = document.querySelector('dialog[edit_post] textarea').value;
+	closeModal(el);
+	ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
+}
+
+function edited(postId,html) {
+	let div = document.querySelector(`div[post="${postId}"]`);
+	if( div ) {
+		div.outerHTML = html;
+		fixPosts();
+	}
+}
+
 function added(html) {
 	let input = document.querySelector('div[input]');
 	input.insertAdjacentHTML('beforebegin',html);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edit_post.js.luan	Tue Nov 05 19:45:08 2024 -0700
@@ -0,0 +1,18 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Post = require "site:/lib/Post.luan"
+local get_post_by_id = Post.get_by_id or error()
+
+
+return function()
+	local post = Http.request.parameters.post or error()
+	post = get_post_by_id(post) or error()
+	Io.stdout = Http.response.text_writer()
+%>
+	openEditPost('<%=post.id%>',<%=json_string(post.content)%>);
+<%
+end
--- a/src/index.html.luan	Tue Nov 05 16:09:10 2024 -0700
+++ b/src/index.html.luan	Tue Nov 05 19:45:08 2024 -0700
@@ -120,6 +120,14 @@
 				<button go onclick="doDeletePost(this)">Delete</button>
 			</div>
 		</dialog>
+		<dialog edit_post>
+			<h2>Edit Message</h2>
+			<p><textarea onfocus="fixTextarea(event)" oninput="fixTextarea(event)"></textarea></p>
+			<div buttons>
+				<button cancel onclick="closeModal(this)">Cancel</button>
+				<button go onclick="savePost(this)">Save</button>
+			</div>
+		</dialog>
 		<script>
 <%
 	if selected ~= nil then
--- a/src/lib/Post.luan	Tue Nov 05 16:09:10 2024 -0700
+++ b/src/lib/Post.luan	Tue Nov 05 19:45:08 2024 -0700
@@ -38,6 +38,10 @@
 		post.id = doc.id
 	end
 
+	function post.reload()
+		return Post.get_by_id(post.id) or error(post.id)
+	end
+
 	function post.delete()
 		run_in_transaction( function()
 			local id = post.id
--- a/src/lib/Shared.luan	Tue Nov 05 16:09:10 2024 -0700
+++ b/src/lib/Shared.luan	Tue Nov 05 19:45:08 2024 -0700
@@ -106,7 +106,7 @@
 				<span pulldown>
 					<img onclick="clickMenu(this)" src="/images/more_vert.svg">
 					<div>
-						<span>Edit</span>
+						<span onclick="editPost('<%=id%>')">Edit</span>
 						<span onclick="deletePost('<%=id%>')">Delete</span>
 					</div>
 				<span>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/save_post.js.luan	Tue Nov 05 19:45:08 2024 -0700
@@ -0,0 +1,37 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Post = require "site:/lib/Post.luan"
+local get_post_by_id = Post.get_by_id or error()
+local Db = require "site:/lib/Db.luan"
+local run_in_transaction = Db.run_in_transaction or error()
+local Chat = require "site:/lib/Chat.luan"
+local get_chat_by_id = Chat.get_by_id or error()
+local User = require "site:/lib/User.luan"
+local current_user = User.current or error()
+local Shared = require "site:/lib/Shared.luan"
+local post_html = Shared.post_html or error()
+local Utils = require "site:/lib/Utils.luan"
+local is_in_list = Utils.is_in_list or error()
+
+
+return function()
+	local post = Http.request.parameters.post or error()
+	local content = Http.request.parameters.content or error()
+	post = get_post_by_id(post) or error()
+	local chat = get_chat_by_id(post.chat_id) or error()
+	local user_ids = chat.user_ids
+	local user = current_user() or error()
+	is_in_list( user.id, user_ids ) or error()
+	run_in_transaction( function()
+		post = post.reload()
+		post.content = content
+		post.save()
+	end )
+	local html = `post_html(post)`
+	local js = "edited( '"..post.id.."', "..json_string(html).." )"
+	chat.http_push(js)
+end