Skip to content

Instantly share code, notes, and snippets.

@note89
Last active July 22, 2016 15:05
Show Gist options
  • Save note89/45dfb0166bfe35d44fb15812c82e0957 to your computer and use it in GitHub Desktop.
Save note89/45dfb0166bfe35d44fb15812c82e0957 to your computer and use it in GitHub Desktop.
processEvent : ERT.OutMsg -> Model -> ( Model, Cmd Msg )
processEvent msg model =
case msg of
ERT.OnNoOp ->
model ! []
ERT.ToPhx phxMsg ->
processPhxTwo (Phx2.toProcessPhx phxMsg Event) model
ERT.OnNavigate url ->
update (Navigate url) model
ERT.OnRemoveEvent eventId ->
update (User <| UT.RemoveWateredEvent eventId) model
ERT.OnWaterEventSuccess event ->
update (User <| UT.AddWateredEvent event) model
ERT.OnGoToLocation place ->
update (GoToLocation place) model
processPhxTwo : Phx2.Msg Msg -> Model -> ( Model, Cmd Msg )
processPhxTwo msg model =
case msg of
Phx2.NoOp ->
model ! []
Phx2.SendMessage ( msgType, channel, payload ) ->
let
push' =
Phoenix.Push.init msgType channel
|> Phoenix.Push.withPayload payload
( phxSocket', phxCmd ) =
Phoenix.Socket.push push' model.phxSocket
in
{ model | phxSocket = phxSocket' } ! [ Cmd.map PhoenixMsg phxCmd ]
Phx2.LeaveChannel channel ->
let
( phxSocket, phxCmd ) =
Phoenix.Socket.leave channel model.phxSocket
in
{ model | phxSocket = phxSocket } ! [ Cmd.map PhoenixMsg phxCmd ]
Phx2.JoinChannel ( events_tuple, channel ) ->
let
payload =
JE.object [ ( "guardian_token", JE.string model.guardian_token ) ]
phxChannel =
Phoenix.Channel.init channel
|> Phoenix.Channel.withPayload payload
phxSocket =
List.foldl (\( event, msg ) socket -> Phoenix.Socket.on event channel msg socket) model.phxSocket events_tuple
( phxSocket', phxCmd ) =
phxSocket
|> Phoenix.Socket.join phxChannel
in
( { model | phxSocket = phxSocket' }, Cmd.map PhoenixMsg phxCmd )
type OutMsg
= OnNoOp
| ToPhx (Phx2.Msg Msg)
module CommentsComponent.State exposing (..)
import CommentsComponent.Types exposing (Msg(..), Model, Comment, Comments, InputType(..), OutMsg(..))
import App.Types as AT
import User.Types as User
import DB.Event.Comment.Append exposing (AppendCommentType, execute)
import Ports exposing (removeThisWhenThatExists, focusWhenExists)
import Phoenix.Socket
import Phoenix.Channel
import Phoenix.Push
import Json.Encode as JE
import Json.Decode as JD exposing ((:=))
import Common.Phx2 as Phx2
initWithData comments grapqhl_id =
{ list = comments
, newComment = Nothing
, inputStatus = Placeholder
, grapqhl_id = grapqhl_id
}
type alias Response =
{ creator_id : String
, creator_name : String
, text : String
}
-- No they will not allways be the same, we want to send data to grapqhl via
-- sockets at some point, and then we want grapqhl_id for example.
type alias Send =
{ creator_id : String
, creator_name : String
, text : String
}
-- chatMessageDecoder : JD.Decoder AppendCommentType
chatMessageDecoder =
JD.object3 Response
("creator_id" := JD.string)
("creator_name" := JD.string)
("text" := JD.string)
createParamList : Send -> List ( String, JE.Value )
createParamList params =
[ ( "creator_id", JE.string params.creator_id )
, ( "creator_name", JE.string params.creator_name )
, ( "text", JE.string params.text )
]
update : Msg -> Model -> User.Model -> ( ( Model, Cmd Msg ), OutMsg )
update msg model user =
case msg of
NoOp ->
( model ! [], OnNoOp )
OnStart ->
update JoinChannel model user
OnClose ->
( model ! [], OnNoOp )
JoinChannel ->
let
msgs =
[ ( "new:msg", ReceiveChatMessage ) ]
channel =
"rooms:" ++ model.grapqhl_id
in
( model ! []
, ToPhx <| Phx2.JoinChannel ( msgs, channel )
)
LeaveChannel ->
( model ! [], ToPhx <| Phx2.LeaveChannel ("rooms:" ++ model.grapqhl_id) )
AddComment ->
let
( ( model', cmdList ), outMsg ) =
case model.newComment of
Just comment ->
let
params =
Send user.id user.name comment.node.text
paramsQL =
AppendCommentType user.id model.grapqhl_id comment.node.text
payload =
(JE.object <| createParamList params)
phxPushTuple =
( "new:msg", "rooms:" ++ model.grapqhl_id, payload )
in
( model ! [ execute paramsQL ], ToPhx <| Phx2.SendMessage phxPushTuple )
Nothing ->
( model ! [], OnNoOp )
in
( model' ! [ cmdList ], outMsg )
ReceiveChatMessage raw ->
case JD.decodeValue chatMessageDecoder raw of
Ok chatMessage ->
let
creator =
{ id = chatMessage.creator_id, name = chatMessage.creator_name }
newComment : Comment
newComment =
Comment { text = chatMessage.text, creator = creator }
newList =
([ newComment ] ++ model.list.edges)
in
( { model | list = (Comments newList) } ! []
, OnNoOp
)
Err error ->
( model ! [], OnNoOp )
CancelComment ->
let
model' =
{ model | newComment = Nothing, inputStatus = Placeholder }
in
( model' ! [ removeThisWhenThatExists ( "input-real", "input-placeholder" ) ], OnNoOp )
SetComment comment ->
let
creator =
{ id = user.id, name = user.name }
newComment : Comment
newComment =
Comment { text = comment, creator = creator }
model' =
{ model | newComment = Just newComment }
in
( model' ! [], OnNoOp )
ShowInputView ->
let
model' =
{ model | inputStatus = Input }
in
( model' ! [ focusWhenExists "input-real" ], OnNoOp )
module Common.Phx2 exposing (..)
import Json.Encode as JE
type Msg msg
= NoOp
| JoinChannel ( List ( String, JE.Value -> msg ), String )
| SendMessage ( String, String, JE.Value )
| LeaveChannel String
tagPhxReceiveWith tagger phxJoinStruct =
let
list =
List.map (\( x, y ) -> ( x, tagger << y )) (fst phxJoinStruct)
in
( list, snd phxJoinStruct )
sendAlongWithTag phxMsg toPhx tag' model =
case phxMsg of
JoinChannel dataStruct ->
( model ! [], toPhx <| (JoinChannel <| tagPhxReceiveWith tag' dataStruct) )
SendMessage msg ->
( model ! [], toPhx <| SendMessage msg )
LeaveChannel channel ->
( model ! [], toPhx <| LeaveChannel channel )
NoOp ->
( model ! [], toPhx <| NoOp )
toProcessPhx msg tag' =
case msg of
JoinChannel phxStruct ->
let
phxStruct1 =
tagPhxReceiveWith tag' phxStruct
in
(JoinChannel phxStruct1)
SendMessage msgTuple ->
SendMessage msgTuple
LeaveChannel channel ->
LeaveChannel channel
NoOp ->
NoOp
processComments : CCT.OutMsg -> Model -> ( ( Model, Cmd Msg ), OutMsg )
processComments msg model =
case msg of
CCT.OnNoOp ->
( model ! [], OnNoOp )
CCT.ToPhx phxMsg ->
sendAlongWithTag phxMsg ToPhx Comments model
update : Msg -> Model -> User.Model -> ( ( Model, Cmd Msg ), OutMsg )
update msg model user =
case msg of
NoOp ->
( model ! [], OnNoOp )
OnStart ->
update (Comments <| CCT.OnStart) model user
Comments subMsg ->
let
( ( commentsComponent, fx ), childOutMsg ) =
CCS.update subMsg model.commentsComponent user
model1 =
{ model | commentsComponent = commentsComponent }
( ( model2, fxProcess ), outMsg ) =
processComments childOutMsg model1
in
( model2 ! [ Cmd.map Comments fx, fxProcess ], outMsg )
processCurrent : ECT.OutMsg -> Model -> UT.Model -> ( ( Model, Cmd Msg ), OutMsg )
processCurrent msg model user =
case msg of
ECT.OnNoOp ->
( model ! [], OnNoOp )
ECT.OnWaterEvent eventId userId ->
update (WaterEvent eventId userId) model user
ECT.OnGoToLocation place ->
update (GoToLocation place) model user
ECT.OnCloseDialog ->
update CloseDialog model user
ECT.OnEditEvent currentEvent ->
update (EditEvent currentEvent) model user
ECT.ToPhx phxMsg ->
Phx2.sendAlongWithTag phxMsg ToPhx Current model
@note89
Copy link
Author

note89 commented Jul 22, 2016

CommentsComponent -> Event Current -> Event Root -> App.elm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment