diff options
author | Joris | 2016-03-31 00:06:50 +0200 |
---|---|---|
committer | Joris | 2016-03-31 00:13:25 +0200 |
commit | c95e19407d492a0d4e9e14e320520fe29ce379e5 (patch) | |
tree | ca6a14ad1396af6a4bc36e17ce89278d5dbea0a0 /src/client/elm | |
parent | c542551ad043260e6a4a6569b4af5c748f7b6001 (diff) |
Add init data in html page
Diffstat (limited to 'src/client/elm')
-rw-r--r-- | src/client/elm/Main.elm | 22 | ||||
-rw-r--r-- | src/client/elm/Model.elm | 16 | ||||
-rw-r--r-- | src/client/elm/Model/Init.elm | 17 | ||||
-rw-r--r-- | src/client/elm/Model/InitResult.elm | 28 | ||||
-rw-r--r-- | src/client/elm/Model/View.elm | 3 | ||||
-rw-r--r-- | src/client/elm/Server.elm | 10 | ||||
-rw-r--r-- | src/client/elm/View.elm | 3 | ||||
-rw-r--r-- | src/client/elm/View/Loading.elm | 12 |
8 files changed, 63 insertions, 48 deletions
diff --git a/src/client/elm/Main.elm b/src/client/elm/Main.elm index 561ea91..0813573 100644 --- a/src/client/elm/Main.elm +++ b/src/client/elm/Main.elm @@ -17,6 +17,7 @@ import Server import Mailbox import Action exposing (..) import Model exposing (Model, initialModel) +import Model.InitResult as InitResult exposing (initResultDecoder) import Update exposing (update, routerConfig) import View exposing (view) @@ -28,7 +29,7 @@ main = app.html app : App Model app = StartApp.start - { init = initData `Effects.andThen` initRouter + { init = (initData, Effects.none) `Effects.andThen` initRouter , view = view , update = update , inputs = @@ -40,20 +41,13 @@ app = StartApp.start -- Init -initData : (Model, Effects Action) +initData : Model initData = - case Json.decodeString Json.string signInError of - Ok signInError -> - ( initialModel initialTime translations conf (Just signInError) - , Effects.none - ) + case Json.decodeString initResultDecoder initResult of + Ok init -> + initialModel initialTime translations conf init Err _ -> - ( initialModel initialTime translations conf Nothing - , Server.init - |> Task.map GoLoggedInView - |> flip Task.onError (always <| Task.succeed GoSignInView) - |> Effects.task - ) + initialModel initialTime translations conf InitResult.InitEmpty initRouter : Model -> (Model, Effects Action) initRouter model = TransitRouter.init routerConfig location model @@ -68,5 +62,5 @@ port tasks = app.tasks port initialTime : Time port translations : String port conf : String -port signInError : String +port initResult : String port location : String diff --git a/src/client/elm/Model.elm b/src/client/elm/Model.elm index 9e3f4a0..b4213d5 100644 --- a/src/client/elm/Model.elm +++ b/src/client/elm/Model.elm @@ -12,6 +12,8 @@ import Route exposing (Route) import Model.View exposing (..) import Model.Translations exposing (..) import Model.Conf exposing (..) +import Model.InitResult exposing (..) +import LoggedIn.Model as LoggedInModel import SignIn.Model as SignInModel @@ -25,12 +27,16 @@ type alias Model = , transitRouter : TransitRouter.TransitRouter Route } -initialModel : Time -> String -> String -> Maybe String -> Model -initialModel initialTime translations conf mbSignInError = +initialModel : Time -> String -> String -> InitResult -> Model +initialModel initialTime translations conf initResult = { view = - if isJust mbSignInError - then SignInView (SignInModel.init mbSignInError) - else LoadingView + case initResult of + InitEmpty -> + SignInView (SignInModel.init Nothing) + InitSuccess init -> + LoggedInView (LoggedInModel.init init) + InitError error -> + SignInView (SignInModel.init (Just error)) , currentTime = initialTime , translations = case Json.decodeString translationsDecoder translations of diff --git a/src/client/elm/Model/Init.elm b/src/client/elm/Model/Init.elm index 7fccf00..5db038d 100644 --- a/src/client/elm/Model/Init.elm +++ b/src/client/elm/Model/Init.elm @@ -1,10 +1,13 @@ module Model.Init ( Init + , initDecoder ) where -import Model.Payment exposing (Payments) -import Model.Income exposing (Incomes) -import Model.User exposing (Users, UserId) +import Json.Decode as Json exposing ((:=)) + +import Model.Payment exposing (Payments, paymentsDecoder) +import Model.Income exposing (Incomes, incomesDecoder) +import Model.User exposing (Users, UserId, usersDecoder, userIdDecoder) type alias Init = { users : Users @@ -12,3 +15,11 @@ type alias Init = , payments : Payments , incomes : Incomes } + +initDecoder : Json.Decoder Init +initDecoder = + Json.object4 Init + ("users" := usersDecoder) + ("me" := userIdDecoder) + ("payments" := paymentsDecoder) + ("incomes" := incomesDecoder) diff --git a/src/client/elm/Model/InitResult.elm b/src/client/elm/Model/InitResult.elm new file mode 100644 index 0000000..d1f1348 --- /dev/null +++ b/src/client/elm/Model/InitResult.elm @@ -0,0 +1,28 @@ +module Model.InitResult + ( InitResult(..) + , initResultDecoder + ) where + +import Json.Decode as Json exposing ((:=)) + +import Model.Init exposing (Init, initDecoder) + +type InitResult = + InitEmpty + | InitSuccess Init + | InitError String + +initResultDecoder : Json.Decoder InitResult +initResultDecoder = ("tag" := Json.string) `Json.andThen` initResultDecoderWithTag + +initResultDecoderWithTag : String -> Json.Decoder InitResult +initResultDecoderWithTag tag = + case tag of + "InitEmpty" -> + Json.succeed InitEmpty + "InitSuccess" -> + Json.map InitSuccess ("contents" := initDecoder) + "InitError" -> + Json.map InitError ("contents" := Json.string) + _ -> + Json.fail <| "got " ++ tag ++ " for InitResult" diff --git a/src/client/elm/Model/View.elm b/src/client/elm/Model/View.elm index 9d64c73..475e826 100644 --- a/src/client/elm/Model/View.elm +++ b/src/client/elm/Model/View.elm @@ -8,6 +8,5 @@ import SignIn.Model as SignInModel import LoggedIn.Model as LoggedInModel type View = - LoadingView - | SignInView SignInModel.Model + SignInView SignInModel.Model | LoggedInView LoggedInModel.Model diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm index ad6d212..be052bb 100644 --- a/src/client/elm/Server.elm +++ b/src/client/elm/Server.elm @@ -1,6 +1,5 @@ module Server - ( init - , signIn + ( signIn , addPayment , deletePayment , setIncome @@ -20,13 +19,6 @@ import Model.Income exposing (incomesDecoder, incomeIdDecoder, IncomeId) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) import Model.Init exposing (Init) -init : Task Http.Error Init -init = - Task.map Init (Http.get usersDecoder "/api/users") - `Task.andMap` (Http.get ("id" := userIdDecoder) "/api/whoAmI") - `Task.andMap` (Http.get paymentsDecoder "/api/payments") - `Task.andMap` (Http.get incomesDecoder "/api/incomes") - signIn : String -> Task Http.Error () signIn email = post ("/api/signIn?email=" ++ email) diff --git a/src/client/elm/View.elm b/src/client/elm/View.elm index 6165766..90808aa 100644 --- a/src/client/elm/View.elm +++ b/src/client/elm/View.elm @@ -11,7 +11,6 @@ import Model.View exposing (..) import LoggedData import View.Header exposing (renderHeader) -import View.Loading exposing (renderLoading) import SignIn.View as SignInView import LoggedIn.View as LoggedInView @@ -27,8 +26,6 @@ view address model = renderMain : Address Action -> Model -> Html renderMain address model = case model.view of - LoadingView -> - renderLoading address SignInView signIn -> SignInView.view address model signIn LoggedInView loggedIn -> diff --git a/src/client/elm/View/Loading.elm b/src/client/elm/View/Loading.elm deleted file mode 100644 index 5270099..0000000 --- a/src/client/elm/View/Loading.elm +++ /dev/null @@ -1,12 +0,0 @@ -module View.Loading - ( renderLoading - ) where - -import Signal exposing (Address) - -import Html exposing (..) - -import Action exposing (Action) - -renderLoading : Address Action -> Html -renderLoading address = text "" |