Created
May 21, 2016 22:26
-
-
Save cdcarter/437840821223ea6d3d09afc41b2d64d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (div, span, strong, text) | |
import Html.Attributes exposing (href) | |
import Html.App | |
import Html.Events | |
import SObject | |
type alias Model = | |
{ sobject : SObject.Model | |
} | |
init : Model | |
init = | |
{ sobject = SObject.init "" | |
} | |
type Msg | |
= Obj SObject.Msg | |
| Change String | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update message model = | |
case message of | |
Obj msg -> | |
let | |
( obj, effect ) = | |
SObject.update msg model.sobject | |
in | |
( { model | sobject = obj }, Cmd.map Obj effect ) | |
Change str -> | |
( { model | sobject = SObject.init str }, Cmd.none ) | |
view : Model -> Html.Html Msg | |
view model = | |
div [] | |
[ div [] | |
[ Html.h1 [] [ text "SObject Viewer" ] | |
, text "" | |
, Html.a [ href "http://github.com/cdcarter/sobs" ] [ text "view on github" ] | |
] | |
, div [] | |
[ Html.input | |
[ Html.Attributes.placeholder "ObjectName" | |
, Html.Events.onInput Change | |
, Html.Events.onBlur (Obj SObject.helpLoad) | |
] | |
[] | |
] | |
, div [] [ Html.App.map Obj (SObject.view model.sobject) ] | |
] | |
main = | |
Html.App.program | |
{ init = ( init, Cmd.none ) | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SObject exposing (Model, Msg, helpLoad, init, update, view) | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onClick) | |
import Html.App | |
import Http | |
import Json.Decode as Json | |
import Task | |
import Field | |
-- Model | |
type alias Model = | |
{ name : String | |
, label : String | |
, custom : Bool | |
, labelPlural : String | |
, customSetting : Bool | |
, fields : List Field.Model | |
} | |
-- init | |
init : String -> Model | |
init name = | |
{ name = name | |
, label = "" | |
, custom = True | |
, labelPlural = "" | |
, customSetting = False | |
, fields = [] | |
} | |
-- update | |
-- Just passing a Msg up the chain?! | |
helpLoad : Msg | |
helpLoad = | |
Called | |
type Msg | |
= N | |
| Called | |
| FetchSucceed Model | |
| FetchFail Http.Error | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Called -> | |
( model, getSObject model.name ) | |
N -> | |
( model, Cmd.none ) | |
FetchSucceed obj -> | |
( obj, Cmd.none ) | |
FetchFail err -> | |
( model, Cmd.none ) | |
getSObject : String -> Cmd Msg | |
getSObject name = | |
let | |
url = | |
"https://l8fii0ljb8.execute-api.us-west-2.amazonaws.com/dev/metadata/object/" ++ name | |
in | |
Task.perform FetchFail FetchSucceed (Http.get decodeSObject url) | |
decodeSObject : Json.Decoder Model | |
decodeSObject = | |
Json.object6 Model | |
(Json.at [ "name" ] Json.string) | |
(Json.at [ "label" ] Json.string) | |
(Json.at [ "custom" ] Json.bool) | |
(Json.at [ "labelPlural" ] Json.string) | |
(Json.at [ "customSetting" ] Json.bool) | |
(Json.at [ "fields" ] (Json.list Field.decode)) | |
-- view | |
view : Model -> Html Msg | |
view model = | |
if model.labelPlural == "" then text "" else render model | |
render : Model -> Html Msg | |
render model = | |
let | |
fieldRows = | |
List.map (Field.view N) model.fields | |
fieldHeader = | |
tr [] [ th [] [ text "Name" ], th [] [ text "Type" ] ] | |
in | |
div [ ] | |
[ h1 [] [ text model.labelPlural ] | |
, table [] | |
[ tr [] [ td [] [ text "Custom?" ], td [] [ text (toString model.custom) ] ] | |
, tr [] [ td [] [ text "Custom Setting?" ], td [] [ text (toString model.customSetting) ] ] | |
, tr [] [ td [] [ text "API Name" ], td [] [ text model.name ] ] | |
] | |
, hr [] [] | |
, table [] | |
[ thead [] [ fieldHeader ] | |
, tbody [] fieldRows | |
] | |
] | |
main = | |
Html.App.program | |
{ init = ( init "", getSObject "Unit__c" ) | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment