From 717cc0856a6b6d0205e49263ef0bded6725bda36 Mon Sep 17 00:00:00 2001 From: s0kil Date: Thu, 27 Jul 2023 01:50:30 -0400 Subject: [PATCH] Initial State Is Sent Back To The SSC --- .../Controller/ComponentsController.hs | 12 +++++++++++- IHP/ServerSideComponent/RouterFunctions.hs | 2 ++ IHP/ServerSideComponent/ViewFunctions.hs | 9 +++++---- lib/IHP/static/vendor/ihp-ssc.js | 3 +++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/IHP/ServerSideComponent/Controller/ComponentsController.hs b/IHP/ServerSideComponent/Controller/ComponentsController.hs index d4b5c7506..5ce7f76d7 100644 --- a/IHP/ServerSideComponent/Controller/ComponentsController.hs +++ b/IHP/ServerSideComponent/Controller/ComponentsController.hs @@ -7,7 +7,7 @@ import IHP.ServerSideComponent.ControllerFunctions as SSC import qualified Data.Aeson as Aeson -instance (Component component controller, FromJSON controller) => WSApp (ComponentsController component) where +instance (Component component controller, Aeson.FromJSON component, FromJSON controller, ToJSON controller) => WSApp (ComponentsController component) where initialState = ComponentsController run = do @@ -30,3 +30,13 @@ instance (Component component controller, FromJSON controller) => WSApp (Compone nextState <- SSC.action currentState theAction SSC.setState nextState Left error -> putStrLn (cs error) + Left error -> do + let theState = Aeson.eitherDecode @component actionPayload + + case theState of + Right initialState -> do + SSC.setState initialState + Left error -> do + putStrLn "Failed Parsing Server Side Component Message As JSON" + putStrLn (cs actionPayload) + putStrLn (cs error) \ No newline at end of file diff --git a/IHP/ServerSideComponent/RouterFunctions.hs b/IHP/ServerSideComponent/RouterFunctions.hs index f4045b14a..e50f86722 100644 --- a/IHP/ServerSideComponent/RouterFunctions.hs +++ b/IHP/ServerSideComponent/RouterFunctions.hs @@ -18,8 +18,10 @@ import IHP.ApplicationContext routeComponent :: forall component controller application. ( Typeable component + , FromJSON component , Component component controller , FromJSON controller + , ToJSON controller , InitControllerContext application , Typeable application , ?application :: application diff --git a/IHP/ServerSideComponent/ViewFunctions.hs b/IHP/ServerSideComponent/ViewFunctions.hs index 6b5dd8b17..7487c3f80 100644 --- a/IHP/ServerSideComponent/ViewFunctions.hs +++ b/IHP/ServerSideComponent/ViewFunctions.hs @@ -9,16 +9,17 @@ import IHP.Prelude import IHP.ViewSupport import IHP.ServerSideComponent.Types import IHP.HSX.QQ (hsx) +import qualified Data.Aeson as Aeson import qualified Data.Typeable as Typeable -component :: forall component action. (Component component action, Typeable component) => Html +component :: forall component action. (Component component action, Typeable component, Aeson.ToJSON component) => Html component = componentFromState (initialState @component) -componentFromState :: forall component action. (Component component action, Typeable component) => component -> Html -componentFromState state = [hsx|
{render state}
|] +componentFromState :: forall component action. (Component component action, Typeable component, Aeson.ToJSON component) => component -> Html +componentFromState state = [hsx|
{render state}
|] where path = "/SSC/" <> typeName typeName = (undefined :: component) |> Typeable.typeOf - |> show \ No newline at end of file + |> show diff --git a/lib/IHP/static/vendor/ihp-ssc.js b/lib/IHP/static/vendor/ihp-ssc.js index 0b8215ec4..cc2d076b8 100644 --- a/lib/IHP/static/vendor/ihp-ssc.js +++ b/lib/IHP/static/vendor/ihp-ssc.js @@ -28,6 +28,9 @@ function initalizeSSC(component) { component.connection.onopen = function (event) { if (debugMode) console.log('Connected'); + + // Send Initial State + component.connection.send(component.dataset.initialState) }; component.connection.onclose = function (event) {