Store examples model only in the App model

Otherwise the hot reload will not update the view, which was stored in the App model.
This commit is contained in:
Brian J. Cardiff 2024-02-02 10:51:44 -03:00
parent 6aff072f02
commit 52fe42f868

View File

@ -40,8 +40,8 @@ type alias Model key =
{ -- Global UI
route : Route
, previousRoute : Maybe Route
, moduleStates : Dict String (Example Examples.State Examples.Msg)
, usageExampleStates : Dict String (UsageExample UsageExamples.State UsageExamples.Msg)
, moduleStates : Dict String Examples.State
, usageExampleStates : Dict String UsageExamples.State
, isSideNavOpen : Bool
, openTooltip : Maybe TooltipId
, selectedContent : Content
@ -55,17 +55,8 @@ init : () -> Url -> key -> ( Model key, Effect )
init () url key =
( { route = Routes.fromLocation url
, previousRoute = Nothing
, moduleStates =
Dict.fromList
(List.map
(\example -> ( Example.routeName example, example ))
Examples.all
)
, usageExampleStates =
Dict.fromList
(List.map (\example -> ( UsageExample.routeName example, example ))
UsageExamples.all
)
, moduleStates = Dict.map (\_ example -> example.state) examplesDict
, usageExampleStates = Dict.map (\_ example -> example.state) usageExamplesDict
, isSideNavOpen = False
, openTooltip = Nothing
, selectedContent = ComponentExamples
@ -101,21 +92,54 @@ type Msg
| SwallowEvent
examplesDict : Dict String (Example Examples.State Examples.Msg)
examplesDict =
Dict.fromList
(List.map
(\example -> ( Example.routeName example, example ))
Examples.all
)
findExample : Model k -> String -> Maybe ( Example Examples.State Examples.Msg, Examples.State )
findExample model key =
Dict.get key model.moduleStates
|> Maybe.andThen
(\state ->
Dict.get key examplesDict
|> Maybe.map (\example -> ( example, state ))
)
usageExamplesDict : Dict String (UsageExample UsageExamples.State UsageExamples.Msg)
usageExamplesDict =
Dict.fromList
(List.map (\example -> ( UsageExample.routeName example, example ))
UsageExamples.all
)
findUsageExample : Model k -> String -> Maybe ( UsageExample UsageExamples.State UsageExamples.Msg, UsageExamples.State )
findUsageExample model key =
Dict.get key model.usageExampleStates
|> Maybe.andThen
(\state ->
Dict.get key usageExamplesDict
|> Maybe.map (\example -> ( example, state ))
)
update : Msg -> Model key -> ( Model key, Effect )
update action model =
case action of
UpdateModuleStates key exampleMsg ->
case Dict.get key model.moduleStates of
Just example ->
example.update exampleMsg example.state
case findExample model key of
Just ( example, exampleState ) ->
example.update exampleMsg exampleState
|> Tuple.mapFirst
(\newState ->
let
newExample =
{ example | state = newState }
in
{ model
| moduleStates = Dict.insert key newExample model.moduleStates
| moduleStates = Dict.insert key newState model.moduleStates
}
)
|> Tuple.mapSecond (Cmd.map (UpdateModuleStates key) >> Command)
@ -124,17 +148,13 @@ update action model =
( model, None )
UpdateUsageExamples key exampleMsg ->
case Dict.get key model.usageExampleStates of
Just usageExample ->
usageExample.update exampleMsg usageExample.state
case findUsageExample model key of
Just ( usageExample, usageExampleState ) ->
usageExample.update exampleMsg usageExampleState
|> Tuple.mapFirst
(\newState ->
let
newExample =
{ usageExample | state = newState }
in
{ model
| usageExampleStates = Dict.insert key newExample model.usageExampleStates
| usageExampleStates = Dict.insert key newState model.usageExampleStates
}
)
|> Tuple.mapSecond (Cmd.map (UpdateUsageExamples key) >> Command)
@ -160,7 +180,7 @@ update action model =
, previousRoute = Just model.route
, isSideNavOpen = False
}
, Maybe.map FocusOn (Routes.headerId route model.moduleStates model.usageExampleStates)
, Maybe.map FocusOn (Routes.headerId route examplesDict usageExamplesDict)
|> Maybe.withDefault None
)
@ -267,10 +287,10 @@ subscriptions : Model key -> Sub Msg
subscriptions model =
let
exampleSubs exampleName =
case Dict.get exampleName model.moduleStates of
Just example ->
case findExample model exampleName of
Just ( example, exampleState ) ->
Sub.map (UpdateModuleStates exampleName)
(example.subscriptions example.state)
(example.subscriptions exampleState)
Nothing ->
Sub.none
@ -284,10 +304,10 @@ subscriptions model =
exampleSubs exampleName
Routes.Usage exampleName ->
case Dict.get exampleName model.usageExampleStates of
Just example ->
case findUsageExample model exampleName of
Just ( example, exampleState ) ->
Sub.map (UpdateUsageExamples exampleName)
(example.subscriptions example.state)
(example.subscriptions exampleState)
Nothing ->
Sub.none
@ -313,10 +333,10 @@ view model =
]
exampleDocument exampleName =
case Dict.get exampleName model.moduleStates of
Just example ->
case findExample model exampleName of
Just ( example, exampleState ) ->
{ title = example.name ++ " in the NoRedInk Component Catalog"
, body = viewExample model example |> toBody
, body = viewExample model example exampleState |> toBody
}
Nothing ->
@ -340,7 +360,7 @@ view model =
}
Routes.Usage exampleName ->
case Dict.get exampleName model.usageExampleStates of
case Dict.get exampleName usageExamplesDict of
Just example ->
{ title = example.name ++ " Usage Example in the NoRedInk Component Catalog"
, body = viewUsageExample model example |> toBody
@ -360,8 +380,12 @@ view model =
}
viewExample : Model key -> Example a Examples.Msg -> Html Msg
viewExample model example =
viewExample : Model key -> Example a Examples.Msg -> a -> Html Msg
viewExample model example0 exampleState =
let
example =
{ example0 | state = exampleState }
in
Example.view { packageDependencies = model.elliePackageDependencies } example
|> Html.map (UpdateModuleStates example.name)
|> viewLayout model [ Example.extraLinks (UpdateModuleStates example.name) example ]
@ -394,8 +418,8 @@ viewAll model =
, navigate = UsageExample.routeName >> Routes.Usage >> ChangeRoute
, exampleHref = UsageExample.routeName >> Routes.Usage >> Routes.toString
}
(Dict.values model.moduleStates)
(Dict.values model.usageExampleStates)
Examples.all
UsageExamples.all
model.selectedContent
@ -409,7 +433,7 @@ viewCategory model category =
(Set.fromList Category.sorter item.categories)
category
)
(Dict.values items)
items
in
viewLayout model [] <|
viewExamplePreviews (Category.forId category)
@ -421,8 +445,8 @@ viewCategory model category =
, navigate = UsageExample.routeName >> Routes.Usage >> ChangeRoute
, exampleHref = UsageExample.routeName >> Routes.Usage >> Routes.toString
}
(filtered model.moduleStates)
(filtered model.usageExampleStates)
(filtered Examples.all)
(filtered UsageExamples.all)
model.selectedContent
@ -431,8 +455,8 @@ viewLayout model headerExtras content =
Html.div []
[ Html.header []
[ Routes.viewHeader model.route
model.moduleStates
model.usageExampleStates
examplesDict
usageExamplesDict
headerExtras
]
, Html.div
@ -530,7 +554,7 @@ navigation : Model key -> Html Msg
navigation { moduleStates, route, isSideNavOpen, openTooltip } =
let
examples =
Dict.values moduleStates
Examples.all
exampleEntriesForCategory category =
List.filter (\{ categories } -> List.any ((==) category) categories) examples