-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bugs discovered while making an example
- Loading branch information
Showing
9 changed files
with
241 additions
and
888 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
{{block "head" "example"}} | ||
<head> | ||
<meta charset='UTF-8'/> | ||
<title>{{.}}</title> | ||
<script src='https://unpkg.com/htmx.org@2.0.1' integrity='sha384-QWGpdj554B4ETpJJC9z+ZHJcA/i59TyjxEPXiiUgN2WmTyV5OEZWCD6gQhgkdpB/' crossorigin='anonymous'></script> | ||
<script src='https://unpkg.com/htmx-ext-response-targets@2.0.0/response-targets.js'></script> | ||
|
||
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css'> | ||
</head> | ||
{{end}} | ||
<body hx-ext='response-targets'> | ||
<main class='container'> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Fruit</th> | ||
<th>Count</th> | ||
</tr> | ||
</thead> | ||
<tbody hx-target="closest tr" hx-swap="outerHTML"> | ||
|
||
{{- define "fruit row" -}} | ||
<tr> | ||
<td>{{ .Name }}</td> | ||
<td id="count" hx-get='/fruits/{{.Name}}/edit'>{{ .Value }}</td> | ||
</tr> | ||
{{- end -}} | ||
|
||
{{range .}} | ||
{{template "fruit row" .}} | ||
{{end}} | ||
|
||
{{- define "GET /{$} List(ctx)" -}} | ||
{{template "index.gohtml" .}} | ||
{{- end -}} | ||
|
||
{{- define "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" -}} | ||
<tr> | ||
<td>{{ .Row.Name }}</td> | ||
<td> | ||
<form hx-patch='/fruits/{{.Row.Name}}'> | ||
<input aria-label='Count' type='number' name='count' value='{{ .Row.Value }}' step='1' min='0'> | ||
<input type='submit' value='Update'> | ||
</form> | ||
<p id='error'>{{.Error}}</p> | ||
</td> | ||
</tr> | ||
{{- end -}} | ||
|
||
{{- define "PATCH /fruits/{fruit} SubmitFormEditRow(request, fruit)" }} | ||
{{- if .Error -}} | ||
{{template "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" .}} | ||
{{- else -}} | ||
{{template "fruit row" .Row}} | ||
{{- end -}} | ||
{{ end -}} | ||
|
||
</tbody> | ||
</table> | ||
</main> | ||
</body> | ||
</html> | ||
|
||
{{define "GET /help"}} | ||
<!DOCTYPE html> | ||
<html lang='us-en'> | ||
{{template "head" "Help"}} | ||
<body> | ||
<main class='container'> | ||
Hello, help! | ||
</main> | ||
</body> | ||
</html> | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
//go:embed *.gohtml | ||
var templateSource embed.FS | ||
|
||
var templates = template.Must(template.ParseFS(templateSource, "*")) | ||
|
||
type Backend struct { | ||
data []Row | ||
} | ||
|
||
type EditRowPage struct { | ||
Row Row | ||
Error error | ||
} | ||
|
||
func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage { | ||
count, err := strconv.Atoi(request.FormValue("count")) | ||
if err != nil { | ||
return EditRowPage{Error: err, Row: Row{Name: fruit}} | ||
} | ||
for i := range b.data { | ||
if b.data[i].Name == fruit { | ||
b.data[i].Value = count | ||
return EditRowPage{Error: nil, Row: b.data[i]} | ||
} | ||
} | ||
return EditRowPage{Error: fmt.Errorf("fruit not found")} | ||
} | ||
|
||
func (b *Backend) GetFormEditRow(fruit string) EditRowPage { | ||
for i := range b.data { | ||
if b.data[i].Name == fruit { | ||
return EditRowPage{Error: nil, Row: b.data[i]} | ||
} | ||
} | ||
return EditRowPage{Error: fmt.Errorf("fruit not found")} | ||
} | ||
|
||
type Row struct { | ||
Name string | ||
Value int | ||
} | ||
|
||
func (b *Backend) List(_ context.Context) []Row { return b.data } | ||
|
||
//go:generate muxt generate --receiver Backend | ||
|
||
func main() { | ||
backend := &Backend{ | ||
data: []Row{ | ||
{Name: "Peach", Value: 10}, | ||
{Name: "Plum", Value: 20}, | ||
{Name: "Pineapple", Value: 2}, | ||
}, | ||
} | ||
mux := http.NewServeMux() | ||
Routes(mux, backend) | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.