Compare commits

...

2 Commits

Author SHA1 Message Date
bel
518909b7f7 transactions.html can edit though needs some kinda user feedback of the changed row or something...
All checks were successful
cicd / ci (push) Successful in 1m28s
2024-07-21 08:31:37 -06:00
bel
d69de4da11 ui ready enough for amending 2024-07-21 08:23:34 -06:00
3 changed files with 96 additions and 8 deletions

View File

@@ -1,19 +1,24 @@
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
<style>
input[type="text"] {
border: 1px solid black;
}
</style>
<script>
function http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status)
}
};
xmlhttp.open(method, remote, true);
}
xmlhttp.open(method, remote, true)
if (typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
xmlhttp.send(body)
}
function callback(responseBody, responseStatus) {
@@ -83,8 +88,73 @@
const xactionJSON = atob(row.attributes.transaction.value)
const xaction = JSON.parse(xactionJSON)
console.log(xaction)
row.innerHTML = `<td colspan="3"></td>`
TODO
row.attributes.onclick = ""
row.onclick = ""
row.style = ""
console.log(row)
var result = `<td colspan="3">`
result += `<form method="modal">`
result += ` <div>`
result += ` <label for="date">Date</label>`
result += ` <input type="text" name="date" value='${xaction[0].Date}' style="width: 100%;"/>`
result += ` </div>`
result += ` <div>`
result += ` <label for="description">Description</label>`
result += ` <input type="text" name="description" value='${xaction[0].Description}' style="width: 100%;"/>`
result += ` </div>`
for (var i in xaction) {
var delta = xaction[i]
result += `<hr>`
result += `<div style="display: flex; flex-direction: row; width: 100%; margin-top: 1em;">`
result += ` <div style="flex-grow: 1; margin-left: 1em; margin-right: 1em;">`
result += ` <label for="name${i}">Name${i}</label>`
result += ` <input type="text" name="name${i}" value='${delta.Name}' style="width: 100%;"/>`
result += ` </div>`
result += ` <div style="margin-left: 1em; margin-right: 1em;">`
result += ` <label for="value${i}">Value${i}</label>`
result += ` <input type="text" name="value${i}" value='${delta.Value}' style="width: 100%;"/>`
result += ` </div>`
result += ` <input type="button" name="submit${i}" value="Submit${i}" onclick="submitEdit(this); return false;" delta="${btoa(JSON.stringify(delta))}"/>`
result += `</div>`
}
result += `</form`
result += `</td>`
row.innerHTML = result
}
function submitEdit(btn) {
const old = JSON.parse(atob(btn.attributes.delta.value))
const now = JSON.parse(atob(btn.attributes.delta.value))
const idx = btn.name.split("submit")[1]
const form = btn.parentElement.parentElement
now.Value = parseFloat(form[`value${idx}`].value)
now.Name = form[`name${idx}`].value
now.Description = form[`description`].value
now.Date = form[`date`].value
var different = false
for(var k in old) {
different = different || old[k] != now[k]
}
if (!different) {
return
}
http("PUT", "/api/amend", (body, status) => {
if (status != 200)
throw(`Unexpected status ${status}: ${body}`)
load()
}, JSON.stringify({
old: old,
now: now,
}))
}
function stage(who, contributesToHouse) {

View File

@@ -4,6 +4,7 @@ import (
"embed"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"os"
@@ -110,7 +111,21 @@ func (router Router) APITransactions(w http.ResponseWriter, r *http.Request) {
}
func (router Router) APIAmend(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
b, _ := io.ReadAll(r.Body)
var req struct {
Old ledger.Delta
Now ledger.Delta
}
if err := json.Unmarshal(b, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := router.files.Amend(req.Old, req.Now); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func (router Router) APIReg(w http.ResponseWriter, r *http.Request) {

View File

@@ -31,8 +31,11 @@ func (deltas Deltas) Transactions() Transactions {
slices.SortFunc(result, func(a, b Transaction) int {
if a[0].Date == b[0].Date {
if a[0].Description == b[0].Description {
return strings.Compare(a[0].Transaction, b[0].Transaction)
}
return strings.Compare(a[0].Description, b[0].Description)
}
return strings.Compare(a[0].Date, b[0].Date)
})