website/frontend/src/Shared.elm

113 lines
2.1 KiB
Elm
Executable file

module Shared exposing
( Flags, decoder
, Model, Msg
, init, update, subscriptions
)
{-|
@docs Flags, decoder
@docs Model, Msg
@docs init, update, subscriptions
-}
import Browser.Events as BR exposing (..)
import Effect exposing (Effect)
import Element as E exposing (..)
import Json.Decode exposing (..)
import Route exposing (Route)
import Route.Path
import Shared.Model
import Shared.Msg
-- FLAGS
type alias Flags =
{ height : Int
, width : Int
}
decoder : Json.Decode.Decoder Flags
decoder =
Json.Decode.map2
(\height width ->
{ height = height
, width = width
}
)
(field "height" int)
(field "width" int)
-- INIT
type alias Model =
Shared.Model.Model
init : Result Json.Decode.Error Flags -> Route () -> ( Model, Effect Msg )
init flagsResult route =
( modelFromFlagsResult flagsResult
, Effect.none
)
modelFromFlagsResult : Result Error Flags -> Model
modelFromFlagsResult f =
case f of
Ok flags ->
{ device = E.classifyDevice flags
, height = flags.height
, width = flags.width
}
Err _ ->
{ device =
E.classifyDevice
{ height = 0
, width = 0
}
, height = 10
, width = 10
}
-- UPDATE
type alias Msg =
Shared.Msg.Msg
update : Route () -> Msg -> Model -> ( Model, Effect Msg )
update route msg model =
case msg of
Shared.Msg.Resize width height ->
( { model
| device =
{ height = height
, width = width
}
|> E.classifyDevice
, height = height
, width = width
}
, Effect.none
)
-- SUBSCRIPTIONS
subscriptions : Route () -> Model -> Sub Msg
subscriptions route model =
BR.onResize Shared.Msg.Resize