tangled
alpha
login
or
join now
kacaii.dev
/
sigo
0
fork
atom
๐ฉโ๐ Firefighters API written in Gleam!
lustre
gleam
0
fork
atom
overview
issues
pulls
pipelines
:label: add request builder for the client page
kacaii.dev
2 weeks ago
024126b3
9aa3ffbc
verified
This commit was signed with the committer's
known signature
.
kacaii.dev
SSH Key Fingerprint:
SHA256:7b2TlnwcI/GLZ86tUYFGlDuG0t1aTKF1fB3PJj5umRQ=
+71
-5
2 changed files
expand all
collapse all
unified
split
client
src
client
page
signup.gleam
shared
src
shared
contract
signup.gleam
+34
-5
client/src/client/page/signup.gleam
···
9
9
import lustre/element/html
10
10
import lustre/event
11
11
import rsvp
12
12
+
import shared/contract/signup as contract
12
13
import shared/role
13
14
import shared/session
14
15
···
33
34
user_is_active: Bool,
34
35
loading: Bool,
35
36
text_message: String,
37
37
+
)
38
38
+
}
39
39
+
40
40
+
fn build_request(model: Model) -> contract.RequestBody {
41
41
+
contract.RequestBody(
42
42
+
name: model.user_name,
43
43
+
role: model.user_role,
44
44
+
email: model.user_email,
45
45
+
password: model.user_password,
46
46
+
confirm_password: model.user_confirm_password,
47
47
+
is_active: model.user_is_active,
36
48
)
37
49
}
38
50
···
88
100
}
89
101
90
102
UserSentRequest -> {
91
91
-
todo as "build and send request"
103
103
+
let body =
104
104
+
build_request(model)
105
105
+
|> contract.request_to_json()
106
106
+
107
107
+
let effect =
108
108
+
rsvp.expect_ok_response(ServerSentResponse)
109
109
+
|> rsvp.post("api/signup", body, _)
92
110
93
93
-
let model = Model(..model, loading: True)
94
94
-
#(model, todo as "rsvp_effect")
111
111
+
#(Model(..model, loading: True), effect)
95
112
}
96
113
97
97
-
ServerSentResponse(Ok(_)) -> todo
98
98
-
ServerSentResponse(Error(reason)) -> todo
114
114
+
ServerSentResponse(Ok(resp)) -> #(
115
115
+
Model(..model, loading: False, text_message: resp.body),
116
116
+
effect.none(),
117
117
+
)
118
118
+
119
119
+
ServerSentResponse(Error(reason)) -> {
120
120
+
let text_message = case reason {
121
121
+
rsvp.HttpError(resp) -> resp.body
122
122
+
rsvp.NetworkError -> "Connection not available"
123
123
+
_ -> "An error occurred"
124
124
+
}
125
125
+
126
126
+
#(Model(..model, loading: False, text_message:), effect.none())
127
127
+
}
99
128
}
100
129
}
101
130
+37
shared/src/shared/contract/signup.gleam
···
1
1
+
import gleam/dynamic/decode
2
2
+
import gleam/json
3
3
+
import shared/role
4
4
+
5
5
+
pub type RequestBody {
6
6
+
RequestBody(
7
7
+
name: String,
8
8
+
role: role.Role,
9
9
+
email: String,
10
10
+
password: String,
11
11
+
confirm_password: String,
12
12
+
is_active: Bool,
13
13
+
)
14
14
+
}
15
15
+
16
16
+
pub fn request_to_json(body: RequestBody) -> json.Json {
17
17
+
json.object([
18
18
+
#("name", json.string(body.name)),
19
19
+
#("role", role.to_json(body.role)),
20
20
+
#("email", json.string(body.email)),
21
21
+
#("password", json.string(body.password)),
22
22
+
#("confirm_password", json.string(body.confirm_password)),
23
23
+
#("is_active", json.bool(body.is_active)),
24
24
+
])
25
25
+
}
26
26
+
27
27
+
pub fn request_decoder() -> decode.Decoder(RequestBody) {
28
28
+
use name <- decode.field("name", decode.string)
29
29
+
use role <- decode.field("role", role.decoder())
30
30
+
use email <- decode.field("email", decode.string)
31
31
+
use password <- decode.field("password", decode.string)
32
32
+
use confirm_password <- decode.field("confirm_password", decode.string)
33
33
+
use is_active <- decode.field("is_active", decode.bool)
34
34
+
35
35
+
RequestBody(name:, role:, email:, password:, confirm_password:, is_active:)
36
36
+
|> decode.success
37
37
+
}