Code Monkey home page Code Monkey logo

elm-kinto's Introduction

elm-kinto client

Build Status

Kinto client for elm.

Need help? Join the #kinto channel on the freenode irc server, or in the kinto-storage Slack!

Thanks to @luke_dot_js and his elm-http-builder package which this library uses, and which was a huge inspiration for the design (and even for the README ;)

Example

You can find some full examples in src/examples.

import Kinto


-- Define what our Kinto records will look like for a Todo list.


type alias Todo =
    { id : String
    , title : Maybe String
    , description : Maybe String
    , last_modified : Int
    }



-- And how to decode from a Kinto record to a Todo...


decodeTodo : Decode.Decoder Todo
decodeTodo =
    Decode.map4 Todo
        (Decode.field "id" Decode.string)
        (Decode.maybe (Decode.field "title" Decode.string))
        (Decode.maybe (Decode.field "description" Decode.string))
        (Decode.field "last_modified" Decode.int)



-- And how to encode Todo data to send to Kinto.


encodeData : Maybe String -> Maybe String -> Encode.Value
encodeData title description =
    Encode.object
        [ ( "title", Encode.string (Maybe.withDefault "" title) )
        , ( "description", Encode.string (Maybe.withDefault "" description) )
        ]



-- Configure a Kinto client: the server url and authentication


client : Kinto.Client
client =
    Kinto.client
        "https://kinto.dev.mozaws.net/v1/"
        (Kinto.Basic "test" "test")



-- Set up the resource we're interested in: in this case, Kinto records, stored
-- in the "test-items" collection, in the "default" bucket.
-- We give it the `decodeTodo` decoder so the data we get back from the Kinto
-- server will be properly decoded into our `Todo` record.


recordResource : Kinto.Resource Todo
recordResource =
    Kinto.recordResource "default" "test-items" decodeTodo



--

type Msg
    = TodoAdded (Result Kinto.Error Todo)
    | TodosFetched (Result Kinto.Error (Kinto.Pager Todo))


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        TodoAdded (Ok todo) ->
            ( { model | todos = todo :: model.todos }, Cmd.none )

        TodoAdded (Err err) ->
            let
                _ =
                    Debug.log "Error while creating the todo" err
            in
                ( model, Cmd.none )

        TodosFetched (Ok todoPager) ->
            ( { model | todos = todoPager.objects }, Cmd.none )

        TodosFetched (Err err) ->
            let
                _ =
                    Debug.log "Error while getting the list of todos" err
            in
                ( model, Cmd.none )



-- Add a new Todo


addTodo : Maybe String -> Maybe String -> Cmd Msg
addTodo title description =
    let
        data =
            encodeData title description
    in
        client
            |> Kinto.create recordResource data TodoAdded
            |> Kinto.send



-- Get all the Todos


getTodoList : Cmd Msg
getTodoList =
    client
        |> Kinto.getList recordResource TodosFetched
        |> Kinto.sort [ "title", "description" ]
        |> Kinto.send

Contributing

We're happy to receive any feedback and ideas for about additional features. Any input and pull requests are very welcome and encouraged. If you'd like to help or have ideas, get in touch with us on irc, slack, email, github or any other mean listed on the kinto webpage!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.