1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-24 05:45:11 +03:00

Extract routes- / url-helper into *.Common.*

This commit is contained in:
Jens Krause 2017-09-08 17:26:25 +02:00
parent 2968979823
commit 3c8f552597
No known key found for this signature in database
GPG Key ID: 3B2FAFBCEFA5906D
3 changed files with 24 additions and 24 deletions

View File

@ -4,11 +4,13 @@ import Prelude
import Data.Generic (class Generic, gEq, gShow)
import Data.Maybe (fromMaybe)
import Pux.Router (end, lit, router, str)
import Guide.Api.Types (CUid(..))
import Guide.Common.Routes (categoryLit, categoryDetailUrl)
import Guide.Common.Types (CategoryName(..))
import Pux.Router (end, lit, router, str)
data Route
= CategoryDetail CategoryName String
= CategoryDetail CategoryName (CUid String)
| NotFound String
derive instance genericRoute :: Generic Route
@ -21,20 +23,8 @@ instance eqRoute :: Eq Route where
match :: String -> Route
match url = fromMaybe (NotFound url) $ router url $
CategoryDetail <<< CategoryName <$> (lit categoryLit *> str)
<*> str <* end
<*> (CUid <$> str) <* end
toURL :: Route -> String
toURL (NotFound url) = url
toURL (CategoryDetail catName catId) = categoryDetailUrl catName catId
litUrl :: String -> String
litUrl lit = "/" <> lit
categoryLit :: String
categoryLit = "category"
categoryUrl :: CategoryName -> String
categoryUrl (CategoryName name) = (litUrl categoryLit) <> (litUrl name)
categoryDetailUrl :: CategoryName -> String -> String
categoryDetailUrl catName catId = (categoryUrl catName) <> (litUrl catId)

View File

@ -5,6 +5,7 @@ import Prelude
import Data.Generic (class Generic, gEq, gShow)
import Data.Maybe (fromMaybe)
import Pux.Router (end, lit, router, str)
import Guide.Common.Routes (categoryLit, categoryUrl)
import Guide.Common.Types (CategoryName(..))
data Route
@ -24,12 +25,3 @@ match url = fromMaybe (NotFound url) $ router url $
toURL :: Route -> String
toURL (NotFound url) = url
toURL (CategoryOverview catName) = categoryUrl catName
litUrl :: String -> String
litUrl lit = "/" <> lit
categoryLit :: String
categoryLit = "category"
categoryUrl :: CategoryName -> String
categoryUrl (CategoryName name) = (litUrl categoryLit) <> (litUrl name)

View File

@ -0,0 +1,18 @@
module Guide.Common.Routes where
import Prelude
import Guide.Api.Types (CUid(..))
import Guide.Common.Types (CategoryName(..))
litUrl :: String -> String
litUrl lit = "/" <> lit
categoryLit :: String
categoryLit = "category"
categoryUrl :: CategoryName -> String
categoryUrl (CategoryName name) = (litUrl categoryLit) <> (litUrl name)
categoryDetailUrl :: forall a. CategoryName -> CUid a -> String
categoryDetailUrl catName (CUid catId) = (categoryUrl catName) <> (litUrl catId)