Created
November 27, 2018 06:33
-
-
Save Woody88/632978bd837bfa6d22ef010c51a22657 to your computer and use it in GitHub Desktop.
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 Control.Router where | |
| import Prelude | |
| import Control.Monad (class Monad) | |
| import Control.Monad.Reader (class MonadAsk, ask) | |
| import Data.Router | |
| import Effect (Effect) | |
| import Effect.Class (class MonadEffect, liftEffect) | |
| import Foreign (Foreign, unsafeToForeign) | |
| import React.Basic as React | |
| import Router.Types (Route) | |
| import Routing.PushState (LocationState) | |
| class Monad r <= MonadRouting r where | |
| getLocation :: r LocationState | |
| getRoute :: r Route | |
| navigateTo :: Route -> r Unit | |
| pushState :: Foreign -> Route -> r Unit | |
| instance monadRouting :: | |
| ( MonadEffect m | |
| , MonadAsk (React.Self { router :: Router Effect | props } state action) m | |
| ) => MonadRouting m where | |
| navigateTo :: Route -> m Unit | |
| navigateTo url = do | |
| self <- ask | |
| self.props.router.navigateTo url | |
| getLocation :: m LocationState | |
| getLocation = do | |
| self <- ask | |
| self.props.router.getLocation | |
| getRoute :: m Route | |
| getRoute = do | |
| self <- ask | |
| location <- liftEffect $ self.props.router.getLocation | |
| self.props.router.parseRoute location.path | |
| pushState :: Foreign -> Route -> m Unit | |
| pushState f r = do | |
| self <- ask | |
| self.props.router.pushState f r | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment