diff --git a/main.go b/main.go index e3efd12..d8b756f 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/postmark.go b/postmark.go index a934655..83f423c 100644 --- a/postmark.go +++ b/postmark.go @@ -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, + } } diff --git a/templates/index.html b/templates/index.html index 5f1c631..5ec0f3c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,7 +22,8 @@