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 pacesmain
parent
20a5bb0db2
commit
45d25d8990
30
main.go
30
main.go
|
@ -129,7 +129,35 @@ func main() {
|
||||||
Body: email_content,
|
Body: email_content,
|
||||||
Subject: "This is a test subject", // TODO(jack): Get from user input on page
|
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
|
// Endpoint called on load or button press by htmx to retrieve and populate the mailing list
|
||||||
|
|
12
postmark.go
12
postmark.go
|
@ -64,6 +64,11 @@ type ValidationResponse struct {
|
||||||
SuggestedTemplateModel interface{}
|
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
|
// 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 {
|
func getPostmarkTemplate(cfg PostmarkConfig) PostmarkTemplate {
|
||||||
// Get Template
|
// 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
|
// 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{
|
bodyObj := ValidateTemplateBody{
|
||||||
Subject: template.Subject,
|
Subject: template.Subject,
|
||||||
HTMLBody: template.HtmlBody,
|
HTMLBody: template.HtmlBody,
|
||||||
|
@ -131,5 +136,8 @@ func renderPostmarkTemplate(cfg PostmarkConfig, template PostmarkTemplate, model
|
||||||
panic(decode_error)
|
panic(decode_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.HtmlBody.RenderedContent
|
return Email{
|
||||||
|
Html: response.HtmlBody.RenderedContent,
|
||||||
|
Subject: response.Subject.RenderedContent,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,8 @@
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<div style="display: flex; justify-content: space-between;">
|
<div style="display: flex; justify-content: space-between;">
|
||||||
<span class="input_label">Subject:</span>
|
<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>
|
</div><br>
|
||||||
<textarea name="email_input" id="typebox" rows="32" cols="64" style="resize: none;"
|
<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"
|
hx-trigger="keyup changed delay:1000ms" hx-post="/mail-content" hx-target="#email-preview"
|
||||||
|
@ -30,7 +31,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<div id="subject-preview">Subject preview here</div><br>
|
<div id="subject-preview"> </div><br>
|
||||||
<div id="email-preview"></div>
|
<div id="email-preview"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue