Guest User

Untitled

a guest
Apr 11th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. port module PortTest exposing (..)
  2.  
  3. import Browser
  4. import Html exposing (..)
  5. import Html.Attributes exposing (..)
  6. import Html.Events exposing (..)
  7. import Json.Decode as D
  8. import Json.Decode exposing (Decoder, map4, field, int, string)
  9.  
  10.  
  11.  
  12.  
  13. -- MAIN
  14.  
  15.  
  16. main : Program () Model Msg
  17. main =
  18.   Browser.element
  19.     { init = init
  20.     , view = view
  21.     , update = update
  22.     , subscriptions = subscriptions
  23.     }
  24.  
  25.  
  26.  
  27.  
  28. -- PORTS
  29.  
  30.  
  31. port sendMessage : String -> Cmd msg
  32. port messageReceiver : (String -> msg) -> Sub msg
  33.  
  34.  
  35.  
  36. -- MODEL
  37.  
  38.  
  39. type alias Model =
  40.   { draft : String
  41.   , messages : List String
  42.   }
  43.  
  44. type alias Quote =
  45.   { quote : String
  46.   , source : String
  47.   , author : String
  48.   , year : Int
  49.   }
  50.  
  51.  
  52. init : () -> ( Model, Cmd Msg )
  53. init flags =
  54.   ( { draft = "", messages = [] }
  55.   , Cmd.none
  56.   )
  57.  
  58.  
  59.  
  60. -- UPDATE
  61.  
  62.  
  63. type Msg
  64.   = DraftChanged String
  65.   | Send
  66.   | Recv String
  67.  
  68.  
  69. -- Use the `sendMessage` port when someone presses ENTER or clicks
  70. -- the "Send" button. Check out index.html to see the corresponding
  71. -- JS where this is piped into a WebSocket.
  72. --
  73. update : Msg -> Model -> ( Model, Cmd Msg )
  74. update msg model =
  75.   case msg of
  76.     DraftChanged draft ->
  77.       ( { model | draft = draft }
  78.       , Cmd.none
  79.       )
  80.  
  81.     Send ->
  82.       ( { model | draft = "" }
  83.       , sendMessage model.draft
  84.       )
  85.  
  86.     Recv message ->
  87.       ( { model | messages = model.messages ++ [ message] }
  88.       , Cmd.none
  89.       )
  90.  
  91.  
  92. -- SUBSCRIPTIONS
  93.  
  94.  
  95. -- Subscribe to the `messageReceiver` port to hear about messages coming in
  96. -- from JS. Check out the index.html file to see how this is hooked up to a
  97. -- WebSocket.
  98. --
  99. subscriptions : Model -> Sub Msg
  100. subscriptions _ = messageReceiver Recv
  101.  
  102.  
  103.  
  104. -- VIEW
  105.  
  106.  
  107. view : Model -> Html Msg
  108. view model =
  109.   div []
  110.     [ h1 [] [ text "Echo Chat" ]
  111.     , ul []
  112.         (List.map (\msg -> li [] [ text msg ]) model.messages)
  113.     , input
  114.         [ type_ "text"
  115.         , placeholder "Draft"
  116.         , onInput DraftChanged
  117.         , on "keydown" (ifIsEnter Send)
  118.         , value model.draft
  119.         ]
  120.         []
  121.     , button [ onClick Send ] [ text "Send" ]
  122.     ]
  123.  
  124.  
  125.  
  126. -- DETECT ENTER
  127.  
  128.  
  129. ifIsEnter : msg -> D.Decoder msg
  130. ifIsEnter msg =
  131.   D.field "key" D.string
  132.     |> D.andThen (\key -> if key == "Enter" then D.succeed msg else D.fail "some other key")
  133.  
  134. quoteDecoder : Decoder Quote
  135. quoteDecoder =
  136.   map4 Quote
  137.     (field "quote" string)
  138.     (field "source" string)
  139.     (field "author" string)
  140.     (field "year" int)
  141.  
Add Comment
Please, Sign In to add comment