Moving the behaviour to types too

This commit is contained in:
2025-07-28 22:59:34 +01:00
parent 5261ba3ea3
commit ef21189d6c
6 changed files with 52 additions and 57 deletions
+7 -1
View File
@@ -5,7 +5,13 @@
<legend>CATEGORY FORM</legend> <legend>CATEGORY FORM</legend>
<div id="form-state" class="warning"></div><br> <div id="form-state" class="warning"></div><br>
<div class="form-container form-left"> <div class="form-container form-inline">
{{ if eq .method "put" }}
<label for="category-id">ID:</label>
<input class="tui-input underline" type="text" readonly
name="category-id" id="category-id" value="{{ .category.CategoryID }}">
{{ end }}
<label for="category-name">NAME:</label> <label for="category-name">NAME:</label>
<input class="tui-input underline" type="text" name="category-name" <input class="tui-input underline" type="text" name="category-name"
id="category-name" value="{{ .category.CategoryName }}"> id="category-name" value="{{ .category.CategoryName }}">
+2 -4
View File
@@ -61,8 +61,6 @@
{{ template "categories" . }} {{ template "categories" . }}
{{ else if eq .content "category" }} {{ else if eq .content "category" }}
{{ template "category" . }} {{ template "category" . }}
{{ else if eq .content "category" }}
{{ template "category" . }}
{{ else if eq .content "overview" }} {{ else if eq .content "overview" }}
{{ template "overview" . }} {{ template "overview" . }}
{{ else if eq .content "stores" }} {{ else if eq .content "stores" }}
@@ -73,8 +71,8 @@
{{ template "storeNew" . }} {{ template "storeNew" . }}
{{ else if eq .content "types" }} {{ else if eq .content "types" }}
{{ template "types" . }} {{ template "types" . }}
{{ else if eq .content "typeNew" }} {{ else if eq .content "type" }}
{{ template "typeNew" . }} {{ template "type" . }}
{{ else }} {{ else }}
<div>Unknown page</div> <div>Unknown page</div>
{{ end }} {{ end }}
+31 -23
View File
@@ -6,32 +6,40 @@
<div class="form-container form-inline"> <div class="form-container form-inline">
<div id="form-state" class="warning"></div><br> <div id="form-state" class="warning"></div><br>
<p> {{ if eq .method "put" }}
<label for="type-id">ID:</label> <label for="type-id">ID:</label>
<input class="tui-input underline fullwidth" <input class="tui-input underline" type="text" readonly
type="text" readonly name="type-id" id="type-id" value="{{ .type.TypeID }}">
name="type-id" id="type-id" value="{{ .type.TypeID }}"> {{ end }}
</p>
<br> <label class="fullwidth-label" for="type-name">NAME:</label>
<input class="tui-input underline" type="text" name="type-name"
id="type-name" value="{{ .type.TypeName }}">
<p>
<label for="type-name">NAME</label>
<input class="tui-input underline fullwidth"
type="text"
name="type-name" id="type-name" value="{{ .type.TypeName }}">
</p>
<br>
<!-- TODO -->
<!-- The after attribute of this button is written on a newline for no reason -->
<button
class="subbutton"
hx-put="/types/{{ .type.TypeID }}"
hx-swap="none"
type="submit"
id="type-submit-button">UPDATE</button><br>
</div> </div>
</fieldset> </fieldset>
<br>
<button
class="subbutton"
{{ if eq .method "put" }}
hx-put="/types/{{ .type.TypeID }}"
{{ end }}
{{ if eq .method "post" }}
hx-post="/types/new"
{{ end }}
hx-swap="none"
type="submit">
{{- if eq .method "put" }}UPDATE{{- end }}
{{- if eq .method "post" }}CREATE{{- end }}</button>
{{ if eq .method "put" }}
<button
onclick="confirmDelete(this)"
type="button"
class="delete-button"
data-delete-url="/types/{{ .type.TypeID }}">DELETE</button>
{{ end }}
</form> </form>
</div> </div>
{{ template "deleteModal" }}
{{ end }} {{ end }}
-23
View File
@@ -1,23 +0,0 @@
{{ define "typeNew" }}
<div>
<form id="type-form" action="submit">
<fieldset id="form-box-boundaries">
<legend>TYPE FORM</legend>
<div class="form-container form-left">
<div id="form-state" class="warning"></div><br>
<label class="fullwidth-label" for="type-name">NAME</label>
<input class="tui-input underline fullwidth" type="text" name="type-name" id="type-name"><br><br>
<button
class="subbutton"
hx-post="/types/new"
hx-swap="none"
type="submit"
id="type-submit-button">CREATE</button>
<br>
</div>
</fieldset>
</form>
</div>
{{ end }}
+4 -5
View File
@@ -4,7 +4,6 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"strconv"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@@ -58,12 +57,12 @@ func GetType(typID int) (Type, error) {
query := "SELECT TypeID,TypeName " + query := "SELECT TypeID,TypeName " +
"FROM expenseTypes " + "FROM expenseTypes " +
"WHERE TypeID = " + strconv.Itoa(typID) "WHERE TypeID = ?"
var nType Type nType := Type{TypeID: -1}
err = db.QueryRow(query).Scan(&nType.TypeID, &nType.TypeName) err = db.QueryRow(query, typID).Scan(&nType.TypeID, &nType.TypeName)
if err != nil { if err != nil {
return Type{}, nil return Type{TypeID: -1}, nil
} }
return nType, nil return nType, nil
+8 -1
View File
@@ -39,6 +39,11 @@ func typePage(c *gin.Context) {
typ, err := expenses.GetType(typeID) typ, err := expenses.GetType(typeID)
if err != nil { if err != nil {
ServerErrorView(c, "failed to retrieve requested type") ServerErrorView(c, "failed to retrieve requested type")
return
}
if typ.TypeID == -1 {
NotFoundView(c, fmt.Sprintf("Couldn't find type `%d`", typeID))
return
} }
c.HTML(http.StatusOK, "terminal", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
@@ -46,6 +51,7 @@ func typePage(c *gin.Context) {
"renderNavBar": false, "renderNavBar": false,
"content": "type", "content": "type",
"type": typ, "type": typ,
"method": "put",
}) })
} }
@@ -53,7 +59,8 @@ func newTypePage(c *gin.Context) {
c.HTML(http.StatusOK, "terminal", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "typeNew", "page": "typeNew",
"renderNavBar": false, "renderNavBar": false,
"content": "typeNew", "content": "type",
"method": "post",
}) })
} }