Subject rendering

This isn't the best solution and might not even be worth doing as the subject template doesn't do anything and means i have to call the API in multiple paces
main
Jack Punter 2024-04-22 00:21:36 +01:00
parent 20a5bb0db2
commit 45d25d8990
3 changed files with 43 additions and 6 deletions

30
main.go
View File

@ -129,7 +129,35 @@ func main() {
Body: email_content,
Subject: "This is a test subject", // TODO(jack): Get from user input on page
}
w.Write([]byte(renderPostmarkTemplate(apiConfig.Postmark, postmarkTemplate, model)))
renderedEmail := renderPostmarkTemplate(apiConfig.Postmark, postmarkTemplate, model)
w.Write([]byte(renderedEmail.Html))
})
mux.HandleFunc("/subject_preview", func(w http.ResponseWriter, r *http.Request) {
log.Printf("Hello '%v %v' handler", r.Method, r.URL)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Body.Close()
text_field_body := string(body)
text_field_body, _ = strings.CutPrefix(text_field_body, "subject=")
decoded_subject, err := url.QueryUnescape(text_field_body)
if err != nil {
panic(err)
}
model := PostmarkTemplateModel{
Name: apiConfig.Default.SenderName,
Email: apiConfig.Postmark.SenderEmail,
Body: "",
Subject: decoded_subject, // TODO(jack): Get from user input on page
}
renderedEmail := renderPostmarkTemplate(apiConfig.Postmark, postmarkTemplate, model)
w.Write([]byte(renderedEmail.Subject))
})
// Endpoint called on load or button press by htmx to retrieve and populate the mailing list

View File

@ -64,6 +64,11 @@ type ValidationResponse struct {
SuggestedTemplateModel interface{}
}
type Email struct {
Html string
Subject string
}
// Gets the tempalte in the postmark config section of the app.ini file from teh postmark server so we can render it
func getPostmarkTemplate(cfg PostmarkConfig) PostmarkTemplate {
// Get Template
@ -92,7 +97,7 @@ func getPostmarkTemplate(cfg PostmarkConfig) PostmarkTemplate {
}
// Given a tempalte and model use the postmark tempalte validation api to render the template
func renderPostmarkTemplate(cfg PostmarkConfig, template PostmarkTemplate, model PostmarkTemplateModel) string {
func renderPostmarkTemplate(cfg PostmarkConfig, template PostmarkTemplate, model PostmarkTemplateModel) Email {
bodyObj := ValidateTemplateBody{
Subject: template.Subject,
HTMLBody: template.HtmlBody,
@ -131,5 +136,8 @@ func renderPostmarkTemplate(cfg PostmarkConfig, template PostmarkTemplate, model
panic(decode_error)
}
return response.HtmlBody.RenderedContent
return Email{
Html: response.HtmlBody.RenderedContent,
Subject: response.Subject.RenderedContent,
}
}

View File

@ -22,7 +22,8 @@
<div class="column">
<div style="display: flex; justify-content: space-between;">
<span class="input_label">Subject:</span>
<input name="subject" type="text" id="subject">
<input name="subject" type="text" id="subject" hx-post="/subject_preview"
hx-trigger="keyup changed delay:1000ms" hx-target="#subject-preview">
</div><br>
<textarea name="email_input" id="typebox" rows="32" cols="64" style="resize: none;"
hx-trigger="keyup changed delay:1000ms" hx-post="/mail-content" hx-target="#email-preview"
@ -30,7 +31,7 @@
</div>
<div class="column">
<div id="subject-preview">Subject preview here</div><br>
<div id="subject-preview">&nbsp;</div><br>
<div id="email-preview"></div>
</div>
</div>
@ -40,7 +41,7 @@
<h1>Current Mailing List Subscribers</h1>
</div>
<div class="column">
<button hx-get="/mailing_list" hx-target="#mailing_list"> Refresh Mailing List</button>
<button hx-get="/mailing_list" hx-target="#mailing_list">Refresh Mailing List</button>
</div>
</div>
<div class="card" hx-trigger="load" hx-get="/mailing_list" id="mailing_list"></div>