diff --git a/src/lib/E.re b/src/lib/E.re index 177f4a85..298cb21e 100644 --- a/src/lib/E.re +++ b/src/lib/E.re @@ -211,6 +211,7 @@ module A = { let unsafe_get = Array.unsafe_get; let get = Belt.Array.get; let getBy = Belt.Array.getBy; + let hasBy = (r, fn) => Belt.Array.getBy(r, fn) |> O.isSome; let fold_left = Array.fold_left; let fold_right = Array.fold_right; let concatMany = Belt.Array.concatMany; diff --git a/src/lib/Prop.re b/src/lib/Prop.re index e55aefda..9d240aee 100644 --- a/src/lib/Prop.re +++ b/src/lib/Prop.re @@ -4,7 +4,7 @@ module Value = { | Unselected; type conditional = { - statement: string, + name: string, truthValue: bool, }; @@ -45,7 +45,7 @@ module Value = { |> ReasonReact.string | SelectSingle(r) => r |> ReasonReact.string | ConditionalArray(r) => "Array" |> ReasonReact.string - | Conditional(r) => r.statement |> ReasonReact.string + | Conditional(r) => r.name |> ReasonReact.string | FloatCdf(r) => let cdf: Types.distribution = CdfLibrary.Distribution.fromString(r, 2000); diff --git a/src/lib/ValueForm.re b/src/lib/ValueForm.re index ceaf9d00..ab73f1d3 100644 --- a/src/lib/ValueForm.re +++ b/src/lib/ValueForm.re @@ -3,6 +3,26 @@ let handleChange = (handleChange, event) => handleChange(ReactEvent.Form.target(event)##value); type onChange = option(Value.t) => unit; +module ConditionalReducer = { + type action = + | ADD_OR_UPDATE_CONDITIONAL(Value.conditional) + | REMOVE_CONDITIONAL(Value.conditional); + + let reducer = (items: array(Value.conditional), action: action) => + switch (action) { + | ADD_OR_UPDATE_CONDITIONAL(conditional) => + items->E.A.hasBy(c => c.name == conditional.name) + ? items + |> E.A.fmap((r: Value.conditional) => + r.name == conditional.name ? conditional : r + ) + : E.A.append(items, [|conditional|]) + | REMOVE_CONDITIONAL(conditional) => + items + |> E.A.filter((c: Value.conditional) => c.name != conditional.name) + }; +}; + [@react.component] let make = ( @@ -11,7 +31,80 @@ let make = ~onChange: onChange, ) => { switch (type_.type_, value) { - | (Conditionals(_), r) => "sdfsdf" |> ReasonReact.string + | (Conditionals(l), Some(ConditionalArray(n))) => +