Skip to content

Commit

Permalink
Cleaned up static view to match editable view. Added null initializat…
Browse files Browse the repository at this point in the history
…ion function.
  • Loading branch information
Dr. Bearhands committed Jan 8, 2018
1 parent 63264c0 commit 2b46a08
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
34 changes: 24 additions & 10 deletions EditableValue.elm
@@ -1,4 +1,4 @@
module EditableValue exposing (EditableValue, Config, config, encode, decoder, view, editableView)
module EditableValue exposing (EditableValue, Config, null, config, encode, decoder, view, editableView)

{-| This package allows editing JSON files with unknown structure in an Elm view.
This might be necessary e.g. for managing a noSQL server with varying document structures.
Expand All @@ -7,6 +7,9 @@ This might be necessary e.g. for managing a noSQL server with varying document s
@docs EditableValue
@docs Config
# Initialization
@docs null
# Encoding/Decoding
@docs decoder
@docs encode
Expand Down Expand Up @@ -38,6 +41,12 @@ type EditableValue
| JsonBool Bool
| JsonNull

{-|
Constructs a 'null' EditableValue
-}
null : EditableValue
null = JsonNull

{-| Opaque type used for configuring the view functions
-}
type Config =
Expand Down Expand Up @@ -100,15 +109,20 @@ view config editableValue =
JsonObject obj ->
div []
[ text "{"
, ul []
<| List.map
( \(key, val) ->
li []
[ text <| key ++ ": "
, view config val
]
)
( Array.toList obj )
, table [style [("margin-left", "30px")] ]
[ tbody [] <| List.concat
[ Array.toList <| Array.indexedMap
( \ii (key, σ) ->
tr []
[ td [style [("vertical-align", "top")]]
[ text <| key ++ ": "
, view config σ
]
]
)
obj
]
]
, text "}"
]
JsonBool bool -> text <| toString bool
Expand Down
62 changes: 30 additions & 32 deletions example.elm
@@ -1,54 +1,52 @@
module Main exposing (main)

import EditableValue exposing (..)
import EditableValue exposing (EditableValue)

import Http
import Json.Encode exposing (..)
import Json.Decode exposing (decodeString)
import Html exposing (..)

type alias Model =
{ editableValue : Maybe EditableValue
{ editableValue : EditableValue
}

type Msg
= ReceivedJson (Result Http.Error EditableValue)
| SetEditableValue EditableValue

main : Program Never Model Msg
main : Program Never Model EditableValue
main = Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}

init : (Model, Cmd Msg)
init : (Model, Cmd EditableValue)
init =
let
url = "http://echo.jsontest.com/key/value/otherkey/othervalue"
request = Http.get url decoder
requestAdvertList msg = Http.send msg request
jsonString = "{\"foo\": \"foo\", \"bar\": {\"baz\":[1,2,3,4]}}"
editableValue =
case decodeString EditableValue.decoder jsonString of
Ok editableValue -> editableValue
Err err -> EditableValue.null
in
( { editableValue = Nothing }
, requestAdvertList ReceivedJson
( { editableValue = editableValue }
, Cmd.none
)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
ReceivedJson (Ok editableValue) -> ({model | editableValue = Just <| editableValue}, Cmd.none)
ReceivedJson (Err err) -> Debug.crash <| toString err
SetEditableValue newViewer -> ({model | editableValue = Just newViewer}, Cmd.none)
update : EditableValue -> Model -> (Model, Cmd EditableValue)
update newValue model =
({model | editableValue = newValue}, Cmd.none)

view : Model -> Html Msg
view : Model -> Html EditableValue
view model =
case model.editableValue of
Just editableValue ->
div []
[ Html.map SetEditableValue (editableView config editableValue)
, br [] []
, br [] []
, text <| toString <| encode editableValue
]
Nothing -> div [] []

subscriptions : Model -> Sub Msg
let
editableValue = model.editableValue
in
div []
[ EditableValue.view EditableValue.config editableValue
, br [] []
, br [] []
, text <| toString <| EditableValue.encode editableValue
]


subscriptions : Model -> Sub EditableValue
subscriptions model = Sub.none

0 comments on commit 2b46a08

Please sign in to comment.