From 0462a88ad552614ee6e533ebd92ef759e7943f00 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sun, 9 Feb 2020 21:45:57 +0000 Subject: [PATCH] Autogenerated simple version mostly works --- src/EAFunds/EAFunds_Model.bs.js | 6 ++- src/EAFunds/EAFunds_Model.re | 4 +- src/Prop.re | 82 ++++++++++++++++++++++++--------- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/src/EAFunds/EAFunds_Model.bs.js b/src/EAFunds/EAFunds_Model.bs.js index 4cecfecf..90c32aab 100644 --- a/src/EAFunds/EAFunds_Model.bs.js +++ b/src/EAFunds/EAFunds_Model.bs.js @@ -113,7 +113,8 @@ var model_002 = /* inputTypes : array */[ /* default */2030.0, /* min */2020.0, /* max */2050.0 - ]]), undefined, undefined, undefined, /* () */0) + ]]), undefined, undefined, undefined, /* () */0), + Prop$ProbExample.TypeWithMetadata.currentYear ]; var model_003 = /* outputTypes : array */[]; @@ -176,7 +177,8 @@ function run(p) { function EAFunds_Model$Interface$Form(Props) { return React.createElement(Prop$ProbExample.ModelForm.make, { - combo: Prop$ProbExample.Combo.fromModel(model) + combo: Prop$ProbExample.Combo.fromModel(model), + runModel: run }); } diff --git a/src/EAFunds/EAFunds_Model.re b/src/EAFunds/EAFunds_Model.re index 0babcba0..4040a173 100644 --- a/src/EAFunds/EAFunds_Model.re +++ b/src/EAFunds/EAFunds_Model.re @@ -86,6 +86,7 @@ module Interface = { }), (), ), + TypeWithMetadata.currentYear, |], outputTypes: [||], }; @@ -110,6 +111,7 @@ module Interface = { module Form = { [@react.component] - let make = () => ; + let make = () => + ; }; }; \ No newline at end of file diff --git a/src/Prop.re b/src/Prop.re index 833bb480..0ed00cb2 100644 --- a/src/Prop.re +++ b/src/Prop.re @@ -41,9 +41,9 @@ module Type = { let default = (t: t) => switch (t) { | Year(r) => r.default->Belt.Option.map(p => Value.FloatPoint(p)) + | FloatPoint(r) => r.default->Belt.Option.map(p => Value.FloatPoint(p)) | SelectSingle(r) => r.default->Belt.Option.map(p => Value.SelectSingle(p)) - | FloatPoint(r) => r.default->Belt.Option.map(p => Value.FloatPoint(p)) | FloatCdf => None }; }; @@ -80,15 +80,6 @@ module TypeWithMetadata = { type ts = array(t); - // TODO: Change default here - let currentYear = { - id: "currentyear", - name: "Current Year", - description: None, - type_: FloatPoint({default: None, min: None, max: None}), - assumptionType: ASSUMPTION, - }; - let make = ( ~name, @@ -104,6 +95,18 @@ module TypeWithMetadata = { description, assumptionType, }; + + // TODO: Change default here + let currentYear = + make( + ~id="currentYear", + ~name="Current Year", + ~description=None, + ~type_= + Year({default: Some(2050.), min: Some(2020.0), max: Some(2050.0)}), + ~assumptionType=ASSUMPTION, + (), + ); }; module Model = { @@ -116,7 +119,7 @@ module Model = { module InputTypes = { let keys = (t: t) => - t.inputTypes |> E.A.fmap((r: TypeWithMetadata.t) => r.name); + t.inputTypes |> E.A.fmap((r: TypeWithMetadata.t) => r.id); }; }; @@ -130,9 +133,7 @@ module Combo = { module InputValues = { let defaults = (t: Model.t) => t.inputTypes - |> E.A.fmap((o: TypeWithMetadata.t) => - (o.name, Type.default(o.type_)) - ) + |> E.A.fmap((o: TypeWithMetadata.t) => (o.id, Type.default(o.type_))) |> ValueMap.fromOptionalArray; let isValid = t => @@ -189,36 +190,71 @@ module ValueForm = { )} /> | (FloatPoint(_), Some(FloatPoint(r))) => - Js.Float.toString} /> + Js.Float.toString} + onChange={handleChange(r => + switch (Js.Float.fromString(r)) { + | r => onChange(Some(Value.FloatPoint(r))) + } + )} + /> | (Year(_), _) | (FloatPoint(_), _) => - | (SelectSingle(_), _) => -
{"Single Choice" |> ReasonReact.string}
+ | (SelectSingle(t), Some(SelectSingle(r))) => + }; }; }; +type formState = { + combo: Combo.t, + setCombo: (Combo.t => Combo.t) => unit, + setInputValue: (Combo.t, string, option(Value.t)) => unit, +}; + +let makeHelpers = (combo): formState => { + let (combo, setCombo) = React.useState(() => combo); + let setInputValue = (combo, id, newValue) => + setCombo(_ => Combo.updateInputValue(combo, id, newValue)); + {combo, setCombo, setInputValue}; +}; + module ModelForm = { let handleChange = (handleChange, event) => handleChange(ReactEvent.Form.target(event)##value); [@react.component] - let make = (~combo: Combo.t) => { - let (combo, setCombo) = React.useState(() => combo); + let make = (~combo: Combo.t, ~runModel: Combo.t => option(Value.t)) => { + let formState = makeHelpers(combo);
- {Combo.inputTypeValuePairs(combo) + {Combo.inputTypeValuePairs(formState.combo) |> E.A.fmap(((type_, value)) => - setCombo(_ => - Combo.updateInputValue(combo, type_.id, newValue) - ) + formState.setInputValue(formState.combo, type_.id, newValue) } /> ) |> ReasonReact.array} + {runModel(formState.combo) + |> E.O.fmap(Value.to_string) + |> E.O.default("") + |> ReasonReact.string}
; }; }; \ No newline at end of file