diff src/edit_link.js.luan @ 0:8f4df159f06b

start public repo
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 20:57:49 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edit_link.js.luan	Fri Jul 11 20:57:49 2025 -0600
@@ -0,0 +1,37 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or 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 Link = require "site:/lib/Link.luan"
+
+
+return function()
+	local link_id = Http.request.parameters.link or error()
+	local link = Link.get_by_id(link_id)
+	Io.stdout = Http.response.text_writer()
+	if link == nil then
+%>
+		let div = document.querySelector('div[link="<%=link_id%>"]');
+		if(div) div.outerHTML = '';
+<%
+		return
+	end
+	local html = `%>
+				<form onsubmit="ajaxForm('/save_link.js',this)" action="javascript:">
+					<input type=hidden name=link value="<%=link_id%>">
+					<label>Title</label>
+					<input type=text required name=title value="<%=html_encode(link.title)%>">
+					<label>URL</label>
+					<input type=url required name=url value="<%=html_encode(link.url)%>">
+					<button small type=submit>Save</button>
+					<button small type=button onclick="cancel('<%=link.id%>')">Cancel</button>
+				</form>
+<%`
+%>
+	document.querySelector('div[link="<%=link_id%>"]').innerHTML = <%= json_string(html) %>;
+<%
+end