github.com/simpleiot/simpleiot@v0.18.3/frontend/src/Shared.elm (about)

     1  module Shared exposing
     2      ( Flags
     3      , Model
     4      , Msg(..)
     5      , init
     6      , subscriptions
     7      , update
     8      )
     9  
    10  import Gen.Route
    11  import Json.Decode as Json
    12  import Request exposing (Request)
    13  import Storage exposing (Storage)
    14  
    15  
    16  type alias Flags =
    17      Json.Value
    18  
    19  
    20  type alias Model =
    21      { storage : Storage
    22      }
    23  
    24  
    25  init : Request -> Flags -> ( Model, Cmd Msg )
    26  init req flags =
    27      let
    28          model =
    29              { storage = Storage.fromJson flags }
    30      in
    31      ( model
    32      , if model.storage.user /= Nothing && req.route == Gen.Route.SignIn then
    33          Request.replaceRoute Gen.Route.SignIn req
    34  
    35        else
    36          Cmd.none
    37      )
    38  
    39  
    40  type Msg
    41      = StorageUpdated Storage
    42  
    43  
    44  update : Request -> Msg -> Model -> ( Model, Cmd Msg )
    45  update req msg model =
    46      case msg of
    47          StorageUpdated storage ->
    48              ( { model | storage = storage }
    49              , if Gen.Route.SignIn == req.route then
    50                  Request.pushRoute Gen.Route.Home_ req
    51  
    52                else
    53                  Cmd.none
    54              )
    55  
    56  
    57  subscriptions : Request -> Model -> Sub Msg
    58  subscriptions _ _ =
    59      Storage.load StorageUpdated