all repos

onasty @ b59aa76595abf2171bde3a5a97551f1f008aa622

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
web: create note page (#144)..., 11 months ago
1
module Api.Note exposing (create)
2
3
import Api
4
import Data.Note as Note exposing (CreateResponse)
5
import Effect exposing (Effect)
6
import Http
7
import ISO8601
8
import Json.Encode as E
9
import Time exposing (Posix)
10
11
12
create :
13
    { onResponse : Result Api.Error CreateResponse -> msg
14
    , content : String
15
    , slug : Maybe String
16
    , password : Maybe String
17
    , expiresAt : Posix
18
    , burnBeforeExpiration : Bool
19
    }
20
    -> Effect msg
21
create options =
22
    let
23
        body : E.Value
24
        body =
25
            E.object
26
                [ ( "content", E.string options.content )
27
                , case options.slug of
28
                    Just slug ->
29
                        ( "slug", E.string slug )
30
31
                    Nothing ->
32
                        ( "slug", E.null )
33
                , case options.password of
34
                    Just password ->
35
                        ( "password", E.string password )
36
37
                    Nothing ->
38
                        ( "password", E.null )
39
                , ( "burn_before_expiration", E.bool options.burnBeforeExpiration )
40
                , if options.expiresAt == Time.millisToPosix 0 then
41
                    ( "expires_at", E.null )
42
43
                  else
44
                    ( "expires_at"
45
                    , options.expiresAt
46
                        |> ISO8601.fromPosix
47
                        |> ISO8601.toString
48
                        |> E.string
49
                    )
50
                ]
51
    in
52
    Effect.sendApiRequest
53
        { endpoint = "/api/v1/note"
54
        , method = "POST"
55
        , body = Http.jsonBody body
56
        , onResponse = options.onResponse
57
        , decoder = Note.decodeCreateResponse
58
        }