Skip to content
This repository has been archived by the owner on Oct 28, 2019. It is now read-only.

Commit

Permalink
Upgrade to 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Rijnders committed Aug 30, 2018
1 parent 7c136d3 commit bebbdd5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 102 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
export PATH := $(NPM_PATH):$(PATH)

all: $(ELM_FILES)
@elm-make --warn --yes src/Main.elm --output dist/main.js
@elm make src/Main.elm --output dist/main.js

analyse: deps
@elm-analyse --elm-format-path=./node_modules/elm-format/bin/elm-format src
# analyse: deps
# @elm-analyse --elm-format-path=./node_modules/elm-format/bin/elm-format src

clean:
@rm -Rf dist/*
Expand Down Expand Up @@ -42,7 +42,7 @@ format-validate:
help:
@echo "Run: make <target> where <target> is one of the following:"
@echo " all Compile all Elm files"
@echo " analyse Run Elm analyse"
# @echo " analyse Run Elm analyse"
@echo " clean Remove 'dist' folder"
@echo " cover Run Elm coverage"
@echo " deps Install build dependencies"
Expand Down
19 changes: 0 additions & 19 deletions elm-package.json

This file was deleted.

26 changes: 26 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/html": "1.0.0",
"elm/http": "1.0.0",
"elm/json": "1.0.0",
"elm/url": "1.0.0"
},
"indirect": {
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
58 changes: 26 additions & 32 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
module Main exposing (main)

import Ginger.Media as Media
import Ginger.Resource exposing (Resource)
import Ginger.Rest
import Browser
import Browser.Navigation
import Html exposing (..)
import Html.Attributes exposing (..)
import Http
import Navigation
import Route exposing (Route)
import Url exposing (Url)



-- MAIN


main : Program Never Model Msg
main : Program () Model Msg
main =
Navigation.program OnNavigation
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
, onUrlChange = OnUrlChange
, onUrlRequest = OnUrlRequest
}


Expand All @@ -30,7 +32,7 @@ main =
type alias Model =
{ route : Route
, page : Page
, data : Maybe Resource
, key : Browser.Navigation.Key
}


Expand All @@ -41,12 +43,12 @@ type Page
| Unknown


init : Navigation.Location -> ( Model, Cmd Msg )
init location =
init : flags -> Url -> Browser.Navigation.Key -> ( Model, Cmd Msg )
init _ location key =
updatePage
{ route = Route.fromLocation location
, page = Loading
, data = Nothing
, key = key
}


Expand All @@ -55,26 +57,23 @@ init location =


type Msg
= OnNavigation Navigation.Location
| PushUrl Route
| GotResource (Result Http.Error Resource)
= OnUrlChange Url
| OnUrlRequest Browser.UrlRequest


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnNavigation location ->
OnUrlChange location ->
updatePage { model | route = Route.fromLocation location }

PushUrl route ->
( model, Navigation.newUrl (Route.toUrl route) )
OnUrlRequest urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Browser.Navigation.pushUrl model.key (Url.toString url) )

GotResource result ->
let
_ =
Debug.log "data" result
in
( { model | data = Result.toMaybe result }, Cmd.none )
Browser.External href ->
( model, Browser.Navigation.load href )


updatePage : Model -> ( Model, Cmd Msg )
Expand All @@ -92,21 +91,16 @@ updatePage model =
( Unknown, Cmd.none )
in
( { model | page = page }
, Http.send GotResource <|
Ginger.Rest.requestResourceByPath "/"
, Cmd.none
)



-- VIEW


view : Model -> Html Msg
view : Model -> Browser.Document Msg
view model =
let
imageUrl =
Maybe.andThen (Media.depiction Media.Small) model.data
|> Maybe.withDefault "fallbackurl"
in
main_ []
[ h1 [] [ text "hello world" ], img [ src imageUrl ] [] ]
{ title = Route.toTitle model.route
, body = [ main_ [] [ h1 [] [ text "hello world" ] ] ]
}
64 changes: 17 additions & 47 deletions src/Route.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module Route exposing (Route(..), fromLocation, link, toTitle, toUrl)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (defaultOptions, onWithOptions)
import Json.Decode as Decode
import Navigation
import UrlParser as Url exposing ((</>))
import Url exposing (Url)
import Url.Parser exposing ((</>), Parser)


type Route
Expand All @@ -22,18 +21,18 @@ type alias Id =
-- PARSE


fromLocation : Navigation.Location -> Route
fromLocation : Url -> Route
fromLocation =
Maybe.withDefault Root
<< Url.parsePath parser
<< Url.Parser.parse parser


parser : Url.Parser (Route -> a) a
parser : Parser (Route -> a) a
parser =
Url.oneOf
[ Url.map Root Url.top
, Url.map Article (Url.s "page" </> Url.int)
, Url.map NotFound (Url.s "404")
Url.Parser.oneOf
[ Url.Parser.map Root Url.Parser.top
, Url.Parser.map Article (Url.Parser.s "page" </> Url.Parser.int)
, Url.Parser.map NotFound (Url.Parser.s "404")
]


Expand All @@ -54,7 +53,7 @@ toRouteData route =
RouteData "/" "Home"

Article id ->
RouteData ("/page/" ++ toString id) ("Page " ++ toString id)
RouteData ("/page/" ++ String.fromInt id) ("Page " ++ String.fromInt id)

NotFound ->
RouteData "/404" "Not found"
Expand All @@ -70,49 +69,20 @@ toUrl =
.url << toRouteData


{-| link
{-
LINK
Type checked internal links.
Type checked internal links.
- Each path for each route is defined once
- Easy to change path
- You can't go to non-existing routes
- Each path for each route is defined once
- Easy to change path
- You can't go to non-existing routes
- Prevent page reload
- Enable ctrl-click
-}


link : (Route -> msg) -> Route -> List (Attribute msg) -> List (Html msg) -> Html msg
link toMsg route attributes content =
link : Route -> List (Attribute msg) -> List (Html msg) -> Html msg
link route attributes content =
a
([ href (toUrl route)
, onPreventDefaultClick (toMsg route)
]
++ attributes
)
content


onPreventDefaultClick : msg -> Attribute msg
onPreventDefaultClick msg =
onWithOptions "click"
{ defaultOptions | preventDefault = True }
(Decode.andThen (eventDecoder msg) eventKeyDecoder)


eventKeyDecoder : Decode.Decoder Bool
eventKeyDecoder =
Decode.map2
(not >> xor)
(Decode.field "ctrlKey" Decode.bool)
(Decode.field "metaKey" Decode.bool)


eventDecoder : msg -> Bool -> Decode.Decoder msg
eventDecoder msg preventDefault =
if preventDefault then
Decode.succeed msg
else
Decode.fail ""

0 comments on commit bebbdd5

Please sign in to comment.