example-spa-elm-app/frontend/src/Body.elm

137 lines
4.8 KiB
Elm
Raw Normal View History

2025-01-02 01:17:48 +00:00
module Body exposing (Msg(..), Model(..), init, update, view, handleRoute, subscriptions)
import Element
import Page.Landing
import Page.Products
import Page.Resources
import Page.About
import Page.Contact
import Page.Term
2025-01-01 01:50:26 +00:00
import Page.NotFound
type Msg
= MsgLanding Page.Landing.Msg
| MsgProducts Page.Products.Msg
| MsgResources Page.Resources.Msg
| MsgAbout Page.About.Msg
| MsgContact Page.Contact.Msg
| MsgTerm Page.Term.Msg
2025-01-01 01:50:26 +00:00
| MsgNotFound Page.NotFound.Msg
type Model
= ModelLanding Page.Landing.Model
| ModelProducts Page.Products.Model
| ModelResources Page.Resources.Model
| ModelAbout Page.About.Model
| ModelContact Page.Contact.Model
| ModelTerm Page.Term.Model
2025-01-01 01:50:26 +00:00
| ModelNotFound Page.NotFound.Model
init : () -> (Model, Cmd Msg)
init flags =
let
(landingModel, landingCmd) = Page.Landing.init ()
in
(ModelLanding landingModel, Cmd.map MsgLanding landingCmd)
2025-01-02 01:17:48 +00:00
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
ModelLanding m -> Page.Landing.subscriptions m |> Sub.map MsgLanding
ModelProducts m -> Page.Products.subscriptions m |> Sub.map MsgProducts
ModelResources m -> Page.Resources.subscriptions m |> Sub.map MsgResources
ModelAbout m -> Page.About.subscriptions m |> Sub.map MsgAbout
ModelContact m -> Page.Contact.subscriptions m |> Sub.map MsgContact
ModelTerm m -> Page.Term.subscriptions m |> Sub.map MsgTerm
2025-01-02 01:17:48 +00:00
ModelNotFound m -> Page.NotFound.subscriptions m |> Sub.map MsgNotFound
handleRoute : String -> (Model, Cmd Msg)
2025-01-01 01:50:26 +00:00
handleRoute path =
case path of
"/" ->
let
(landingModel, landingCmd) = Page.Landing.init ()
in
(ModelLanding landingModel, Cmd.map MsgLanding landingCmd)
2025-01-01 01:50:26 +00:00
"/Products" ->
let
(productsModel, productsCmd) = Page.Products.init ()
in
(ModelProducts productsModel, Cmd.map MsgProducts productsCmd)
"/Resources" ->
let
(resourcesModel, resourcesCmd) = Page.Resources.init ()
in
(ModelResources resourcesModel, Cmd.map MsgResources resourcesCmd)
"/About" ->
let
(aboutModel, aboutCmd) = Page.About.init ()
in
(ModelAbout aboutModel, Cmd.map MsgAbout aboutCmd)
"/Contact" ->
let
(contactModel, contactCmd) = Page.Contact.init ()
in
(ModelContact contactModel, Cmd.map MsgContact contactCmd)
"/Term" ->
let
(termModel, termCmd) = Page.Term.init ()
in
(ModelTerm termModel, Cmd.map MsgTerm termCmd)
_ ->
let
(notFoundModel, notFoundCmd) = Page.NotFound.init ()
in
(ModelNotFound notFoundModel, Cmd.map MsgNotFound notFoundCmd)
update : Msg -> Model -> (Model, Cmd Msg)
update bodyMsg bodyModel =
let
updatePage msg model updateFn wrapMsg toBodyModel =
let
(newModel, cmd) = updateFn msg model
in
(toBodyModel newModel, Cmd.map wrapMsg cmd)
in
case (bodyMsg, bodyModel) of
(MsgLanding msg, ModelLanding m) ->
updatePage msg m Page.Landing.update MsgLanding ModelLanding
(MsgProducts msg, ModelProducts m) ->
updatePage msg m Page.Products.update MsgProducts ModelProducts
(MsgResources msg, ModelResources m) ->
updatePage msg m Page.Resources.update MsgResources ModelResources
(MsgAbout msg, ModelAbout m) ->
updatePage msg m Page.About.update MsgAbout ModelAbout
(MsgContact msg, ModelContact m) ->
updatePage msg m Page.Contact.update MsgContact ModelContact
(MsgTerm msg, ModelTerm m) ->
updatePage msg m Page.Term.update MsgTerm ModelTerm
_ ->
(bodyModel, Cmd.none)
view : Model -> Element.Element Msg
view model =
let
content = case model of
2025-01-02 01:17:48 +00:00
ModelLanding m -> Page.Landing.view m |> Element.map MsgLanding
ModelProducts m -> Page.Products.view m |> Element.map MsgProducts
ModelResources m -> Page.Resources.view m |> Element.map MsgResources
ModelAbout m -> Page.About.view m |> Element.map MsgAbout
ModelContact m -> Page.Contact.view m |> Element.map MsgContact
ModelTerm m -> Page.Term.view m |> Element.map MsgTerm
2025-01-02 01:17:48 +00:00
ModelNotFound m -> Page.NotFound.view m |> Element.map MsgNotFound
in
Element.el [Element.centerY ,Element.centerX] content