Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to orus-io/elm-spa #106

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Next Next commit
Setup a orus-io/elm-spa empty page stack
cdevienne committed Apr 1, 2022
commit 268537d61dc86c708074264192847c58d8a23b73
4 changes: 3 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"type": "application",
"source-directories": [
"src"
"src",
"../elm-spa/src"
],
"elm-version": "0.19.1",
"dependencies": {
@@ -15,6 +16,7 @@
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm-explorations/markdown": "1.0.0",
"orus-io/elm-spa": "2.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.0"
},
"indirect": {
61 changes: 61 additions & 0 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import Article.Slug exposing (Slug)
import Avatar exposing (Avatar)
import Browser exposing (Document)
import Browser.Navigation as Nav
import Effect
import Html exposing (..)
import Json.Decode as Decode exposing (Value)
import Page exposing (Page)
@@ -19,10 +20,12 @@ import Page.Register as Register
import Page.Settings as Settings
import Route exposing (Route)
import Session exposing (Session)
import Spa.PageStack
import Task
import Time
import Url exposing (Url)
import Username exposing (Username)
import View exposing (View)
import Viewer exposing (Viewer)


@@ -44,6 +47,44 @@ type Model
| Profile Username Profile.Model
| Article Article.Model
| Editor (Maybe Slug) Editor.Model
| Stack Session StackModel



-- Elm SPA pages


type alias StackModel =
Spa.PageStack.Model Never StackCurrentModel StackPreviousModel


type alias StackCurrentModel =
()


type alias StackPreviousModel =
()


type alias StackMsg =
Spa.PageStack.Msg Route StackCurrentMsg StackPreviousMsg


type alias StackCurrentMsg =
()


type alias StackPreviousMsg =
()


type alias Stack =
Spa.PageStack.Stack Never Session Never Route (View StackMsg) StackCurrentModel StackPreviousModel StackCurrentMsg StackPreviousMsg


stack : Stack
stack =
Spa.PageStack.setup { defaultView = View.default }



@@ -106,6 +147,13 @@ view model =
Editor (Just _) editor ->
viewPage Page.Other GotEditorMsg (Editor.view editor)

Stack session stackmodel ->
let
page =
stack.view session stackmodel
in
viewPage page.page StackMsg { title = page.title, content = page.content }



-- UPDATE
@@ -122,6 +170,8 @@ type Msg
| GotArticleMsg Article.Msg
| GotEditorMsg Editor.Msg
| GotSession Session
| StackMsg StackMsg
| Noop


toSession : Model -> Session
@@ -154,6 +204,9 @@ toSession page =
Editor _ editor ->
Editor.toSession editor

Stack session _ ->
session


changeRouteTo : Maybe Route -> Model -> ( Model, Cmd Msg )
changeRouteTo maybeRoute model =
@@ -268,6 +321,11 @@ update msg model =
, Route.replaceUrl (Session.navKey session) Route.Home
)

( StackMsg stackMsg, Stack session stackModel ) ->
stack.update session stackMsg stackModel
|> Tuple.mapFirst (Stack session)
|> Tuple.mapSecond (Effect.toCmd ( always Noop, StackMsg ))

( _, _ ) ->
-- Disregard messages that arrived for the wrong page.
( model, Cmd.none )
@@ -314,6 +372,9 @@ subscriptions model =
Editor _ editor ->
Sub.map GotEditorMsg (Editor.subscriptions editor)

Stack session stackmodel ->
Sub.map StackMsg (stack.subscriptions session stackmodel)



-- MAIN
27 changes: 27 additions & 0 deletions src/View.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module View exposing (..)

import Html exposing (Html)
import Page exposing (Page)


type alias View msg =
{ title : String
, page : Page
, content : Html msg
}


map : (msg -> msg1) -> View msg -> View msg1
map fn view =
{ title = view.title
, page = view.page
, content = Html.map fn view.content
}


default : View msg
default =
{ title = ""
, page = Page.Other
, content = Html.text ""
}