-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin.fs
236 lines (214 loc) · 10.9 KB
/
Admin.fs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
module Demo911App.Admin
open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Formlet
open IntelliFactory.WebSharper.Html
open IntelliFactory.WebSharper.Html5
open IntelliFactory.WebSharper.Sitelets
open IntelliFactory.WebSharper.Web
open IntelliFactory.WebSharper.JQuery
open Helpers
type LoginInfo = { UserName: string; Password: string }
type LoginControl() =
inherit Control()
[<JavaScript>]
let LoginForm() =
let uName =
Controls.Input ""
|> Validator.IsNotEmpty "Enter Username"
|> Enhance.WithTextLabel "Username"
let pw =
Controls.Password ""
|> Validator.IsNotEmpty "Enter Password"
|> Enhance.WithTextLabel "Password"
Formlet.Yield (fun n pw -> { UserName=n; Password=pw })
<*> uName <*> pw
|> Enhance.WithCustomSubmitButton
({Enhance.FormButtonConfiguration.Default with Label=Some("Login"); Style=Some("background-color:#04C;color:#fff; margin-left:78px; padding:6px; width:262px")})
|> Enhance.WithFormContainer
[<JavaScript>]
let doLogin user pass (label: Element) = async {
try
let! res = Api.Admin.Login user pass
if res.Success then
let splt = res.Data.Split(':')
Api.Vars.SetAccountId splt.[0]
Api.Vars.SetApiKey splt.[1]
Api.Vars.SetIsAdmin "true"
Api.Vars.SetAccountName ""
label.Text <- ""
()
else
label.Text <- "Invalid username or password."
with ex -> label.Text <- ex.Message
}
[<JavaScript>]
override this.Body =
let errorBox = Div[Attr.Class "alert alert-error span hide"; Attr.Style "margin-left:92px; margin-bottom:12px;"]
let errorLabel = Label[Attr.Class ""]
let l = LoginForm().Run(fun li -> Async.Start (async {
JE(errorBox).Hide() |> ignore
do! doLogin li.UserName li.Password errorLabel
if errorLabel.Text = "" then redirect Api.Action.AdminDashboard
else JE(errorBox).Show() |> ignore
}
))
JS("document").Ready(fun _ ->
JS(".formlet").Keyup(fun _ k -> if k.Which = 13 then JS(".submitButton").Click() |> ignore) |> ignore
JS(".nav").Hide() |> ignore
JS(".inputText").First().Focus() |> ignore
) |> ignore
upcast (Div [Attr.Class "well padT2"]
-< [H4[Text "Admin Login"; Attr.Class "padL blue"]]
-< [l]
-< [errorBox -< [errorLabel]; Cl()])
type LogoutControl() =
inherit Control()
[<JavaScript>]
override this.Body =
let logoutBox = Div[Text "Logging out..."; Attr.Class "gray bold"]
Async.Start (async{
let! res = Api.Admin.Logout
if not(res.Success) then
let! res2 = Api.Admin.Logout
redirect Api.Action.AdminLogin
redirect Api.Action.AdminLogin
}
)
upcast (Div [logoutBox])
type AccountsControl() =
inherit Control()
[<JavaScript>]
let ImpersonateControl() =
//Impersonate fields
let parent = Div[]
let AccountId = Dropdown "" "Customer Account to log in as" "" "input-xlarge" ["","- Select -"]
let btn = Button[Text "Go"; Attr.Class "btn btn-success"; Attr.Style "margin-top:20px;"]
//get data
let accounts = ref null
Async.Start(
async {
let! res = Api.Admin.ListAccounts()
if res.Success then accounts := res.Data
else ErrorAlert "Unable to load Accounts"
let accountsDrop = JD(AccountId.Dom.LastChild)
!accounts |> Array.iter (fun (x: Api.Account) ->
accountsDrop.Append("<option" + (if x.AccountId.ToString() = Api.Vars.AccountId then "selected='selected'" else "") +
" value='"+(x.AccountId.ToString())+"'>"+x.ContactCompany + " (" + x.Email + ")</option>") |> ignore)
}
)
//js
JE(btn).Click(fun _ _ ->
Api.Vars.SetAccountName (JD(AccountId.Dom.LastChild).Find("option:selected").Text())
Api.Vars.SetAccountId (JD(AccountId.Dom.LastChild).Find("option:selected").Val())
redirect Api.Action.Dashboard
) |> ignore
parent -< [B[Text "Login as customer account: ";Attr.Class "green left padR"; Attr.Style "font-size:1.2em;margin-top:25px;"]; AccountId; btn;]
[<JavaScript>]
override this.Body =
let parent = Div[Attr.Class "well up6";]
//account fields
let ContactName = TextboxTip "Name" "Required" "input-medium" ""
let ContactCompany = TextboxTip "Company" "Required" "input-large" ""
let Email = TextboxTip "Email" "Required" "input-medium" ""
let ContactAddress = TextboxTip "Address" "Required" "" ""
let ContactCity = TextboxTip "City" "Required" "" ""
let ContactState = TextboxTip "State" "Required" "input-mini" ""
let ContactPostal = TextboxTip "Postal" "Required" "input-small" ""
let ContactCountry = Dropdown "Country" "Required" "" "input-medium" ["US","United States"; "CA","Canada";]
let saveBtn = Button[Text "Save Changes"; Attr.Class "btn btn-primary"]
let msgHolder = Div[]
//TODO get data
let tableHolder = Div[]
let accounts = ref null
Async.Start(
async {
let! res = Api.Admin.ListAccounts()
if res.Success then accounts := res.Data
else ErrorAlert "Unable to load accounts"
//table
let rows =
!accounts |> Array.map(fun x ->
let impBtn = Button[Text "Login as Customer"; Attr.Class "btn btn-success"]
JE(impBtn).Click(fun _ _ ->
Api.Vars.SetAccountId <| x.AccountId.ToString()
Api.Vars.SetAccountName (x.ContactCompany + "("+x.Email+")")
redirect Api.Action.Dashboard
) |> ignore
TR[
TD[Text <| x.AccountId.ToString()]
TD[Text <| screenUndefined x.ContactCompany]
TD[Text <| screenUndefined x.Email]
TD[Text <| screenUndefined x.ContactName]
TD[Text <| screenUndefined (formatDate x.CreatedOn)]
TD[impBtn]
]
)
|> Array.toSeq
let table = Table[Attr.Class "table table-striped"]
-< [TR[TH[Text "ID"];TH[Text "Company"];TH[Text "Email"];TH[Text "Name"];TH[Text "Created"];TH[];]]
-< rows
JE(tableHolder).Append(table.Dom) |> ignore
}
)
//js
let jsaveBtn = JE(saveBtn)
jsaveBtn.Click(fun _ _ ->
Async.Start(
async {
ClearMessageAlerts()
//validate
let valid = Required msgHolder
[
ContactName;
ContactCompany;
Email;
ContactAddress;
ContactCity;
ContactState;
ContactPostal;
ContactCountry;
]
if !valid then
let newAcc = {Api.Account.Empty with
Email = GetInputVal Email;
ContactName = GetInputVal ContactName;
ContactCompany = GetInputVal ContactCompany;
ContactAddress = GetInputVal ContactAddress;
ContactCity = GetInputVal ContactCity;
ContactState = GetInputVal ContactState;
ContactPostal = GetInputVal ContactPostal;
ContactCountry = GetInputVal ContactCountry;
CrmAccountId = "";
}
let! res = Api.Admin.CreateAccount newAcc
match res with
| z when z.Success ->
Success "Account created" parent
| z ->
Error ("Unable to create account: "+ z.ErrorInfo + "(" + z.ErrorCode.ToString() + ")") parent
}
)
) |> ignore
//compose
authScreen <|
parent
-< [
H3[Attr.Class "blue"; Text "Create Account"; Attr.Style "margin-top:-6px;";]
ContactName;
ContactCompany;
Email;
Cl();
ContactAddress;
ContactCity;
ContactState;
ContactPostal;
ContactCountry;
Cl();
Br[]
saveBtn;
msgHolder;
Cll();
ImpersonateControl();
Cll();
tableHolder;
]