/
contact.go
45 lines (36 loc) · 1.3 KB
/
contact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package handlers
import (
"net/http"
"github.com/EngineerKamesh/igb/igweb/common"
"github.com/EngineerKamesh/igb/igweb/shared/forms"
"github.com/EngineerKamesh/igb/igweb/shared/templatedata"
"github.com/EngineerKamesh/igb/igweb/submissions"
"go.isomorphicgo.org/go/isokit"
)
func ContactHandler(env *common.Env) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
formParams := isokit.FormParams{ResponseWriter: w, Request: r}
contactForm := forms.NewContactForm(&formParams)
switch r.Method {
case "GET":
DisplayContactForm(env, contactForm)
case "POST":
validationResult := contactForm.Validate()
if validationResult == true {
submissions.ProcessContactForm(env, contactForm)
DisplayConfirmation(env, w, r)
} else {
DisplayContactForm(env, contactForm)
}
default:
DisplayContactForm(env, contactForm)
}
})
}
func DisplayContactForm(env *common.Env, contactForm *forms.ContactForm) {
templateData := &templatedata.Contact{PageTitle: "Contact", Form: contactForm}
env.TemplateSet.Render("contact_page", &isokit.RenderParams{Writer: contactForm.FormParams().ResponseWriter, Data: templateData})
}
func DisplayConfirmation(env *common.Env, w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/contact-confirmation", 302)
}