mail contact

This commit is contained in:
Ruidy Nemausat 2020-03-16 19:42:52 +01:00
parent 85af1381ef
commit 80c914238b
5 changed files with 17 additions and 27 deletions

View file

@ -16,21 +16,13 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
}
func contactHandler(w http.ResponseWriter, r *http.Request) {
views.Template(w, "contact", nil)
}
func postContactHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkError(err, w) // bad error handling
mail := parseContactForm(r)
// mail.Send()
if r.Method != http.MethodPost {
views.Template(w, "contact", nil)
return
}
mail := services.NewMail(r.PostFormValue("email"), r.PostFormValue("message"))
log.Println(mail)
views.Template(w, "contact_sent", nil)
}
func parseContactForm(r *http.Request) services.Mail {
log.Println(r.PostForm)
return services.NewMail(r.PostFormValue("email"), r.PostFormValue("message"))
views.Template(w, "contact", struct{ Success bool }{true})
}
func checkError(err error, w http.ResponseWriter) {

View file

@ -12,7 +12,6 @@ func Router() {
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/contact/", contactHandler)
http.HandleFunc("/contact/post/", postContactHandler)
http.HandleFunc("/", homeHandler)
}

View file

@ -5,11 +5,10 @@ import (
"net/http"
"text/template"
"github.com/rjNemo/go-wiki/models"
"github.com/rjNemo/go-wiki/settings"
)
func Template(w http.ResponseWriter, tmpl string, p *models.Page) {
func Template(w http.ResponseWriter, tmpl string, p interface{}) {
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
t, err := template.ParseFiles(getTmplName("base"), getTmplName(tmpl))

View file

@ -1,7 +1,15 @@
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
{{if.Success}}
<h1>Thanks for your mail</h1>
<div class="section">
<a class="btn btn-primary" href="/">Return to Home</a>
</div>
{{else}}
<h1>Contact us</h1>
<form action="/contact/post" method="POST">
<form method="POST">
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" />
@ -14,4 +22,4 @@
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
{{end}}
{{end}}{{end}}

View file

@ -1,8 +0,0 @@
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
<h1>Thanks for your mail</h1>
<div class="section">
<a class="btn btn-primary" href="/">Return to Home</a>
</div>
{{end}}