all repos

onasty @ 083ffe6e4f0106301ed2fa85753f9c645dbdf174

a one-time notes service

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
feat(web): add dashboard (#214)..., 8 months ago
1
module Api.Note exposing (create, delete, get, getAll, getMetadata)
2
3
import Api
4
import Data.Note as Note exposing (CreateResponse, Metadata, Note)
5
import Effect exposing (Effect)
6
import Http
7
import Iso8601
8
import Json.Decode as D
9
import Json.Encode as E
10
import Time exposing (Posix)
11
12
13
create :
14
    { onResponse : Result Api.Error CreateResponse -> msg
15
    , content : String
16
    , slug : Maybe String
17
    , password : Maybe String
18
    , expiresAt : Posix
19
    , keepBeforeExpiration : Bool
20
    }
21
    -> Effect msg
22
create options =
23
    let
24
        encodeMaybe : String -> (a -> E.Value) -> Maybe a -> ( String, E.Value )
25
        encodeMaybe field value maybe =
26
            case maybe of
27
                Just data ->
28
                    ( field, value data )
29
30
                Nothing ->
31
                    ( field, E.null )
32
33
        body =
34
            E.object
35
                [ ( "content", E.string options.content )
36
                , encodeMaybe "slug" E.string options.slug
37
                , encodeMaybe "password" E.string options.password
38
                , ( "keep_before_expiration", E.bool options.keepBeforeExpiration )
39
                , if options.expiresAt == Time.millisToPosix 0 then
40
                    ( "expires_at", E.null )
41
42
                  else
43
                    ( "expires_at", options.expiresAt |> Iso8601.fromTime |> E.string )
44
                ]
45
    in
46
    Effect.sendApiRequest
47
        { endpoint = "/api/v1/note"
48
        , method = "POST"
49
        , body = Http.jsonBody body
50
        , onResponse = options.onResponse
51
        , decoder = Note.decodeCreateResponse
52
        }
53
54
55
get :
56
    { onResponse : Result Api.Error Note -> msg
57
    , slug : String
58
    , password : Maybe String
59
    }
60
    -> Effect msg
61
get options =
62
    case options.password of
63
        Just passwd ->
64
            Effect.sendApiRequest
65
                { endpoint = "/api/v1/note/" ++ options.slug ++ "/view"
66
                , method = "POST"
67
                , body = E.object [ ( "password", E.string passwd ) ] |> Http.jsonBody
68
                , onResponse = options.onResponse
69
                , decoder = Note.decode
70
                }
71
72
        Nothing ->
73
            Effect.sendApiRequest
74
                { endpoint = "/api/v1/note/" ++ options.slug
75
                , method = "GET"
76
                , body = Http.emptyBody
77
                , onResponse = options.onResponse
78
                , decoder = Note.decode
79
                }
80
81
82
delete : { onResponse : Result Api.Error () -> msg, slug : String } -> Effect msg
83
delete options =
84
    Effect.sendApiRequest
85
        { endpoint = "/api/v1/note/" ++ options.slug
86
        , method = "DELETE"
87
        , body = Http.emptyBody
88
        , onResponse = options.onResponse
89
        , decoder = D.succeed ()
90
        }
91
92
93
getMetadata :
94
    { onResponse : Result Api.Error Metadata -> msg
95
    , slug : String
96
    }
97
    -> Effect msg
98
getMetadata options =
99
    Effect.sendApiRequest
100
        { endpoint = "/api/v1/note/" ++ options.slug ++ "/meta"
101
        , method = "GET"
102
        , body = Http.emptyBody
103
        , onResponse = options.onResponse
104
        , decoder = Note.decodeMetadata
105
        }
106
107
108
getAll : { onResponse : Result Api.Error (List Note) -> msg } -> Effect msg
109
getAll opts =
110
    Effect.sendApiRequest
111
        { endpoint = "/api/v1/note"
112
        , method = "GET"
113
        , body = Http.emptyBody
114
        , onResponse = opts.onResponse
115
        , decoder = D.list Note.decode
116
        }