on master help sign in
Got encoding working
commit0b9dcd7764f60f53f65a5693600a25fcac87dc83 authoriko <ilyakooo0@gmail.com> dateTue 26 Jul 2022, 16:33 (4 years ago) parent1b25928
9 files changed+100−39
backend/Bridge.purs+17−2 backend/Main.purs+4−1 bridge.yaml+5−1 frontend/Bridge.elm+28−5 frontend/FFI.elm+3−1 frontend/Main.elm+32−27 frontend/Page.elm−2 frontend/Utils.elm+10 spago.dhall+1
backend/Bridge.purs+17−2
@@ -38,9 +38,24 @@ derive instance genericCmd :: Generic Cmd _
3838derive instance eqCmd :: Eq Cmd
3939derive instance ordCmd :: Ord Cmd
4040
41+newtype Sub_Hello =
42+ Sub_Hello {
43+ first :: String
44+ , second :: String
45+ }
46+
47+instance encodeJsonSub_Hello :: EncodeJson Sub_Hello where
48+ encodeJson = genericEncodeAeson Argonaut.defaultOptions
49+instance decodeJsonSub_Hello :: DecodeJson Sub_Hello where
50+ decodeJson = genericDecodeAeson Argonaut.defaultOptions
51+derive instance genericSub_Hello :: Generic Sub_Hello _
52+derive instance eqSub_Hello :: Eq Sub_Hello
53+derive instance ordSub_Hello :: Ord Sub_Hello
54+
4155data Sub =
42− GotEverything String
43− | Hello
56+ Empty
57+ | GotEverything String
58+ | Hello Sub_Hello
4459
4560instance encodeJsonSub :: EncodeJson Sub where
4661 encodeJson = genericEncodeAeson Argonaut.defaultOptions
backend/Main.purs+4−1
@@ -10,6 +10,7 @@ import Bridge as Bridge
1010import Control.Monad.Reader (runReaderT)
1111import Control.Promise as Promise
1212import Data.Either (Either(..))
13+import Data.Interval (second)
1314import Data.Maybe (Maybe(..))
1415import Data.Nullable (null, toNullable)
1516import Effect (Effect)
@@ -69,6 +70,8 @@ main = do
6970 crypto : services.crypto
7071 }
7172
72− Elm.send app Hello
73+ Elm.send app (Bridge.Hello (Bridge.Sub_Hello {first: "f", second: "u"}))
74+
75+ Elm.send app (Bridge.Empty)
7376
7477 log "🍝"
bridge.yaml+5−1
@@ -1,6 +1,10 @@
11Sub:
2−- Hello
2+- Hello:
3+ first: String
4+ second: String
5+
36- GotEverything: String
7+- Empty
48
59Cmd:
610- Login:
frontend/Bridge.elm+28−5
@@ -45,15 +45,37 @@ jsonEncCmd (Login v1) =
4545
4646
4747
48+type alias Sub_Hello =
49+ { first: String
50+ , second: String
51+ }
52+
53+jsonDecSub_Hello : Json.Decode.Decoder ( Sub_Hello )
54+jsonDecSub_Hello =
55+ Json.Decode.succeed (\pfirst psecond -> {first = pfirst, second = psecond})
56+ |> required "first" (Json.Decode.string)
57+ |> required "second" (Json.Decode.string)
58+
59+jsonEncSub_Hello : Sub_Hello -> Value
60+jsonEncSub_Hello val =
61+ Json.Encode.object
62+ [ ("first", Json.Encode.string val.first)
63+ , ("second", Json.Encode.string val.second)
64+ ]
65+
66+
67+
4868type Sub =
49− GotEverything String
50− | Hello
69+ Empty
70+ | GotEverything String
71+ | Hello Sub_Hello
5172
5273jsonDecSub : Json.Decode.Decoder ( Sub )
5374jsonDecSub =
5475 let jsonDecDictSub = Dict.fromList
55− [ ("GotEverything", Json.Decode.lazy (\_ -> Json.Decode.map GotEverything (Json.Decode.string)))
56− , ("Hello", Json.Decode.lazy (\_ -> Json.Decode.succeed Hello))
76+ [ ("Empty", Json.Decode.lazy (\_ -> Json.Decode.succeed Empty))
77+ , ("GotEverything", Json.Decode.lazy (\_ -> Json.Decode.map GotEverything (Json.Decode.string)))
78+ , ("Hello", Json.Decode.lazy (\_ -> Json.Decode.map Hello (jsonDecSub_Hello)))
5779 ]
5880 jsonDecObjectSetSub = Set.fromList []
5981 in decodeSumTaggedObject "Sub" "tag" "contents" jsonDecDictSub jsonDecObjectSetSub
@@ -61,7 +83,8 @@ jsonDecSub =
6183jsonEncSub : Sub -> Value
6284jsonEncSub val =
6385 let keyval v = case v of
86+ Empty -> ("Empty", encodeValue (Json.Encode.list identity []))
6487 GotEverything v1 -> ("GotEverything", encodeValue (Json.Encode.string v1))
65− Hello -> ("Hello", encodeValue (Json.Encode.list identity []))
88+ Hello v1 -> ("Hello", encodeValue (jsonEncSub_Hello v1))
6689 in encodeSumTaggedObject "tag" "contents" keyval val
6790
frontend/FFI.elm+3−1
@@ -2,6 +2,7 @@ port module FFI exposing (getBridge, sendBridge)
22
33import Bridge
44import Json.Decode as D
5+import Json.Encode as E
56
67
78port bridgeSub : (D.Value -> msg) -> Sub msg
@@ -13,7 +14,8 @@ port bridgeCmd : D.Value -> Cmd msg
1314getBridge : (String -> msg) -> (Bridge.Sub -> msg) -> Sub msg
1415getBridge err f =
1516 bridgeSub
16− (D.decodeValue Bridge.jsonDecSub
17+ ((\v -> Debug.log (E.encode 1 v) v)
18+ >> D.decodeValue Bridge.jsonDecSub
1719 >> (\res ->
1820 case res of
1921 Ok a ->
frontend/Main.elm+32−27
@@ -4,8 +4,6 @@ import Bridge
44import Browser
55import FFI exposing (getBridge)
66import Html exposing (..)
7−import Html.Attributes as Attr
8−import Html.Events as Ev
97import Notification
108import Pages.Loader exposing (loader)
119import Pages.Login as Login
@@ -22,12 +20,16 @@ type Msg
2220
2321
2422type alias Model =
25− { messages : List String
26− , notification : Maybe Notification.Config
23+ { notifications : List Notification.Config
2724 , page : PageModel
2825 }
2926
3027
28+appendNotification : Notification.Config -> Model -> Model
29+appendNotification cfg model =
30+ { model | notifications = cfg :: model.notifications }
31+
32+
3133type PageModel
3234 = LoginModel Login.Model
3335 | LoadingPage
@@ -58,19 +60,21 @@ main =
5860 getBridge ShowError
5961 (\msg ->
6062 case msg of
61− Bridge.Hello ->
62− RecieveMessage "Got it!"
63+ Bridge.Hello { first, second } ->
64+ RecieveMessage ("Got it! " ++ first ++ second)
6365
6466 Bridge.GotEverything everything ->
6567 RecieveMessage everything
68+
69+ Bridge.Empty ->
70+ RecieveMessage "Empty"
6671 )
6772 }
6873
6974
7075init : Model
7176init =
72− { messages = []
73− , notification = Nothing
77+ { notifications = []
7478 , page = LoadingPage
7579 }
7680
@@ -84,7 +88,7 @@ view model =
8488 div []
8589 [ Navigation.navigation { back = False, title = title }
8690 , main_ []
87− (maybeList model.notification (Notification.notification CloseNotification)
91+ (maybeList (List.head model.notifications) (Notification.notification CloseNotification)
8892 ++ body
8993 )
9094 ]
@@ -100,37 +104,38 @@ update msg model =
100104 ( { model | page = liftModel mdl }, cmd )
101105
102106 Err err ->
103− ( { model
104− | notification =
105− Just
106− { title = "An error had occured"
107− , message = err
108− , severity = Notification.Error
109− }
110− }
107+ ( appendNotification
108+ { title = "An error had occured"
109+ , message = err
110+ , severity = Notification.Error
111+ }
112+ model
111113 , cmd
112114 )
113115 in
114116 case msg of
115117 RecieveMessage x ->
116− ( { model | messages = x :: model.messages }
118+ ( appendNotification
119+ { title = "Got a message!"
120+ , message = x
121+ , severity = Notification.Info
122+ }
123+ model
117124 , Cmd.none
118125 )
119126
120127 ShowError err ->
121− ( { model
122− | notification =
123− Just
124− { title = "Something went wrong"
125− , message = err
126− , severity = Notification.Error
127− }
128− }
128+ ( appendNotification
129+ { title = "Something went wrong"
130+ , message = err
131+ , severity = Notification.Error
132+ }
133+ model
129134 , Cmd.none
130135 )
131136
132137 CloseNotification ->
133− ( { model | notification = Nothing }
138+ ( { model | notifications = tailEmpty model.notifications }
134139 , Cmd.none
135140 )
136141
frontend/Page.elm−2
@@ -1,9 +1,7 @@
11module Page exposing (..)
22
33import Html exposing (..)
4−import Html.Attributes as Attr exposing (..)
54import Html.Events exposing (..)
6−import Time
75
86
97type alias Env =
frontend/Utils.elm+10
@@ -71,3 +71,13 @@ alignRight inner =
7171flexCenter : List (Html msg) -> Html msg
7272flexCenter =
7373 div [ Attr.class "simple-flex-center" ]
74+
75+
76+tailEmpty : List a -> List a
77+tailEmpty x =
78+ case x of
79+ [] ->
80+ []
81+
82+ _ :: xs ->
83+ xs
spago.dhall+1
@@ -19,6 +19,7 @@ to generate this file without the comments in this block.
1919 , "argonaut-codecs"
2020 , "bw"
2121 , "console"
22+ , "datetime"
2223 , "effect"
2324 , "either"
2425 , "foreign"