66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"html"
|
|
|
|
"git.dev.alexdunmow.com/block/pluginsdk/blocks"
|
|
"git.dev.alexdunmow.com/block/pluginsdk/plugin"
|
|
"git.dev.alexdunmow.com/block/pluginsdk/route"
|
|
"git.dev.alexdunmow.com/block/pluginsdk/templates"
|
|
"git.dev.alexdunmow.com/private/quote-requests-a1a5c0/db"
|
|
)
|
|
|
|
const pluginName = "quote-requests-a1a5c0"
|
|
|
|
// Registration is the plugin entry registration.
|
|
var Registration = plugin.PluginRegistration{
|
|
Name: pluginName,
|
|
Version: "1.0.0",
|
|
Register: register,
|
|
HTTPHandler: newHTTPHandler,
|
|
}
|
|
|
|
var current plugin.CoreServices
|
|
|
|
func setServices(deps plugin.CoreServices) { current = deps }
|
|
|
|
func register(_ templates.TemplateRegistry, br blocks.BlockRegistry) error {
|
|
br.Register(blocks.BlockMeta{Key: "quote_request_form", Title: "Quote request form"}, renderForm)
|
|
br.Register(blocks.BlockMeta{Key: "recent_requests", Title: "Recent requests"}, renderRecent)
|
|
return nil
|
|
}
|
|
|
|
func str(content map[string]any, key string) string {
|
|
s, _ := content[key].(string)
|
|
return html.EscapeString(s)
|
|
}
|
|
|
|
func renderForm(_ context.Context, content map[string]any) string {
|
|
button := str(content, "button_label")
|
|
if button == "" {
|
|
button = "Send"
|
|
}
|
|
return `<section class="quote-request-form"><h2>` + str(content, "heading") + `</h2>` +
|
|
`<form method="post" action="/api/plugins/` + pluginName + `/submit">` +
|
|
`<input name="name"><input name="email" type="email"><input name="property_address">` +
|
|
`<textarea name="roof_issue"></textarea>` + route.CaptchaWidget() + `<button type="submit">` + button + `</button></form></section>`
|
|
}
|
|
|
|
// renderRecent lists the latest requests that visitors submitted.
|
|
func renderRecent(ctx context.Context, _ map[string]any) string {
|
|
dbtx, ok := current.Pool.(db.DBTX)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
rows, err := db.New(dbtx).ListQuoteRequests(ctx)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
out := "<ul>"
|
|
for _, row := range rows {
|
|
out += "<li>" + html.EscapeString(row.Name) + "</li>"
|
|
}
|
|
return out + "</ul>"
|
|
}
|