load packages when the app boots

This commit is contained in:
Brian Hicks 2022-03-28 16:50:12 -05:00
parent 3da2bb3709
commit 37cbb06c36

View File

@ -11,6 +11,8 @@ import Dict exposing (Dict)
import Example exposing (Example) import Example exposing (Example)
import Examples import Examples
import Html.Styled.Attributes exposing (..) import Html.Styled.Attributes exposing (..)
import Http
import Json.Decode as Decode
import Nri.Ui.CssVendorPrefix.V1 as VendorPrefixed import Nri.Ui.CssVendorPrefix.V1 as VendorPrefixed
import Nri.Ui.Heading.V2 as Heading import Nri.Ui.Heading.V2 as Heading
import Nri.Ui.MediaQuery.V1 exposing (mobile, notMobile) import Nri.Ui.MediaQuery.V1 exposing (mobile, notMobile)
@ -41,6 +43,7 @@ type alias Model =
, previousRoute : Maybe Route , previousRoute : Maybe Route
, moduleStates : Dict String (Example Examples.State Examples.Msg) , moduleStates : Dict String (Example Examples.State Examples.Msg)
, navigationKey : Key , navigationKey : Key
, ellieDependencies : Dict String String
} }
@ -52,8 +55,12 @@ init () url key =
Dict.fromList Dict.fromList
(List.map (\example -> ( example.name, example )) Examples.all) (List.map (\example -> ( example.name, example )) Examples.all)
, navigationKey = key , navigationKey = key
, ellieDependencies = Dict.empty
} }
, Cmd.none , Cmd.batch
[ loadPackage
, loadApplicationDependencies
]
) )
@ -63,6 +70,7 @@ type Msg
| OnUrlChange Url | OnUrlChange Url
| ChangeRoute Route | ChangeRoute Route
| SkipToMainContent | SkipToMainContent
| LoadedPackages (Result Http.Error (Dict String String))
| NoOp | NoOp
@ -114,6 +122,14 @@ update action model =
, Task.attempt (\_ -> NoOp) (Browser.Dom.focus "maincontent") , Task.attempt (\_ -> NoOp) (Browser.Dom.focus "maincontent")
) )
LoadedPackages (Ok newPackages) ->
( { model | ellieDependencies = Dict.union model.ellieDependencies newPackages }
, Cmd.none
)
LoadedPackages (Err problem) ->
Debug.todo "problem loading packages"
NoOp -> NoOp ->
( model, Cmd.none ) ( model, Cmd.none )
@ -253,3 +269,28 @@ navigation currentRoute =
(SideNav.entry "All" [ SideNav.href Routes.All ] (SideNav.entry "All" [ SideNav.href Routes.All ]
:: categoryNavLinks :: categoryNavLinks
) )
loadPackage : Cmd Msg
loadPackage =
Http.get
{ url = "/package.json"
, expect =
Http.expectJson
LoadedPackages
(Decode.map2 Dict.singleton
(Decode.field "name" Decode.string)
(Decode.field "version" Decode.string)
)
}
loadApplicationDependencies : Cmd Msg
loadApplicationDependencies =
Http.get
{ url = "/application.json"
, expect =
Http.expectJson
LoadedPackages
(Decode.at [ "dependencies", "direct" ] (Decode.dict Decode.string))
}