Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial State Is Sent Back To The SSC #1778

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion IHP/ServerSideComponent/Controller/ComponentsController.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
2 changes: 2 additions & 0 deletions IHP/ServerSideComponent/RouterFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions IHP/ServerSideComponent/ViewFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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|<div class="ihp-ssc" data-path={path}>{render state}</div>|]
componentFromState :: forall component action. (Component component action, Typeable component, Aeson.ToJSON component) => component -> Html
componentFromState state = [hsx|<div class="ihp-ssc" data-path={path} data-initial-state={Aeson.encode state}>{render state}</div>|]
where
path = "/SSC/" <> typeName
typeName = (undefined :: component)
|> Typeable.typeOf
|> show
|> show
3 changes: 3 additions & 0 deletions lib/IHP/static/vendor/ihp-ssc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down