1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-22 20:31:31 +03:00

Change Uid a to CUid a newtype

This commit is contained in:
vrom911 2017-08-09 02:17:53 +03:00
parent ed9431531d
commit b3f41d02e3
8 changed files with 40 additions and 34 deletions

1
.gitignore vendored
View File

@ -34,6 +34,7 @@ tags
node_modules/
front/coverage/
front/build/
static/js/
.env
npm-debug.log*

View File

@ -9,9 +9,8 @@ import Data.Argonaut.Generic.Aeson (options)
import Data.Argonaut.Generic.Decode (genericDecodeJson)
import Data.Either (Either(..), either)
import Data.Generic (class Generic)
import Guide.Api.ClientTypes (CCategoryDetail)
import Guide.Api.ClientTypes (CCategoryDetail, CUid(..))
import Guide.Types (CGrandCategories, CategoryName(..), Users)
import Guide.Utils (Uid(..))
import Lib.IsomorphicFetch (FETCH, fetch)
apiEndpoint :: String
@ -33,9 +32,9 @@ fetchGrandCategories (CategoryName catName) = do
res <- attempt <<< fetch $ apiEndpoint <> catName <> "/all-categories"
pure $ either (Left <<< show) decodeJson res
-- TODO: Use `Uid Category` instead of `String` as second function parameter
-- TODO: Use `CUid Category` instead of `CUid String` as second function parameter
-- if we have found a way to bridge `Uid a` properly from `Haskell` to `PS`
fetchCategory :: forall eff. CategoryName -> (Uid String) -> Aff (fetch :: FETCH | eff) (Either String CCategoryDetail)
fetchCategory (CategoryName catName) (Uid catId) = do
fetchCategory :: forall eff. CategoryName -> (CUid String) -> Aff (fetch :: FETCH | eff) (Either String CCategoryDetail)
fetchCategory (CategoryName catName) (CUid catId) = do
res <- attempt <<< fetch $ apiEndpoint <> catName <> "/category/" <> catId.uidToText
pure $ either (Left <<< show) decodeJson res

View File

@ -1,8 +1,7 @@
module Guide.Events where
import Guide.Api.ClientTypes (CCategoryDetail)
import Guide.Api.ClientTypes (CCategoryDetail, CUid)
import Data.Either (Either)
import Guide.Utils (Uid)
import Guide.Routes (Route)
import Guide.Types (CGrandCategories, CategoryName, Users)
import Pux.DOM.Events (DOMEvent)
@ -14,8 +13,8 @@ data Event
-- API
| RequestGrandCategories CategoryName
| ReceiveGrandCategories (Either String CGrandCategories)
| RequestCategory CategoryName (Uid String) -- (String == Uid Category)
-- TODO: ^ Use `Uid Category` instead of `String` as second type parameter
| RequestCategory CategoryName (CUid String)
-- TODO: ^ Use `CUid Category` instead of `CUid String` as second type parameter
-- if we have found a way to bridge `Uid a` properly from `Haskell` to `PS`
| ReceiveCategory (Either String CCategoryDetail)
-- playground

View File

@ -6,14 +6,14 @@ import Control.Alt ((<|>))
import Data.Generic (class Generic, gEq, gShow)
import Data.Maybe (fromMaybe)
import Guide.Types (CategoryName(..))
import Guide.Utils (Uid(..))
import Guide.Api.ClientTypes (CUid(..))
import Pux.Router (end, lit, router, str)
data Route
= Home
| CategoryOverview CategoryName
| CategoryDetail CategoryName (Uid String) -- String == (Uid Category)
-- TODO: Use `Uid Category` instead of `Uid String`
| CategoryDetail CategoryName (CUid String)
-- TODO: Use `CUid Category` instead of `CUid String`
-- if we have found a way to bridge `Uid a` properly from `Haskell` to `PS`
| Playground
| NotFound String
@ -31,7 +31,7 @@ match url = fromMaybe (NotFound url) $ router url $
CategoryOverview <<< CategoryName <$> (lit categoryLit *> str) <* end
<|>
CategoryDetail <<< CategoryName <$> (lit categoryLit *> str)
<*> ((\s -> Uid {uidToText : s}) <$> str) <* end
<*> ((\s -> CUid {uidToText : s}) <$> str) <* end
<|>
Playground <$ (lit playgroundLit) <* end
@ -57,8 +57,8 @@ categoryUrl (CategoryName name) = (litUrl categoryLit) <> (litUrl name)
categoryDetailLit :: String
categoryDetailLit = "detail"
categoryDetailUrl :: CategoryName -> Uid String -> String
categoryDetailUrl catName (Uid catId) = (categoryUrl catName) <> (litUrl catId.uidToText)
categoryDetailUrl :: CategoryName -> CUid String -> String
categoryDetailUrl catName (CUid catId) = (categoryUrl catName) <> (litUrl catId.uidToText)
playgroundLit :: String
playgroundLit = "playground"

View File

@ -8,7 +8,7 @@ import Guide.Events (Event(..))
import Guide.Routes (Route(..), toUrl)
import Guide.State (State(..))
import Guide.Types (CategoryName(..))
import Guide.Utils (Uid(..))
import Guide.Api.ClientTypes (CUid(..))
import Pux.DOM.Events (onClick) as P
import Pux.DOM.HTML (HTML) as P
import Text.Smolder.HTML (div, h1, a) as S
@ -16,8 +16,8 @@ import Text.Smolder.HTML.Attributes (href) as S
import Text.Smolder.Markup ((#!), (!))
import Text.Smolder.Markup (text) as S
view :: CategoryName -> Uid String -> State -> P.HTML Event
view (CategoryName cName) (Uid catId) (State st) =
view :: CategoryName -> CUid String -> State -> P.HTML Event
view (CategoryName cName) (CUid catId) (State st) =
S.div $ do
S.h1 $ S.text (cName <> " - " <> catId.uidToText)
S.a ! S.href (toUrl Home)

View File

@ -6,13 +6,12 @@ import Prelude
import Data.Array (null)
import Data.Foldable (for_)
import Guide.Api.ClientTypes (CCategoryOverview(..), CGrandCategory(..))
import Guide.Api.ClientTypes (CCategoryOverview(..), CGrandCategory(..), CUid(..))
import Guide.Events (Event(..))
import Guide.Routes (Route(..), toUrl)
import Guide.State (State(..))
import Guide.Types (CGrandCategories, CategoryName(..))
import Guide.Util.DOMUtil (mkKey)
import Guide.Utils (Uid(..))
import Network.RemoteData (RemoteData(..))
import Pux.DOM.Events (onClick) as P
import Pux.DOM.HTML (HTML) as P
@ -69,7 +68,7 @@ catOverviewView :: State -> CCategoryOverview -> P.HTML Event
catOverviewView (State st) (CCategoryOverview cat) =
let url = toUrl $ CategoryDetail st.currentCategoryName cat.ccoUid in
S.li
! P.key ((\(Uid t) -> t.uidToText) cat.ccoUid)
! P.key ((\(CUid t) -> t.uidToText) cat.ccoUid)
$ S.a
! S.href url
#! P.onClick (Navigate url)

View File

@ -3,6 +3,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveDataTypeable #-}
{- |
This module describes API's client types. These are needed to generate
@ -18,6 +19,7 @@ module Guide.Api.ClientTypes
, CItem(..)
, CMarkdown(..)
, CTrait(..)
, CUid(..)
, toCGrandCategory
, toCCategoryDetail
)
@ -39,7 +41,7 @@ import Guide.Views.Utils (categoryLink)
-- | Client type of `Category`, which describes a category overview
data CCategoryOverview = CCategoryOverview
{ ccoUid :: Uid String
{ ccoUid :: CUid String
, ccoTitle :: Text
, ccoLink :: Text
} deriving (Show, Generic)
@ -49,7 +51,7 @@ instance A.ToJSON CCategoryOverview where
toCCategoryOverview :: Category -> CCategoryOverview
toCCategoryOverview cat = CCategoryOverview
{ ccoUid = toUid (cat^.uid)
{ ccoUid = toCUid (cat^.uid)
, ccoTitle = cat^.title
, ccoLink = categoryLink cat
}
@ -76,7 +78,7 @@ toCGrandCategory cats = CGrandCategory
-- | Client type of `Category`, which describes a category detail
data CCategoryDetail = CCategoryDetail
{ ccdUid :: Uid String
{ ccdUid :: CUid String
, ccdTitle :: Text
, ccdGroup :: Text
, ccdDescription :: CMarkdown
@ -90,7 +92,7 @@ instance A.ToJSON CCategoryDetail where
toCCategoryDetail :: Category -> CCategoryDetail
toCCategoryDetail cat = CCategoryDetail
{ ccdUid = toUid (cat^.uid)
{ ccdUid = toCUid (cat^.uid)
, ccdTitle = cat^.title
, ccdGroup = cat^.group_
, ccdDescription = toCMarkdown $ cat^.notes
@ -100,7 +102,7 @@ toCCategoryDetail cat = CCategoryDetail
-- | Client type of `Item`
data CItem = CItem
{ ciUid :: Uid String
{ ciUid :: CUid String
, ciName :: Text
, ciCreated :: UTCTime
, ciGroup :: Maybe Text
@ -120,7 +122,7 @@ instance A.ToJSON CItem where
toCItem :: Item -> CItem
toCItem item = CItem
{ ciUid = toUid (item ^. uid)
{ ciUid = toCUid (item ^. uid)
, ciName = item ^. name
, ciCreated = item ^. created
, ciGroup = item ^. group_
@ -136,7 +138,7 @@ toCItem item = CItem
}
data CTrait = CTrait
{ ctUid :: Uid String
{ ctUid :: CUid String
, ctContent :: CMarkdown
} deriving (Show, Generic)
@ -146,7 +148,7 @@ instance A.ToJSON CTrait where
toCTrait :: Trait -> CTrait
toCTrait trait = CTrait
{ ctUid = toUid (trait ^. uid)
{ ctUid = toCUid (trait ^. uid)
, ctContent = toCMarkdown $ trait ^. content
}
@ -178,5 +180,11 @@ instance ToCMardown MarkdownTree where
, html = T.toStrict . renderText $ toHtml md
}
toUid :: Uid a -> Uid b
toUid (Uid t) = Uid t
newtype CUid a = CUid {uidToText :: Text}
deriving (Eq, Ord, Show, Generic, Data, Typeable)
instance A.ToJSON (CUid a) where
toJSON = A.genericToJSON A.defaultOptions
toCUid :: Uid a -> CUid b
toCUid (Uid t) = CUid t

View File

@ -18,11 +18,11 @@ import Language.PureScript.Bridge ( BridgePart, Language( Haskell ), PSType,
import Language.PureScript.Bridge.PSTypes (psString)
import Language.PureScript.Bridge.TypeParameters (A)
import Guide.Api.ClientTypes ( CCategoryDetail, CCategoryOverview
, CGrandCategory, CItem, CTrait, CMarkdown
, CGrandCategory, CItem, CTrait
, CMarkdown, CUid
)
import Guide.Types.Hue (Hue)
import Guide.Types.Core (CategoryStatus, ItemKind)
import Guide.Utils (Uid)
path :: FilePath
path = "front-ps/src/Generated"
@ -58,7 +58,7 @@ clientTypes =
, mkSumType (Proxy @ItemKind)
, mkSumType (Proxy @CTrait)
, mkSumType (Proxy @CMarkdown)
, mkSumType (Proxy @(Uid A))
, mkSumType (Proxy @(CUid A))
]
-- FIXME: Currently `Uid a` defined in `Guide.Utils` is bridged into a `String`.