Last active
March 22, 2016 01:31
-
-
Save bratsche/e7b39ac90168866ee357 to your computer and use it in GitHub Desktop.
Restricted access with elm-transit-router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (..) where | |
import Effects exposing (Effects, Never, none) | |
import Json.Decode as Json | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import String | |
import StartApp | |
import Signal exposing (Address) | |
import Task | |
import TransitStyle | |
import TransitRouter exposing (WithRoute, getTransition) | |
import Routes exposing (..) | |
import HomePage exposing (..) | |
import LoginPage exposing (..) | |
import ContactsPage exposing (..) | |
import Maybe exposing (..) | |
-- Model | |
type Action | |
= NoOp | |
| RouterAction (TransitRouter.Action Route) | |
| HomePageAction HomePage.Action | |
| LoginPageAction LoginPage.Action | |
| ContactsPageAction ContactsPage.Action | |
type alias Model = WithRoute Route | |
{ authToken : Maybe String | |
, homePageModel : HomePage.Model | |
, loginPageModel : LoginPage.Model | |
, contactsPageModel : ContactsPage.Model | |
} | |
-- Update | |
initialModel : Model | |
initialModel = | |
{ authToken = Nothing | |
, transitRouter = TransitRouter.empty Routes.HomeRoute | |
, homePageModel = HomePage.initialModel | |
, loginPageModel = LoginPage.initialModel | |
, contactsPageModel = ContactsPage.initialModel | |
} | |
init : String -> ( Model, Effects Action ) | |
init path = | |
TransitRouter.init routerConfig path initialModel | |
routerConfig : TransitRouter.Config Route Action Model | |
routerConfig = | |
{ mountRoute = mountRoute | |
, getDurations = \_ _ _ -> ( 50, 200 ) | |
, actionWrapper = RouterAction | |
, routeDecoder = Routes.decode | |
} | |
mountRoute : Route -> Route -> Model -> ( Model, Effects Action ) | |
mountRoute prevRoute route model = | |
case model.authToken of | |
Nothing -> | |
case route of | |
HomeRoute -> | |
( model, Effects.none ) | |
LoginRoute -> | |
( model, Effects.none ) | |
ContactsRoute -> | |
( model | |
, Routes.redirect LoginRoute |> Effects.map (\_ -> NoOp) | |
) | |
Just _ -> | |
case route of | |
HomeRoute -> | |
( model, Effects.none ) | |
LoginRoute -> | |
( model, Effects.none ) | |
ContactsRoute -> | |
( model, Effects.none ) | |
actions : Signal Action | |
actions = | |
Signal.map RouterAction TransitRouter.actions | |
update : Action -> Model -> ( Model, Effects Action ) | |
update action model = | |
case action of | |
NoOp -> | |
noEffects model | |
HomePageAction pageAction -> | |
noEffects { model | homePageModel = HomePage.update pageAction model.homePageModel } | |
LoginPageAction pageAction -> | |
noEffects { model | loginPageModel = LoginPage.update pageAction model.loginPageModel } | |
ContactsPageAction pageAction -> | |
noEffects { model | contactsPageModel = ContactsPage.update pageAction model.contactsPageModel } | |
RouterAction routeAction -> | |
TransitRouter.update routerConfig routeAction model | |
noEffects : Model -> ( Model, Effects Action ) | |
noEffects model = | |
( model, Effects.none ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Routes (..) where | |
import Effects exposing (..) | |
import RouteParser exposing (..) | |
import TransitRouter | |
import Debug | |
type Route | |
= HomeRoute | |
| LoginRoute | |
| ContactsRoute | |
decode : String -> Route | |
decode path = | |
RouteParser.match routeParsers path | |
|> Maybe.withDefault HomeRoute | |
encode : Route -> String | |
encode route = | |
case route of | |
HomeRoute -> | |
"/" | |
LoginRoute -> | |
"/login" | |
ContactsRoute -> | |
"/contacts" | |
redirect : Route -> Effects () | |
redirect route = | |
encode route | |
|> Signal.send TransitRouter.pushPathAddress | |
|> Effects.task | |
routeParsers : List (Matcher Route) | |
routeParsers = | |
[ static HomeRoute "/" | |
, static LoginRoute "/login" | |
, static ContactsRoute "/contacts" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment