diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-11 00:19:44 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-11 00:19:57 +0200 |
commit | 5e12dabced6ce55031f5c7f13afb15048d03edcd (patch) | |
tree | 5ebec65fcc3e92ef31854b28a134c6432bf1eb58 /view | |
parent | 49f7be2a35539bae2d43594c9b39268eec7837d0 (diff) |
Timer create & delete
Diffstat (limited to 'view')
-rw-r--r-- | view/main.templ | 3 | ||||
-rw-r--r-- | view/main_templ.go | 2 | ||||
-rw-r--r-- | view/timers_list.templ | 39 | ||||
-rw-r--r-- | view/timers_list_templ.go | 99 |
4 files changed, 129 insertions, 14 deletions
diff --git a/view/main.templ b/view/main.templ index 1a4bf95..b6d94d8 100644 --- a/view/main.templ +++ b/view/main.templ @@ -7,8 +7,9 @@ templ Main(contents templ.Component) { <title>Cool timer app</title>
<link rel="stylesheet" href="/static/style.css" />
<script src="/static/htmx.min.js"></script>
+ <script src="/static/response-targets.js"></script>
</head>
- <body hx-boost="true">
+ <body hx-boost="true" hx-ext="response-targets">
@contents
</body>
</html>
diff --git a/view/main_templ.go b/view/main_templ.go index 363061e..109be9c 100644 --- a/view/main_templ.go +++ b/view/main_templ.go @@ -23,7 +23,7 @@ func Main(contents templ.Component) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html><head><title>Cool timer app</title><link rel=\"stylesheet\" href=\"/static/style.css\"><script src=\"/static/htmx.min.js\"></script></head><body hx-boost=\"true\">") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html><head><title>Cool timer app</title><link rel=\"stylesheet\" href=\"/static/style.css\"><script src=\"/static/htmx.min.js\"></script><script src=\"/static/response-targets.js\"></script></head><body hx-boost=\"true\" hx-ext=\"response-targets\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/view/timers_list.templ b/view/timers_list.templ index e1731d4..2d71a52 100644 --- a/view/timers_list.templ +++ b/view/timers_list.templ @@ -6,12 +6,41 @@ import ( )
templ timer(t model.Timer) {
- <p><a href={ templ.URL(fmt.Sprint("/timer/", t.Id)) }>{ t.Name }</a></p>
+ <p class="timer-row">
+ <a href={ templ.URL(fmt.Sprint("/timer/", t.Id)) }>{ t.Name }</a>
+ -
+ <a
+ href="javascript:void(0);"
+ hx-delete={ fmt.Sprint("/timer/", t.Id) }
+ hx-target="closest .timer-row"
+ >Delete</a>
+ </p>
+}
+
+templ TimerCreateForm(timerName string, err string) {
+ <form
+ hx-put="/timer"
+ hx-target="closest .timers-list"
+ hx-target-error="this"
+ >
+ <p>
+ <input type="text" name="timerName" value={ timerName } placeholder="Name" />
+ <button type="submit">Create</button>
+ </p>
+ if err != "" {
+ <p class="error">{ err }</p>
+ }
+ </form>
}
templ TimersList(timers []model.Timer) {
- <h1>Timers</h1>
- for _, t := range timers {
- @timer(t)
- }
+ <div class="timers-list">
+ <h1>Timers</h1>
+ for _, t := range timers {
+ @timer(t)
+ }
+ <h4>Create timer</h4>
+ @TimerCreateForm("", "")
+ </div>
}
+
diff --git a/view/timers_list_templ.go b/view/timers_list_templ.go index cbfe2eb..af4922d 100644 --- a/view/timers_list_templ.go +++ b/view/timers_list_templ.go @@ -28,7 +28,7 @@ func timer(t model.Timer) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<p><a href=\"") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<p class=\"timer-row\"><a href=\"") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -44,13 +44,86 @@ func timer(t model.Timer) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(t.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 9, Col: 66} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 10, Col: 67} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a></p>") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a> -\r <a href=\"javascript:void(0);\" hx-delete=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint("/timer/", t.Id)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 14, Col: 51} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" hx-target=\"closest .timer-row\">Delete</a></p>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} + +func TimerCreateForm(timerName string, err string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<form hx-put=\"/timer\" hx-target=\"closest .timers-list\" hx-target-error=\"this\"><p><input type=\"text\" name=\"timerName\" value=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(timerName) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 27, Col: 65} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" placeholder=\"Name\"> <button type=\"submit\">Create</button></p>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if err != "" { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<p class=\"error\">") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(err) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 31, Col: 34} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</p>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</form>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -69,12 +142,12 @@ func TimersList(timers []model.Timer) templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var4 := templ.GetChildren(ctx) - if templ_7745c5c3_Var4 == nil { - templ_7745c5c3_Var4 = templ.NopComponent + templ_7745c5c3_Var8 := templ.GetChildren(ctx) + if templ_7745c5c3_Var8 == nil { + templ_7745c5c3_Var8 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>Timers</h1>") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"timers-list\"><h1>Timers</h1>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -84,6 +157,18 @@ func TimersList(timers []model.Timer) templ.Component { return templ_7745c5c3_Err } } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h4>Create timer</h4>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = TimerCreateForm("", "").Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } if !templ_7745c5c3_IsBuffer { _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) } |