all repos

onasty @ 1994b67

a one-time notes service

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

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
module Api.Note exposing (create)

import Api
import Data.Note as Note exposing (CreateResponse)
import Effect exposing (Effect)
import Http
import ISO8601
import Json.Encode as E
import Time exposing (Posix)


create :
    { onResponse : Result Api.Error CreateResponse -> msg
    , content : String
    , slug : Maybe String
    , password : Maybe String
    , expiresAt : Posix
    , burnBeforeExpiration : Bool
    }
    -> Effect msg
create options =
    let
        body : E.Value
        body =
            E.object
                [ ( "content", E.string options.content )
                , case options.slug of
                    Just slug ->
                        ( "slug", E.string slug )

                    Nothing ->
                        ( "slug", E.null )
                , case options.password of
                    Just password ->
                        ( "password", E.string password )

                    Nothing ->
                        ( "password", E.null )
                , ( "burn_before_expiration", E.bool options.burnBeforeExpiration )
                , if options.expiresAt == Time.millisToPosix 0 then
                    ( "expires_at", E.null )

                  else
                    ( "expires_at"
                    , options.expiresAt
                        |> ISO8601.fromPosix
                        |> ISO8601.toString
                        |> E.string
                    )
                ]
    in
    Effect.sendApiRequest
        { endpoint = "/api/v1/note"
        , method = "POST"
        , body = Http.jsonBody body
        , onResponse = options.onResponse
        , decoder = Note.decodeCreateResponse
        }