@@ -3,20 +3,21 @@ port module CustomElement exposing (init, subscriptions, update, view)
3
3
-- import Html.Attributes exposing (class, classList)
4
4
-- import Html.Events exposing (onClick)
5
5
6
+ import Dict
6
7
import Html exposing (Html , div , h3 , text )
7
8
import Json.Decode exposing (Value , decodeValue )
8
9
import Json.Encode as Encode
9
10
import Json.Form
10
11
import Json.Form.Config exposing (Config )
11
- import Json.Schema.Definitions
12
- import Json.Value as JsonValue exposing (decoder )
12
+ import Json.Schema.Definitions exposing ( Schema )
13
+ import Json.Value as JsonValue exposing (JsonValue , decoder )
13
14
14
15
15
16
type alias Model =
16
17
{ form : Json . Form . Model
17
18
, config : Config
18
19
, schema : Json . Schema . Definitions . Schema
19
- , editedValue : Maybe JsonValue . JsonValue
20
+ , value : Maybe JsonValue . JsonValue
20
21
}
21
22
22
23
@@ -39,12 +40,30 @@ init v =
39
40
|> decodeValue ( Json . Decode . field " value" JsonValue . decoder)
40
41
|> Result . toMaybe
41
42
in
42
- { form = Json . Form . init config schema value
43
- , editedValue = value
44
- , schema = schema
45
- , config = config
46
- }
47
- ! []
43
+ initForm schema value config
44
+
45
+
46
+ initForm : Schema -> Maybe JsonValue -> Config -> ( Model , Cmd Msg )
47
+ initForm schema value config =
48
+ let
49
+ form =
50
+ value |> Json . Form . init config schema
51
+ in
52
+ ( { form = form
53
+ , value = form. value
54
+ , schema = schema
55
+ , config = config
56
+ }
57
+ , [ ( " value"
58
+ , form. value
59
+ |> Maybe . map JsonValue . encode
60
+ |> Maybe . withDefault Encode . null
61
+ )
62
+ , ( " isValid" , form. errors |> Dict . isEmpty |> Encode . bool )
63
+ ]
64
+ |> Encode . object
65
+ |> valueUpdated
66
+ )
48
67
49
68
50
69
type Msg
@@ -76,11 +95,7 @@ update message model =
76
95
|> decodeValue Json . Schema . Definitions . decoder
77
96
|> Result . withDefault Json . Schema . Definitions . blankSchema
78
97
in
79
- { model
80
- | schema = schema
81
- , form = Json . Form . init model. config schema model. editedValue
82
- }
83
- ! []
98
+ initForm schema model. value model. config
84
99
85
100
ChangeValue v ->
86
101
let
@@ -89,18 +104,14 @@ update message model =
89
104
|> decodeValue JsonValue . decoder
90
105
|> Result . toMaybe
91
106
in
92
- { model
93
- | editedValue = value
94
- , form = Json . Form . init model. config model. schema value
95
- }
96
- ! []
107
+ initForm model. schema value model. config
97
108
98
109
JsonFormMsg msg ->
99
110
let
100
111
( ( m, cmd ) , exMsg ) =
101
112
Json . Form . update msg model. form
102
113
103
- ( editedValue , exCmd ) =
114
+ ( value , exCmd ) =
104
115
case exMsg of
105
116
Json . Form . UpdateValue v isValid ->
106
117
( v
@@ -112,15 +123,15 @@ update message model =
112
123
)
113
124
, ( " isValid" , Encode . bool isValid )
114
125
]
115
- |> value
126
+ |> valueUpdated
116
127
)
117
128
118
129
_ ->
119
- ( model. editedValue , Cmd . none )
130
+ ( model. value , Cmd . none )
120
131
in
121
132
{ model
122
133
| form = m
123
- , editedValue = editedValue
134
+ , value = value
124
135
}
125
136
! [ cmd |> Cmd . map JsonFormMsg , exCmd ]
126
137
@@ -135,7 +146,7 @@ view model =
135
146
port valueChange : (Value -> msg ) -> Sub msg
136
147
137
148
138
- port value : Value -> Cmd msg
149
+ port valueUpdated : Value -> Cmd msg
139
150
140
151
141
152
port schemaChange : (Value -> msg ) -> Sub msg
0 commit comments