]> git.plutz.net Git - invoices/blobdiff - clients.sh
split code into multiple files
[invoices] / clients.sh
diff --git a/clients.sh b/clients.sh
new file mode 100755 (executable)
index 0000000..fc047a2
--- /dev/null
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+edit_client(){
+  id="$1"
+  if [ -f "clients/$id" ]; then
+    read -r address hourly <"clients/$id"
+  fi
+  [ "$address" ] || address="address="
+  [  "$hourly" ] ||  hourly="hourly="
+  printf '
+    [form method="POST" action="/update_client"
+      [hidden "id" "%s"]
+      <textarea name="address" placeholder="address">
+%s</textarea>
+      [label for=hourly Hourly Rate:]
+      [input #hourly type=number name=hourly value="%s"]
+      [submit "update" "update" Update]
+    ]' \
+    "$(HTML $id)" \
+    "$(UNSTRING "${address#address=}" |HTML)" \
+    "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')"
+}
+
+list_clients(){
+  [ -d clients/ ] && for c in clients/*; do
+    read -r address hourly <"$c"
+    address="$(UNSTRING "${address#address=}")"
+    [ "$address" ] || address="(no address)"
+    printf '[div .client .address <!--
+      -->%s
+
+[label Hourly Rate:] %s€
+    [a href="/clients/%s" Edit]]
+    ' "$(HTML "$address")" \
+      "$(HTML "${hourly#hourly=}")" \
+      "$(HTML "${c#clients/}")"
+  done
+}
+
+update_client(){
+  id="$(POST id |checkid)"
+  if [ "$(POST update)" = update -a "$id" ]; then
+    mkdir -p clients
+    printf 'address=%s hourly=%s' \
+      "$(POST address |STRING)" "$(POST hourly |STRING)" \
+      >"clients/$id"
+  else
+    echo Invalid Data "$(POST id)" "$(POST update)" >&2
+  fi
+}