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

Fetch CCategoryDetail while routing

to category detail page
This commit is contained in:
Jens Krause 2017-08-05 19:14:26 +03:00
parent 6887d42ae9
commit e9d28f9d9f
No known key found for this signature in database
GPG Key ID: 3B2FAFBCEFA5906D
6 changed files with 30 additions and 18 deletions

View File

@ -9,9 +9,7 @@ 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, CCategoryOverview(..))
-- import Guide.Routes (Route(..))
import Guide.State (haskellCatName)
import Guide.Api.ClientTypes (CCategoryDetail)
import Guide.Types (CGrandCategories, CategoryName(..), Users)
import Lib.IsomorphicFetch (FETCH, fetch)
@ -30,11 +28,13 @@ fetchUsers = do
pure $ either (Left <<< show) decodeJson res
fetchGrandCategories :: forall eff. CategoryName -> Aff (fetch :: FETCH | eff) (Either String CGrandCategories)
fetchGrandCategories (CategoryName cName) = do
res <- attempt <<< fetch $ apiEndpoint <> cName <> "/all-categories"
fetchGrandCategories (CategoryName catName) = do
res <- attempt <<< fetch $ apiEndpoint <> catName <> "/all-categories"
pure $ either (Left <<< show) decodeJson res
fetchCategory :: forall eff. CCategoryOverview -> Aff (fetch :: FETCH | eff) (Either String CCategoryDetail)
fetchCategory (CCategoryOverview cat) = do
res <- attempt <<< fetch $ apiEndpoint <> haskellCatName <> "/category/" <> cat.ccoUid
-- TODO: Use `Uid Category` instead of `String` as second function parameter
-- if we have found a way to bridge `Uid a` properly from `Haskell` to `PS`
fetchCategory :: forall eff. CategoryName -> String -> Aff (fetch :: FETCH | eff) (Either String CCategoryDetail)
fetchCategory (CategoryName catName) catId = do
res <- attempt <<< fetch $ apiEndpoint <> catName <> "/category/" <> catId
pure $ either (Left <<< show) decodeJson res

View File

@ -1,6 +1,6 @@
module Guide.Events where
import Guide.Api.ClientTypes (CCategoryDetail, CCategoryOverview)
import Guide.Api.ClientTypes (CCategoryDetail)
import Data.Either (Either)
import Guide.Routes (Route)
import Guide.Types (CGrandCategories, CategoryName, Users)
@ -13,7 +13,9 @@ data Event
-- API
| RequestGrandCategories CategoryName
| ReceiveGrandCategories (Either String CGrandCategories)
| RequestCategory CCategoryOverview
| RequestCategory CategoryName String -- (String == Uid Category)
-- TODO: ^ Use `Uid Category` instead of `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
| RequestUsers

View File

@ -11,7 +11,9 @@ import Pux.Router (end, lit, router, str)
data Route
= Home
| CategoryOverview CategoryName
| CategoryDetail CategoryName String -- (Uid Category)
| CategoryDetail CategoryName String -- String == (Uid Category)
-- TODO: Use `Uid Category` instead of `String`
-- if we have found a way to bridge `Uid a` properly from `Haskell` to `PS`
| Playground
| NotFound String

View File

@ -4,7 +4,7 @@ import Prelude
import Data.Generic (class Generic, gShow)
import Data.Newtype (class Newtype)
import Guide.Api.ClientTypes (CCategoryDetail(..))
import Guide.Api.ClientTypes (CCategoryDetail)
import Guide.Config (config)
import Guide.Routes (Route, match)
import Guide.Types (CGrandCategories, CategoryName(..), Users)

View File

@ -46,13 +46,13 @@ foldp (ReceiveGrandCategories (Left error)) s@(State st) = noEffects $
, grandCategories = Failure error
}
foldp (RequestCategory cat) (State st) =
foldp (RequestCategory catName catId) (State st) =
{ state: State $ st { currentCategoryDetails = case st.currentCategoryDetails of
Success c -> Refreshing c
_ -> Loading
}
, effects:
[ fetchCategory cat >>= pure <<< Just <<< ReceiveCategory
[ fetchCategory catName catId >>= pure <<< Just <<< ReceiveCategory
]
}
@ -126,10 +126,14 @@ routeEffects (CategoryOverview catName) s@(State st) =
pure Nothing
]}
routeEffects (CategoryDetail catName catId) s@(State st) = noEffects $
State $
st { loaded = false
}
routeEffects (CategoryDetail catName catId) s@(State st) =
{ state:
State $ st { loaded = false
}
, effects: [
pure <<< Just $ RequestCategory catName catId
]
}
routeEffects Playground s@(State st) =
{ state: State $ st { loaded = false }

View File

@ -60,5 +60,9 @@ clientTypes =
, mkSumType (Proxy :: Proxy CMarkdown)
]
-- FIXME: Currently `Uid a` defined in `Guide.Utils` is bridged into a `String`.
-- For example: `Uid Category` on Haskell side is bridged to `String`
-- It would be better to bridge it to a similar `Uid a` type
main :: IO ()
main = writePSTypes path (buildBridge bridge) clientTypes