Skip to content

Commit

Permalink
Add section for creating new entries
Browse files Browse the repository at this point in the history
This is really not a nice approach. I created a new section just
because otherwise the entry creation part would have re-rendered
meaning that you needed to set the values to the current period
somehow.
  • Loading branch information
Ozoniuss committed Oct 13, 2024
1 parent 0ea36d4 commit cbb9d4d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 76 deletions.
150 changes: 79 additions & 71 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,97 +97,105 @@
<h3>Available entries</h3>

Insert new entry here
<form id="element-creation-form" hx-confirm="Are you sure?" hx-post="/createEntry" hx-target="next ul"
<form id="element-creation-form" hx-confirm="Are you sure?" hx-post="/createEntry" hx-target="#all-entries-categorized-inner"
hx-swap="inner-html">
<button type="submit">Create</button>
<label for="entry-category">Category</label>
<input type="text" name="category" id="entry-category">
<label for="entry-subcategory">Subcategory</label>
<input type="text" name="subcategory" id="entry-subcategory">
<label for="entry-expected-value">Expected Value</label>
<input type="number" name="expected-value" id="entry-expected-value">
<label for="entry-expected-total">Expected Total</label>
<input type="number" name="expected-total" id="entry-expected-total">
<label for="entry-currency">Currency</label>
<input type="text" name="currency" id="entry-currency">
<label for="entry-recurring">Recurring</label>
<input type="checkbox" name="recurring" id="entry-recurring">
<input type="hidden" name="month" id="entry-month">
<input type="hidden" name="year" id="entry-year">
</form>

<!-- refresh only this block when creating a new entry -->
<div id="all-entries-categorized-inner"></div>
{{ block "all-entries-categorized-inner" . }}

{{ range .CategorizedEntryList }}

{{ $expectedForCategory := 0 }}
{{ range $_, $entry := .Entries }}
{{ $expectedForCategory = (Sum $expectedForCategory $entry.TotalMoney) }}
{{ end }}
{{ $expectedForCategory := 0 }}
{{ range $_, $entry := .Entries }}
{{ $expectedForCategory = (Sum $expectedForCategory $entry.TotalMoney) }}
{{ end }}

<h4>
{{ .Category }} [Expected: {{ $expectedForCategory }}] [Actual]
</h4>
Insert new expense to this category
<form id="element-creation-form" hx-confirm="Are you sure?" hx-post="/createExpense" hx-target="next ul"
hx-swap="inner-html">
<button type="submit">Create</button>
<label for="expense-subcategory">Subcategory</label>
<input type="text" name="subcategory" id="expense-subcategory">
<label for="expense-name">Name</label>
<input type="text" name="name" id="expense-name">
<label for="expected-value">Expected Value</label>
<input type="number" name="total-money" id="expected-value">
<label for="expected-currency">Currency</label>
<input type="text" name="currency" id="expected-currency">
<label for="expense-details">Details</label>
<input type="text" name="details" id="expense-details">
<label for="payment-method">Payment Method</label>
<input type="text" name="payment-method" id="payment-method">
<input type="hidden" name="category" value="{{ .Category }}">
</form>
<ul id="subcategories-list">
{{ block "all-entries-categorized" . }}
<li>
<div class="entry-row header">
<div><span>Subcategory</span></div>
<div><span>Id</span></div>
<div><span>Expected</span></div>
<div><span>Recurring</span></div>
<div><span>RON</span></div>
<div><span>EUR</span></div>
<div><span>USD</span></div>
<div><span>GBP</span></div>
</div>
</li>
{{ range .Entries }}
<li>
<div class="collapsible entry-row">
<div><span>{{ .Subcategory }}</span></div>
<div><span>{{ .Id }}</span></div>
<div><span>{{ .TotalMoney }} {{ .Currency }}</span></div>
<div><span>{{ .Recurring }}</span></div>
<div><span>{{ .RunningTotal.RON }}</span></div>
<div><span>{{ .RunningTotal.EUR }}</span></div>
<div><span>{{ .RunningTotal.USD }}</span></div>
<div><span>{{ .RunningTotal.GBP }}</span></div>
<!-- Id: {{ .Id }}, Expected: {{.TotalMoney }} {{ .Currency }} Category: {{ .Category }}
Subcategory: {{ .Subcategory }} Recurring: {{ .Recurring }} Total: {{ .RunningTotal.RON }} RON
{{ .RunningTotal.EUR }} EUR {{ .RunningTotal.USD }} USD -->
</div>
<div class="content">
{{ if not .Expenses }} No expenses recorded here
{{ else }}
<ul id="expenses-list">
{{ $entid := .Id }}{{ range .Expenses }}
<li class="expense-row">
<button hx-vals='{"expid":"{{ .Id }}","entid":"{{ $entid }}"}' hx-swap="outerHTML" hx-target="closest li" hx-post="/deleteExpense">delete</button> Id: {{ .Id }} Total Money: {{ .TotalMoney }} {{ .Currency }}
</li>
<h4>
{{ .Category }} [Expected: {{ $expectedForCategory }}] [Actual]
</h4>
Insert new expense to this category
<form id="element-creation-form" hx-confirm="Are you sure?" hx-post="/createExpense" hx-target="next ul"
hx-swap="inner-html">
<button type="submit">Create</button>
<label for="expense-subcategory">Subcategory</label>
<input type="text" name="subcategory" id="expense-subcategory">
<label for="expense-name">Name</label>
<input type="text" name="name" id="expense-name">
<label for="expected-value">Expected Value</label>
<input type="number" name="total-money" id="expected-value">
<label for="expected-currency">Currency</label>
<input type="text" name="currency" id="expected-currency">
<label for="expense-details">Details</label>
<input type="text" name="details" id="expense-details">
<label for="payment-method">Payment Method</label>
<input type="text" name="payment-method" id="payment-method">
<input type="hidden" name="category" value="{{ .Category }}">
</form>
<ul id="subcategories-list">
{{ block "all-entries-categorized" . }}
<li>
<div class="entry-row header">
<div><span>Subcategory</span></div>
<div><span>Id</span></div>
<div><span>Expected</span></div>
<div><span>Recurring</span></div>
<div><span>RON</span></div>
<div><span>EUR</span></div>
<div><span>USD</span></div>
<div><span>GBP</span></div>
</div>
</li>
{{ range .Entries }}
<li>
<div class="collapsible entry-row">
<div><span>{{ .Subcategory }}</span></div>
<div><span>{{ .Id }}</span></div>
<div><span>{{ .TotalMoney }} {{ .Currency }}</span></div>
<div><span>{{ .Recurring }}</span></div>
<div><span>{{ .RunningTotal.RON }}</span></div>
<div><span>{{ .RunningTotal.EUR }}</span></div>
<div><span>{{ .RunningTotal.USD }}</span></div>
<div><span>{{ .RunningTotal.GBP }}</span></div>
<!-- Id: {{ .Id }}, Expected: {{.TotalMoney }} {{ .Currency }} Category: {{ .Category }}
Subcategory: {{ .Subcategory }} Recurring: {{ .Recurring }} Total: {{ .RunningTotal.RON }} RON
{{ .RunningTotal.EUR }} EUR {{ .RunningTotal.USD }} USD -->
</div>
<div class="content">
{{ if not .Expenses }} No expenses recorded here
{{ else }}
<ul id="expenses-list">
{{ $entid := .Id }}{{ range .Expenses }}
<li class="expense-row">
<button hx-vals='{"expid":"{{ .Id }}","entid":"{{ $entid }}"}' hx-swap="outerHTML" hx-target="closest li" hx-post="/deleteExpense">delete</button> Id: {{ .Id }} Total Money: {{ .TotalMoney }} {{ .Currency }}
</li>
{{ end }}
</ul>
{{ end }}
</ul>
{{ end }}
</div>
</li>
{{ end }}
</div>
</li>
{{ end }}
{{ end }}
</ul>
{{ end }}
</ul>

{{ end }}
</div>

{{ end }}
</div>

Expand Down
75 changes: 70 additions & 5 deletions ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
Expand All @@ -12,6 +13,7 @@ import (
"time"

"github.com/Ozoniuss/casheer/client/httpclient"
"github.com/Ozoniuss/casheer/pkg/casheerapi"
"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -238,11 +240,79 @@ func main() {
tmpl.ExecuteTemplate(w, "all-entries-categorized", templateData.CategorizedEntryList[idx])
}

handleCreateEntry := func(w http.ResponseWriter, r *http.Request) {

category := r.FormValue("category")
subcategory := r.FormValue("subcategory")
expectedTotalStr := r.FormValue("expected-total")
recurringCheck := r.FormValue("recurring")
currency := r.FormValue("currency")
monthStr := r.FormValue("month")
yearStr := r.FormValue("year")

recurring := false
if recurringCheck == "on" {
recurring = true
}

// basic validation
if subcategory == "" || category == "" {
w.WriteHeader(400)
fmt.Fprint(w, "some fields should not be empty")
return
}

if currency == "" {
currency = "RON"
}

expectedTotal, err := strconv.Atoi(expectedTotalStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid expected total: %s", err.Error())
return
}
month, err := strconv.Atoi(monthStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid month: %s", err.Error())
return
}
year, err := strconv.Atoi(yearStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid year: %s", err.Error())
return
}

createdEntry, err := cl.CreateEntry(month, year, category, subcategory, expectedTotal, currency, recurring)
if err != nil {
var apierr casheerapi.ErrorResponse
if errors.As(err, &apierr) {
fmt.Printf("got error %v\n", apierr)
w.WriteHeader(apierr.Err.Status)
fmt.Fprintf(w, "could not create entry: %s", apierr.Err.Detail)
} else {
fmt.Printf("got error %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "could not create entry: %s", err)
}
}

fmt.Printf("created entry %+v\n", createdEntry)

templateData.CategorizedEntryList = loadCategorizedEntriesList(cl)

tmpl := template.Must(template.New("index.html").Funcs(Funcs).ParseFiles("index.html"))
tmpl.ExecuteTemplate(w, "all-entries-categorized-inner", templateData)
}

http.HandleFunc("/", h1)
http.HandleFunc("/deleteDebt", handleDeleteDebt)
http.HandleFunc("/deleteExpense", handleDeleteExpense)
http.HandleFunc("/createDebt", handleCreateDebt)
http.HandleFunc("/createExpense", handleCreateExpense)
http.HandleFunc("/createEntry", handleCreateEntry)
http.HandleFunc("/year", func(w http.ResponseWriter, r *http.Request) {
// year=2023
yearstr, err := io.ReadAll(r.Body)
Expand Down Expand Up @@ -361,11 +431,6 @@ func createCategoriesArray(entries []EntryListItem) []CategoryWithEntries {
categories := make(map[string][]EntryListItem)
for _, e := range entries {
categories[e.Category] = append(categories[e.Category], e)
// if _, ok := categories[e.Category]; ok {
// categories[e.Category] = append(categories[e.Category], e)
// } else {
// categories[e.Category] = []EntryListItem{e}
// }
}

categoriesWithEntries := make([]CategoryWithEntries, 0)
Expand Down

0 comments on commit cbb9d4d

Please sign in to comment.