From e9d28f9d9f737bd6fb8d3a8ae68cbd1cf9f28040 Mon Sep 17 00:00:00 2001 From: Jens Krause Date: Sat, 5 Aug 2017 19:14:26 +0300 Subject: [PATCH] Fetch `CCategoryDetail` while routing to category detail page --- front-ps/src/Guide/Api/Http.purs | 16 ++++++++-------- front-ps/src/Guide/Events.purs | 6 ++++-- front-ps/src/Guide/Routes.purs | 4 +++- front-ps/src/Guide/State.purs | 2 +- front-ps/src/Guide/Update.purs | 16 ++++++++++------ src/purescript/Main.hs | 4 ++++ 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/front-ps/src/Guide/Api/Http.purs b/front-ps/src/Guide/Api/Http.purs index 6eade3f..f9d27ac 100644 --- a/front-ps/src/Guide/Api/Http.purs +++ b/front-ps/src/Guide/Api/Http.purs @@ -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 diff --git a/front-ps/src/Guide/Events.purs b/front-ps/src/Guide/Events.purs index 491a7cb..947dd00 100644 --- a/front-ps/src/Guide/Events.purs +++ b/front-ps/src/Guide/Events.purs @@ -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 diff --git a/front-ps/src/Guide/Routes.purs b/front-ps/src/Guide/Routes.purs index 62c0f5f..f538f79 100644 --- a/front-ps/src/Guide/Routes.purs +++ b/front-ps/src/Guide/Routes.purs @@ -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 diff --git a/front-ps/src/Guide/State.purs b/front-ps/src/Guide/State.purs index 7f4947e..1bf32f3 100644 --- a/front-ps/src/Guide/State.purs +++ b/front-ps/src/Guide/State.purs @@ -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) diff --git a/front-ps/src/Guide/Update.purs b/front-ps/src/Guide/Update.purs index d1daa7e..dbb8d41 100644 --- a/front-ps/src/Guide/Update.purs +++ b/front-ps/src/Guide/Update.purs @@ -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 } diff --git a/src/purescript/Main.hs b/src/purescript/Main.hs index 15e0f44..a9efbf9 100644 --- a/src/purescript/Main.hs +++ b/src/purescript/Main.hs @@ -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