/
Action.purs
245 lines (231 loc) · 8.6 KB
/
Action.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
module Action
( simulationPane
) where
import Bootstrap (badge, badgePrimary, btn, btnDanger, btnInfo, btnLink, btnPrimary, btnSecondary, btnSuccess, btnWarning, card, cardBody_, col10_, col2_, col4_, col_, formControl, formGroup_, invalidFeedback_, nbsp, pullRight, row, row_, validFeedback_)
import Control.Monad.Aff.Class (class MonadAff)
import Data.Array (mapWithIndex)
import Data.Array as Array
import Data.Int as Int
import Data.Lens (view)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import Halogen (HTML)
import Halogen.Component (ParentHTML)
import Halogen.ECharts (EChartsEffects)
import Halogen.HTML (ClassName(ClassName), br_, button, code_, div, div_, form, h2_, h3_, input, label, p_, small_, text)
import Halogen.HTML.Elements.Keyed as Keyed
import Halogen.HTML.Events (input_, onClick, onValueInput)
import Halogen.HTML.Events as HE
import Halogen.HTML.Properties (InputType(InputText, InputNumber), class_, classes, disabled, for, placeholder, required, type_, value)
import Halogen.Query as HQ
import Icons (Icon(..), icon)
import Network.RemoteData (RemoteData(Loading, NotAsked, Failure, Success))
import Playground.API (EvaluationResult, SimulatorWallet, _EvaluationResult, _Fn, _FunctionSchema)
import Prelude (map, pure, show, ($), (+), (/=), (<$>), (<<<), (<>))
import Servant.PureScript.Affjax (AjaxError)
import Types (Action(..), ActionEvent(..), Blockchain, ChildQuery, ChildSlot, FormEvent(..), Query(..), SimpleArgument(..), Simulation, _argumentSchema, _functionName, _resultBlockchain, _simulatorWalletWallet)
import Validation (ValidationError, WithPath, addPath, validate)
import Wallet (walletIdPane, walletsPane)
simulationPane ::
forall m aff.
MonadAff (EChartsEffects aff) m
=> Simulation
-> Array SimulatorWallet
-> RemoteData AjaxError EvaluationResult
-> ParentHTML Query ChildQuery ChildSlot m
simulationPane simulation wallets evaluationResult =
div_
[ walletsPane simulation.signatures wallets
, br_
, actionsPane simulation.actions (view (_EvaluationResult <<< _resultBlockchain) <$> evaluationResult)
]
actionsPane :: forall p. Array Action -> RemoteData AjaxError Blockchain -> HTML p Query
actionsPane actions evaluationResult =
div_
[ h2_ [ text "Actions" ]
, p_ [ text "This is your action sequence. Click 'Evaluate' to run these actions against a simulated blockchain." ]
, Keyed.div
[ classes [ ClassName "actions", row ] ]
(Array.snoc (mapWithIndex actionPane actions) addWaitActionPane)
, br_
, row_ [ evaluateActionsPane evaluationResult actions ]
, br_
, div_ [ small_ [ text "Run this set of actions against a simulated blockchain." ] ]
]
actionPane :: forall p. Int -> Action -> Tuple String (HTML p Query)
actionPane index action =
Tuple (show index) $
col4_
[ div [ class_ $ ClassName "action" ]
[ div [ class_ card ]
[ cardBody_
[ div
[ classes [ badge, badgePrimary, ClassName "badge-action" ] ]
[ text $ show (index + 1) ]
, button
[ classes [ btn, btnInfo, pullRight ]
, onClick $ input_ $ ModifyActions $ RemoveAction index
]
[ icon Close ]
, case action of
Action {simulatorWallet, functionSchema} ->
div_
[ h3_
[ walletIdPane (view _simulatorWalletWallet simulatorWallet)
, text ": "
, text $ view (_FunctionSchema <<< _functionName <<< _Fn) functionSchema
]
, actionArgumentForm index $ view (_FunctionSchema <<< _argumentSchema) functionSchema
]
Wait {blocks} ->
div_
[ h3_ [ text "Wait" ]
, row_
[ col_ [ text "Time" ]
, col_ [
input
[ type_ InputNumber
, value $ show blocks
, placeholder "Int"
, onValueInput $ map (HQ.action <<< ModifyActions <<< SetWaitTime index) <<< Int.fromString
]
]
]
]
]
]
]
]
validationClasses ::
forall a.
SimpleArgument
-> Maybe a
-> Array ClassName
validationClasses arg Nothing = [ ClassName "error" ]
validationClasses arg (Just _) = []
actionArgumentForm :: forall p. Int -> Array SimpleArgument -> HTML p Query
actionArgumentForm index arguments =
form [ class_ $ ClassName "was-validated" ]
(Array.mapWithIndex
(\i argument -> PopulateAction index i <$> actionArgumentField ("Field " <> show i) false argument)
arguments)
actionArgumentField ::
forall p. Warn "We're still not handling the Unknowable case."
=> String
-> Boolean
-> SimpleArgument
-> HTML p FormEvent
actionArgumentField context _ arg@(SimpleInt n) =
div_ [
input
[ type_ InputNumber
, class_ formControl
, value $ maybe "" show n
, required true
, placeholder "Int"
, onValueInput $ (Just <<< HQ.action <<< SetIntField <<< Int.fromString)
]
, validationFeedback (addPath context <$> validate arg)
]
actionArgumentField context _ arg@(SimpleString s) =
div_ [
input
[ type_ InputText
, class_ formControl
, value $ fromMaybe "" s
, required true
, placeholder "String"
, onValueInput $ HE.input SetStringField
]
, validationFeedback (addPath context <$> validate arg)
]
actionArgumentField context nested (SimpleTuple (subFieldA /\subFieldB)) =
row_
[ col_ [ SetSubField 1 <$> actionArgumentField "_1" true subFieldA ]
, col_ [ SetSubField 2 <$> actionArgumentField "_2" true subFieldB ]
]
actionArgumentField context nested (SimpleArray schema subFields) =
div_ [(if nested
then Keyed.div [ classes [ ClassName "nested" ] ]
else Keyed.div_) (mapWithIndex subFormContainer subFields)
, button
[ classes [ btn, btnInfo ]
, onClick $ input_ AddSubField
]
[ icon Plus ]
]
where
subFormContainer i field =
show i
/\
formGroup_ [
row_
[ col10_
[ SetSubField i <$> actionArgumentField (show i) true field ]
, col2_
[ button
[ classes [ btn, btnLink ]
, onClick $ input_ (RemoveSubField i)
]
[ icon Trash ]
]
]
]
actionArgumentField context nested (SimpleObject _ subFields) =
(if nested
then div [ classes [ ClassName "nested" ] ]
else div_) $ mapWithIndex (\i field -> map (SetSubField i) (subForm field)) subFields
where
subForm (name /\ arg) =
(formGroup_
[ label [ for name ] [ text name ]
, actionArgumentField name true arg
]
)
actionArgumentField _ _ (Unknowable { context, description }) =
div_ [ text $ "Unsupported: " <> context
, code_ [ text description ]
]
validationFeedback :: forall p i. Array (WithPath ValidationError) -> HTML p i
validationFeedback [] =
validFeedback_ [ nbsp ]
validationFeedback errors =
invalidFeedback_ (div_ <<< pure <<< text <<< show <$> errors)
addWaitActionPane :: forall p. Tuple String (HTML p Query)
addWaitActionPane =
Tuple "add-wait" $
col4_
[ div
[ class_ $ ClassName "add-wait-action" ]
[ div [ class_ card
, onClick $ input_ $ ModifyActions $ AddWaitAction 10
]
[ cardBody_
[ icon Plus
, div_ [ text "Add Wait Action" ]
]
]
]
]
evaluateActionsPane :: forall p. RemoteData AjaxError Blockchain -> Array Action -> HTML p Query
evaluateActionsPane evaluationResult actions =
col_
[ button
[ classes [ btn, btnClass evaluationResult hasErrors ]
, disabled hasErrors
, onClick $ input_ EvaluateActions
]
[ btnText evaluationResult hasErrors ]
]
where
btnClass Loading _ = btnSecondary
btnClass _ true = btnWarning
btnClass (Success _) _ = btnSuccess
btnClass (Failure _) _ = btnDanger
btnClass NotAsked _ = btnPrimary
btnText Loading _ = icon Spinner
btnText _ true = text "Fix Errors"
btnText _ _ = text "Evaluate"
validationErrors = Array.concat $ validate <$> actions
hasErrors = validationErrors /= []