Created
March 9, 2019 00:21
-
-
Save paul/e102fd78196894b7ca65044fbb584373 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 (main) | |
-- import Api.Object.Deck | |
-- import Api.Query as Query | |
-- import Api.Scalar exposing (Id(..)) | |
-- import Graphql.Operation exposing (RootQuery) | |
-- import Graphql.SelectionSet as SelectionSet exposing (SelectionSet) | |
import Browser | |
import Html exposing (..) | |
import Html.Attributes exposing (class, src) | |
import Http | |
import Json.Decode as D | |
import Json.Decode.Extra as DE | |
import Json.Decode.Pipeline exposing (hardcoded, optional, required) | |
import JsonApi | |
import JsonApi.Decode | |
import JsonApi.Documents | |
import JsonApi.Resources | |
import Task exposing (..) | |
-- MODEL | |
type alias DeckId = | |
Int | |
type alias Model = | |
{ deckId : DeckId | |
, deck : Maybe Deck | |
} | |
type alias Deck = | |
{ name : String | |
, previewImage : String | |
} | |
deckDecoder : D.Decoder Deck | |
deckDecoder = | |
D.succeed Deck | |
|> required "name" D.string | |
|> required "preview_image" D.string | |
extractDeck : JsonApi.Document -> Result D.Error Deck | |
extractDeck doc = | |
let | |
resourceResult = | |
JsonApi.Documents.primaryResource doc | |
in | |
case resourceResult of | |
Err err -> | |
Debug.todo (Debug.toString err) | |
Ok maybeResource -> | |
case maybeResource of | |
Nothing -> | |
Debug.todo "whoops" | |
Just resource -> | |
JsonApi.Resources.attributes deckDecoder resource | |
getDeckResource : DeckId -> Cmd Message | |
getDeckResource deckId = | |
Http.request | |
{ method = "GET" | |
, headers = [ Http.header "Accept" "application/json" ] | |
, url = "http://localhost:42080/decks/" ++ String.fromInt deckId | |
, body = Http.emptyBody | |
, expect = Http.expectJson GotDeck JsonApi.Decode.document | |
, timeout = Nothing | |
, tracker = Nothing | |
} | |
-- query : SelectionSet (Maybe Deck) RootQuery | |
-- query = | |
-- Query.deck { id = Id "2" } deckSelection | |
-- deckSelection : SelectionSet Deck Api.Object.Deck | |
-- deckSelection = | |
-- SelectionSet.map2 Deck | |
-- Deck.name | |
-- Deck.previewImage | |
-- INIT | |
type alias Flags = | |
{ deckId : DeckId } | |
flagsDecoder : D.Decoder Flags | |
flagsDecoder = | |
D.succeed Flags | |
|> required "deckId" DE.parseInt | |
init : D.Value -> ( Model, Cmd Message ) | |
init flagJson = | |
let | |
flagsResult = | |
D.decodeValue flagsDecoder flagJson | |
in | |
case flagsResult of | |
Ok flags -> | |
( Model flags.deckId Nothing, getDeckResource flags.deckId ) | |
Err err -> | |
Debug.todo (Debug.toString err) | |
( Model 42, Cmd.none ) | |
-- VIEW | |
section : List (Attribute msg) -> List (Html msg) -> Html msg | |
section attrs content = | |
div (attrs ++ [ class "section" ]) content | |
container : List (Attribute msg) -> List (Html msg) -> Html msg | |
container attrs content = | |
div (attrs ++ [ class "container" ]) content | |
level : List (Attribute msg) -> List (Html msg) -> Html msg | |
level attrs content = | |
div (attrs ++ [ class "level" ]) content | |
levelLeft : List (Attribute msg) -> List (Html msg) -> Html msg | |
levelLeft attrs content = | |
div (attrs ++ [ class "level-left" ]) content | |
levelItem : List (Attribute msg) -> List (Html msg) -> Html msg | |
levelItem attrs content = | |
div (attrs ++ [ class "level-item" ]) content | |
view : Model -> Html Message | |
view model = | |
case model.deck of | |
Just deck -> | |
section [] | |
[ container [] | |
[ level [] | |
[ levelLeft [] | |
[ levelItem [] | |
[ figure [] | |
[ p [ class "image is-128x128 card-art" ] | |
[ img [ src deck.previewImage ] [] | |
] | |
] | |
] | |
] | |
] | |
] | |
] | |
Nothing -> | |
h1 [ class "title" ] | |
[ text "Hello Deck Editor!" | |
, h2 [ class "title" ] [ text "Deck load error" ] | |
] | |
-- MESSAGE | |
type Message | |
= GotDeck (Result Http.Error JsonApi.Document) | |
-- UPDATE | |
update : Message -> Model -> ( Model, Cmd Message ) | |
update message model = | |
case ( message, model ) of | |
( GotDeck result, _ ) -> | |
case result of | |
Ok doc -> | |
let | |
maybeDeck = | |
extractDeck doc | |
deck = | |
case maybeDeck of | |
Ok res -> | |
res | |
Err err -> | |
Debug.todo (Debug.toString err) | |
newModel = | |
{ model | deck = Just deck } | |
in | |
( newModel, Cmd.none ) | |
Err err -> | |
Debug.todo (Debug.toString err) | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Message | |
subscriptions model = | |
Sub.none | |
-- MAIN | |
main : Program D.Value Model Message | |
main = | |
Browser.element | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment