Making the behaviour consistent across the multiple pages
This commit is contained in:
@@ -2,3 +2,4 @@
|
|||||||
- [ ] On the categories page, add a column of how many times a given category is
|
- [ ] On the categories page, add a column of how many times a given category is
|
||||||
used
|
used
|
||||||
- [ ] Make all the other items update/create page be like expenses
|
- [ ] Make all the other items update/create page be like expenses
|
||||||
|
- [ ] Make working with the DB better, yo
|
||||||
|
|||||||
@@ -1,12 +1,40 @@
|
|||||||
{{ define "category" }}
|
{{ define "category" }}
|
||||||
<table>
|
<div>
|
||||||
<tr>
|
<form id="category-form" action="submit">
|
||||||
<td>ID:</td>
|
<fieldset id="form-box-boundaries">
|
||||||
<td>{{ .category.CategoryID }}</td>
|
<legend>CATEGORY FORM</legend>
|
||||||
</tr>
|
<div id="form-state" class="warning"></div><br>
|
||||||
<tr>
|
|
||||||
<td>Name:</td>
|
<div class="form-container form-left">
|
||||||
<td>{{ .category.CategoryName }}</td>
|
<label for="category-name">NAME:</label>
|
||||||
</tr>
|
<input class="tui-input underline" type="text" name="category-name"
|
||||||
</table>
|
id="category-name" value="{{ .category.CategoryName }}">
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<button
|
||||||
|
class="subbutton"
|
||||||
|
{{ if eq .method "put" }}
|
||||||
|
hx-put="/categories/{{ .category.CategoryID }}"
|
||||||
|
{{ end }}
|
||||||
|
{{ if eq .method "post" }}
|
||||||
|
hx-post="/categories/new"
|
||||||
|
{{ end }}
|
||||||
|
hx-swap="none"
|
||||||
|
type="submit"
|
||||||
|
id="expense-submit-button">
|
||||||
|
{{- 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="/categories/{{ .category.CategoryID }}"
|
||||||
|
id="expense-delete-button">DELETE</button>
|
||||||
|
{{ end }}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{ template "deleteModal" }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
{{ define "categoryNew" }}
|
|
||||||
<div>
|
|
||||||
<form id="category-form" action="submit">
|
|
||||||
<fieldset id="form-box-boundaries">
|
|
||||||
<legend>CATEGORY FORM</legend>
|
|
||||||
<div class="form-container form-left">
|
|
||||||
<div id="form-state" class="warning"></div><br>
|
|
||||||
|
|
||||||
<label class="fullwidth-label" for="category-name">NAME</label>
|
|
||||||
<input class="tui-input underline fullwidth" type="text" name="category-name" id="category-name"><br><br>
|
|
||||||
|
|
||||||
<button
|
|
||||||
class="subbutton"
|
|
||||||
hx-post="/categories/new"
|
|
||||||
hx-swap="none"
|
|
||||||
type="submit"
|
|
||||||
id="category-submit-button">CREATE</button>
|
|
||||||
<br>
|
|
||||||
</div>
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{{ define "deleteModal" }}
|
||||||
|
<dialog id="confirm-modal">
|
||||||
|
<form method="dialog" class="modal-content">
|
||||||
|
<p>Are you sure you want to delete this item?</p>
|
||||||
|
<menu>
|
||||||
|
<button id="modal-cancel">Cancel</button>
|
||||||
|
<button id="modal-confirm" class="danger">Yes, Delete</button>
|
||||||
|
</menu>
|
||||||
|
</form>
|
||||||
|
</dialog>
|
||||||
|
{{ end }}
|
||||||
@@ -91,15 +91,7 @@
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<dialog id="confirm-modal">
|
{{ template "deleteModal" }}
|
||||||
<form method="dialog" class="modal-content">
|
|
||||||
<p>Are you sure you want to delete this item?</p>
|
|
||||||
<menu>
|
|
||||||
<button id="modal-cancel">Cancel</button>
|
|
||||||
<button id="modal-confirm" class="danger">Yes, Delete</button>
|
|
||||||
</menu>
|
|
||||||
</form>
|
|
||||||
</dialog>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -110,34 +102,5 @@
|
|||||||
row.remove();
|
row.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let modal = document.getElementById("confirm-modal");
|
|
||||||
let confirmButton = document.getElementById("modal-confirm");
|
|
||||||
let cancelButton = document.getElementById("modal-cancel");
|
|
||||||
|
|
||||||
let pendingDeleteUrl = null;
|
|
||||||
let deleteTarget = null;
|
|
||||||
|
|
||||||
function confirmDelete(button) {
|
|
||||||
pendingDeleteUrl = button.getAttribute("data-delete-url");
|
|
||||||
deleteTarget = button.closest("tr");
|
|
||||||
modal.showModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
confirmButton.addEventListener("click", function () {
|
|
||||||
if (pendingDeleteUrl) {
|
|
||||||
// Use HTMX to send the DELETE
|
|
||||||
htmx.ajax('DELETE', pendingDeleteUrl, {
|
|
||||||
target: deleteTarget,
|
|
||||||
swap: 'outerHTML'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
modal.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
cancelButton.addEventListener("click", function () {
|
|
||||||
modal.close();
|
|
||||||
pendingDeleteUrl = null;
|
|
||||||
deleteTarget = null;
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<script content="text/javascript" defer="" src="/assets/js/htmx.min.js"></script>
|
<script content="text/javascript" defer="" src="/assets/js/htmx.min.js"></script>
|
||||||
<script content="text/javascript" defer="" src="/assets/js/pager.js"></script>
|
<script content="text/javascript" defer="" src="/assets/js/pager.js"></script>
|
||||||
<script content="text/javascript" defer="" src="/assets/js/forms.js"></script>
|
<script content="text/javascript" defer="" src="/assets/js/forms.js"></script>
|
||||||
|
<script content="text/javascript" defer="" src="/assets/js/modals.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
@@ -60,8 +61,8 @@
|
|||||||
{{ template "categories" . }}
|
{{ template "categories" . }}
|
||||||
{{ else if eq .content "category" }}
|
{{ else if eq .content "category" }}
|
||||||
{{ template "category" . }}
|
{{ template "category" . }}
|
||||||
{{ else if eq .content "categoryNew" }}
|
{{ else if eq .content "category" }}
|
||||||
{{ template "categoryNew" . }}
|
{{ 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" }}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
let pendingDeleteUrl = null;
|
||||||
|
let deleteTarget = null;
|
||||||
|
|
||||||
|
function confirmDelete(button) {
|
||||||
|
let modal = document.getElementById("confirm-modal");
|
||||||
|
pendingDeleteUrl = button.getAttribute("data-delete-url");
|
||||||
|
deleteTarget = button.closest("tr");
|
||||||
|
modal.showModal();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
document.body.addEventListener("click", function(evt) {
|
||||||
|
const modalCancelBtn = evt.target.closest("#modal-cancel");
|
||||||
|
if (!modalCancelBtn) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let modal = document.getElementById("confirm-modal");
|
||||||
|
modal.close();
|
||||||
|
pendingDeleteUrl = null;
|
||||||
|
deleteTarget = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
document.body.addEventListener("click", function(evt) {
|
||||||
|
const modalConfirmBtn = evt.target.closest("#modal-confirm");
|
||||||
|
if (!modalConfirmBtn) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let modal = document.getElementById("confirm-modal");
|
||||||
|
if (pendingDeleteUrl) {
|
||||||
|
htmx.ajax('DELETE', pendingDeleteUrl, {
|
||||||
|
target: deleteTarget,
|
||||||
|
swap: 'outerHTML'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
modal.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1 +1,2 @@
|
|||||||
assets: "./assets/"
|
assets: "./assets/"
|
||||||
|
port: 8081
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
AssetsDir string `yaml:"assets"`
|
AssetsDir string `yaml:"assets"`
|
||||||
|
Port string `yaml:"port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
@@ -62,12 +61,12 @@ func GetCategory(catID int) (Category, error) {
|
|||||||
|
|
||||||
query := "SELECT CategoryID,CategoryName " +
|
query := "SELECT CategoryID,CategoryName " +
|
||||||
"FROM categories " +
|
"FROM categories " +
|
||||||
"WHERE CategoryID = " + strconv.Itoa(catID)
|
"WHERE CategoryID = ?"
|
||||||
|
|
||||||
var cat Category
|
cat := Category{CategoryID: -1}
|
||||||
err = db.QueryRow(query).Scan(&cat.CategoryID, &cat.CategoryName)
|
err = db.QueryRow(query, catID).Scan(&cat.CategoryID, &cat.CategoryName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Category{}, nil
|
return Category{CategoryID: -1}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return cat, nil
|
return cat, nil
|
||||||
@@ -123,3 +122,31 @@ func (cat *Category) Delete() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cat *Category) Update() error {
|
||||||
|
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
_, err = db.Exec("PRAGMA foreign_keys = ON")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
query := "UPDATE categories SET CategoryName = ? WHERE CategoryID = ?"
|
||||||
|
res, err := db.Exec(query, cat.CategoryName, cat.CategoryID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if rowsAffected == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
+1
-2
@@ -50,11 +50,10 @@ func main() {
|
|||||||
pages.RouteTypes(router)
|
pages.RouteTypes(router)
|
||||||
pages.RoutePayments(router)
|
pages.RoutePayments(router)
|
||||||
pages.RouteOverview(router)
|
pages.RouteOverview(router)
|
||||||
|
|
||||||
pages.RouteNotFound(router)
|
pages.RouteNotFound(router)
|
||||||
pages.RouteServerError(router)
|
pages.RouteServerError(router)
|
||||||
|
|
||||||
err := router.Run(":8081")
|
err := router.Run(":" + cfg.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to launch application: \n %s\n", err)
|
log.Printf("Failed to launch application: \n %s\n", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-3
@@ -3,6 +3,7 @@ package pages
|
|||||||
import (
|
import (
|
||||||
"expenses/expenses"
|
"expenses/expenses"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -41,20 +42,26 @@ func categoryPage(c *gin.Context) {
|
|||||||
ServerErrorView(c, "The server too makes mistakes")
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if category.CategoryID == -1 {
|
||||||
|
NotFoundView(c, fmt.Sprintf("Could not find category `%d`", categoryID))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||||
"page": "category",
|
"page": "category",
|
||||||
"renderNavBar": false,
|
"renderNavBar": false,
|
||||||
"content": "category",
|
"content": "category",
|
||||||
"category": category,
|
"category": category,
|
||||||
|
"method": "put",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCategoryPage(c *gin.Context) {
|
func newCategoryPage(c *gin.Context) {
|
||||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||||
"page": "expenseNew",
|
"page": "categoryNew",
|
||||||
"renderNavBar": false,
|
"renderNavBar": false,
|
||||||
"content": "categoryNew",
|
"content": "category",
|
||||||
|
"method": "post",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +80,29 @@ func createCategory(c *gin.Context) {
|
|||||||
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateCategory(c *gin.Context) {
|
||||||
|
categoryID, err := strconv.Atoi(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
NotFoundView(c, "No such category")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
categoryName := c.PostForm("category-name")
|
||||||
|
category := expenses.Category{
|
||||||
|
CategoryID: categoryID,
|
||||||
|
CategoryName: categoryName,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = category.Update()
|
||||||
|
if err != nil {
|
||||||
|
errMsg, _ := json.Marshal(err.Error())
|
||||||
|
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":%s}", errMsg))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
||||||
|
}
|
||||||
|
|
||||||
func deleteCategory(c *gin.Context) {
|
func deleteCategory(c *gin.Context) {
|
||||||
categoryID, err := strconv.Atoi(c.Param("id"))
|
categoryID, err := strconv.Atoi(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -91,7 +121,8 @@ func deleteCategory(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := fmt.Sprintf("error deleting category %d", categoryID)
|
errMsg := fmt.Sprintf("error deleting category %d: %+v", categoryID,
|
||||||
|
err.Error())
|
||||||
c.String(http.StatusInternalServerError, errMsg)
|
c.String(http.StatusInternalServerError, errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -107,4 +138,5 @@ func RouteCategories(router *gin.Engine) {
|
|||||||
router.GET(CategoriesPath+"/new", newCategoryPage)
|
router.GET(CategoriesPath+"/new", newCategoryPage)
|
||||||
router.POST(CategoriesPath+"/new", createCategory)
|
router.POST(CategoriesPath+"/new", createCategory)
|
||||||
router.DELETE(CategoriesPath+"/:id", deleteCategory)
|
router.DELETE(CategoriesPath+"/:id", deleteCategory)
|
||||||
|
router.PUT(CategoriesPath+"/:id", updateCategory)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user