/
contact.go
105 lines (82 loc) · 3.53 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package handlers
import (
"context"
"encoding/json"
"strings"
"time"
"honnef.co/go/js/dom"
"honnef.co/go/js/xhr"
"github.com/EngineerKamesh/igb/igweb/client/common"
"github.com/EngineerKamesh/igb/igweb/shared/cogs/datepicker"
"github.com/EngineerKamesh/igb/igweb/shared/forms"
"github.com/EngineerKamesh/igb/igweb/shared/templatedata"
"go.isomorphicgo.org/go/isokit"
)
func ContactHandler(env *common.Env) isokit.Handler {
return isokit.HandlerFunc(func(ctx context.Context) {
contactForm := forms.NewContactForm(nil)
DisplayContactForm(env, contactForm)
})
}
func handleContactButtonClickEvent(env *common.Env, event dom.Event, contactForm *forms.ContactForm) {
event.PreventDefault()
clientSideValidationResult := contactForm.Validate()
// Even if we disable the client side validation by forcing the validation result to true, the server-side validation
// will kick in and keep the contact form validation in check. Go ahead and try it by uncommenting the following line.
//clientSideValidationResult = true
if clientSideValidationResult == true {
contactFormErrorsChannel := make(chan map[string]string)
go ContactFormSubmissionRequest(contactFormErrorsChannel, contactForm)
go func() {
serverContactFormErrors := <-contactFormErrorsChannel
serverSideValidationResult := len(serverContactFormErrors) == 0
if serverSideValidationResult == true {
env.TemplateSet.Render("contact_confirmation_content", &isokit.RenderParams{Data: nil, Disposition: isokit.PlacementReplaceInnerContents, Element: env.PrimaryContent})
} else {
contactForm.SetErrors(serverContactFormErrors)
contactForm.DisplayErrors()
}
}()
} else {
contactForm.DisplayErrors()
}
}
func ContactFormSubmissionRequest(contactFormErrorsChannel chan map[string]string, contactForm *forms.ContactForm) {
jsonData, err := json.Marshal(contactForm.Fields())
if err != nil {
println("Encountered error: ", err)
return
}
data, err := xhr.Send("POST", "/restapi/contact-form", jsonData)
if err != nil {
println("Encountered error: ", err)
return
}
var contactFormErrors map[string]string
json.NewDecoder(strings.NewReader(string(data))).Decode(&contactFormErrors)
contactFormErrorsChannel <- contactFormErrors
}
func InitializeContactPage(env *common.Env, contactForm *forms.ContactForm) {
formElement := env.Document.GetElementByID("contactForm").(*dom.HTMLFormElement)
contactForm.SetFormParams(&isokit.FormParams{FormElement: formElement})
contactButton := env.Document.GetElementByID("contactButton").(*dom.HTMLInputElement)
contactButton.AddEventListener("click", false, func(event dom.Event) {
handleContactButtonClickEvent(env, event, contactForm)
})
byDate := datepicker.NewDatePicker()
byDate.CogInit(env.TemplateSet)
byDate.SetID("sensitivityDate")
byDate.SetProp("datepickerLabel", "Time Sensitivity Date:")
byDate.SetProp("datepickerInputID", "byDateInput")
byDate.SetProp("datepickerMinDate", time.Now())
byDate.SetProp("datepickerMaxDate", time.Date(2027, 12, 31, 23, 59, 0, 0, time.UTC))
err := byDate.Start()
if err != nil {
println("Encountered the following error when attempting to start the datepicker cog: ", err)
}
}
func DisplayContactForm(env *common.Env, contactForm *forms.ContactForm) {
templateData := &templatedata.Contact{PageTitle: "Contact", Form: contactForm}
env.TemplateSet.Render("contact_content", &isokit.RenderParams{Data: templateData, Disposition: isokit.PlacementReplaceInnerContents, Element: env.PrimaryContent, PageTitle: templateData.PageTitle})
InitializeContactPage(env, contactForm)
}