all repos

onasty @ eb4c605

a one-time notes service

onasty/web/src/Api/Profile.elm (view raw)

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
feat(web): add account settings (#190)..., 9 months ago
1
module Api.Profile exposing (changePassword, me, requestEmailChange)
2
3
import Api
4
import Data.Me as Me exposing (Me)
5
import Effect exposing (Effect)
6
import Http
7
import Json.Decode as Decode
8
import Json.Encode as E
9
10
11
me : { onResponse : Result Api.Error Me -> msg } -> Effect msg
12
me options =
13
    Effect.sendApiRequest
14
        { endpoint = "/api/v1/me"
15
        , method = "GET"
16
        , body = Http.emptyBody
17
        , onResponse = options.onResponse
18
        , decoder = Me.decode
19
        }
20
21
22
requestEmailChange : { onResponse : Result Api.Error () -> msg, newEmail : String } -> Effect msg
23
requestEmailChange { onResponse, newEmail } =
24
    Effect.sendApiRequest
25
        { endpoint = "/api/v1/auth/change-email"
26
        , method = "POST"
27
        , body = E.object [ ( "new_email", E.string newEmail ) ] |> Http.jsonBody
28
        , onResponse = onResponse
29
        , decoder = Decode.succeed ()
30
        }
31
32
33
changePassword : { onResponse : Result Api.Error () -> msg, currentPassword : String, newPassword : String } -> Effect msg
34
changePassword { onResponse, currentPassword, newPassword } =
35
    Effect.sendApiRequest
36
        { endpoint = "/api/v1/auth/change-password"
37
        , method = "POST"
38
        , body =
39
            Http.jsonBody <|
40
                E.object
41
                    [ ( "current_password", E.string currentPassword )
42
                    , ( "new_password", E.string newPassword )
43
                    ]
44
        , onResponse = onResponse
45
        , decoder = Decode.succeed ()
46
        }