/
ContactForm.elm
155 lines (121 loc) · 4.58 KB
/
ContactForm.elm
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
module Doc.ContactForm exposing (Model, Msg, init, send, update, view)
import Html exposing (Html, a, br, button, div, form, input, label, p, small, span, text, textarea)
import Html.Attributes exposing (checked, class, for, href, id, name, placeholder, readonly, rows, type_, value)
import Html.Events exposing (onClick, onInput, onSubmit)
import Http
import Json.Encode as Enc
import SharedUI exposing (modalWrapper)
import Translation exposing (Language, TranslationId(..), tr)
import Url.Builder exposing (string, toQuery)
-- MODEL
type alias Model =
{ fromField : String
, bodyField : String
, subjectField : String
, requestType : RequestType
}
type RequestType
= Standard
| Urgent
init : String -> Model
init email =
{ fromField = email
, bodyField = ""
, subjectField = "Could you help me with this?"
, requestType = Standard
}
-- UPDATE
type Msg
= UpdateFromField String
| UpdateSubjectField String
| UpdateBodyField String
| UpdateRequestType RequestType
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateFromField newStr ->
{ model | fromField = newStr }
UpdateSubjectField newStr ->
{ model | subjectField = newStr }
UpdateBodyField newStr ->
{ model | bodyField = newStr }
UpdateRequestType newReqType ->
{ model | requestType = newReqType }
send : (Result Http.Error () -> msg) -> Model -> Cmd msg
send msg model =
Http.post
{ body = Http.jsonBody (toValue model)
, expect = Http.expectWhatever msg
, url = "/pleasenospam"
}
toValue : Model -> Enc.Value
toValue model =
Enc.object
[ ( "toEmail", Enc.string <| supportEmailString model.requestType )
, ( "fromEmail", Enc.string model.fromField )
, ( "subject", Enc.string model.subjectField )
, ( "body", Enc.string model.bodyField )
]
-- VIEW
view : Language -> { closeMsg : msg, submitMsg : Model -> msg, tagger : Msg -> msg, copyEmail : Bool -> msg } -> Model -> List (Html msg)
view lang { closeMsg, submitMsg, tagger, copyEmail } model =
[ form [ id "contact-form", onSubmit <| submitMsg model ]
[ input
[ id "contact-from-email"
, type_ "email"
, value model.fromField
, placeholder "Your Email"
, onInput <| tagger << UpdateFromField
]
[]
, input [ id "contact-subject", value model.subjectField, onInput <| tagger << UpdateSubjectField ] []
, textarea [ id "contact-body", onInput <| tagger << UpdateBodyField, rows 7 ] []
, div []
[ input
[ id "contact-urgency-standard"
, name "contact-urgency"
, type_ "radio"
, value "standard"
, onClick <| tagger <| UpdateRequestType Standard
, checked (model.requestType == Standard)
]
[]
, label [ for "contact-urgency-standard" ] [ span [] [ text "Standard Request " ], span [ id "std-req-info", class "extra-info" ] [ text " (Checked every Wednesday)" ] ]
]
, div []
[ input
[ id "contact-urgency-urgent"
, name "contact-urgency"
, type_ "radio"
, value "urgent"
, onClick <| tagger <| UpdateRequestType Urgent
, checked (model.requestType == Urgent)
]
[]
, label [ for "contact-urgency-urgent" ] [ span [] [ text "Urgent Request " ], span [ id "urg-req-info", class "extra-info" ] [ text " (Notification to Developer's Phone)" ] ]
]
, br [] []
, button [ id "contact-send", type_ "submit" ] [ text "Send Now" ]
]
, p [] [ text "or" ]
, p []
[ text "send an email instead : "
, br [] []
, a [ href <| mailto model ] [ text <| supportEmailString model.requestType ]
, small [ id "email-copy-btn", onClick <| copyEmail (model.requestType == Urgent) ] [ text "copy" ]
]
]
|> modalWrapper closeMsg Nothing (Just [ ( "red-alert", model.requestType == Urgent ) ]) (tr lang EmailSupport)
supportEmailString req =
case req of
Standard ->
"{%SUPPORT_EMAIL%}"
Urgent ->
"{%SUPPORT_URGENT_EMAIL%}"
mailto : Model -> String
mailto model =
("mailto:" ++ supportEmailString model.requestType)
++ toQuery
[ string "subject" model.subjectField
, string "body" model.bodyField
]