diff --git a/src/EAFunds/EAFunds_Model.bs.js b/src/EAFunds/EAFunds_Model.bs.js index 90c32aab..a1718b0d 100644 --- a/src/EAFunds/EAFunds_Model.bs.js +++ b/src/EAFunds/EAFunds_Model.bs.js @@ -2,7 +2,6 @@ var Block = require("bs-platform/lib/js/block.js"); var Curry = require("bs-platform/lib/js/curry.js"); -var React = require("react"); var Math$ProbExample = require("../Math.bs.js"); var Prop$ProbExample = require("../Prop.bs.js"); @@ -74,6 +73,76 @@ function go(group, year, output) { ])]); } +function convertChoice(s) { + switch (s) { + case "animal" : + return /* Fund */[/* ANIMAL_WELFARE */0]; + case "globalHealth" : + return /* Fund */[/* GLOBAL_HEALTH */1]; + case "longTerm" : + return /* Fund */[/* LONG_TERM_FUTURE */2]; + case "meta" : + return /* Fund */[/* META */3]; + default: + return /* All */0; + } +} + +function convertOutput(s) { + if (s === "donations") { + return /* DONATIONS */0; + } else { + return /* PAYOUTS */1; + } +} + +function run(p) { + var partial_arg = p[/* inputValues */1]; + var partial_arg$1 = Prop$ProbExample.ValueMap.get; + var get = function (param) { + return partial_arg$1(partial_arg, param); + }; + var match = Curry._1(get, "Fund"); + var match$1 = Curry._1(get, "Year"); + var match$2 = Curry._1(get, "Output"); + if (match !== undefined) { + var match$3 = match; + switch (match$3.tag | 0) { + case /* SelectSingle */0 : + if (match$1 !== undefined) { + var match$4 = match$1; + switch (match$4.tag | 0) { + case /* FloatPoint */1 : + if (match$2 !== undefined) { + var match$5 = match$2; + switch (match$5.tag | 0) { + case /* SelectSingle */0 : + return go(convertChoice(match$3[0]), match$4[0], convertOutput(match$5[0])); + case /* FloatPoint */1 : + case /* FloatCdf */2 : + return ; + + } + } else { + return ; + } + case /* SelectSingle */0 : + case /* FloatCdf */2 : + return ; + + } + } else { + return ; + } + case /* FloatPoint */1 : + case /* FloatCdf */2 : + return ; + + } + } + +} + var model_002 = /* inputTypes : array */[ Prop$ProbExample.TypeWithMetadata.make("Fund", /* SelectSingle */Block.__(0, [/* record */[ /* options : :: */[ @@ -114,7 +183,23 @@ var model_002 = /* inputTypes : array */[ /* min */2020.0, /* max */2050.0 ]]), undefined, undefined, undefined, /* () */0), - Prop$ProbExample.TypeWithMetadata.currentYear + Prop$ProbExample.TypeWithMetadata.currentYear, + Prop$ProbExample.TypeWithMetadata.make("Output", /* SelectSingle */Block.__(0, [/* record */[ + /* options : :: */[ + /* record */[ + /* id */"donations", + /* name */"Donations" + ], + /* :: */[ + /* record */[ + /* id */"funding", + /* name */"Funding" + ], + /* [] */0 + ] + ], + /* default */"Output" + ]]), undefined, undefined, undefined, /* () */0) ]; var model_003 = /* outputTypes : array */[]; @@ -123,74 +208,15 @@ var model = /* record */[ /* name */"Calculate the payments and payouts of EA Funds based on existing data.", /* author */"George Harrison", model_002, - model_003 + model_003, + /* run */run ]; -function convertChoice(s) { - switch (s) { - case "animal" : - return /* Fund */[/* ANIMAL_WELFARE */0]; - case "globalHealth" : - return /* Fund */[/* GLOBAL_HEALTH */1]; - case "longTerm" : - return /* Fund */[/* LONG_TERM_FUTURE */2]; - case "meta" : - return /* Fund */[/* META */3]; - default: - return /* All */0; - } -} - -function run(p) { - var partial_arg = p[/* inputValues */1]; - var partial_arg$1 = Prop$ProbExample.ValueMap.get; - var get = function (param) { - return partial_arg$1(partial_arg, param); - }; - var match = Curry._1(get, "Fund"); - var match$1 = Curry._1(get, "Year"); - if (match !== undefined) { - var match$2 = match; - switch (match$2.tag | 0) { - case /* SelectSingle */0 : - if (match$1 !== undefined) { - var match$3 = match$1; - switch (match$3.tag | 0) { - case /* FloatPoint */1 : - return go(convertChoice(match$2[0]), match$3[0], /* DONATIONS */0); - case /* SelectSingle */0 : - case /* FloatCdf */2 : - return ; - - } - } else { - return ; - } - case /* FloatPoint */1 : - case /* FloatCdf */2 : - return ; - - } - } - -} - -function EAFunds_Model$Interface$Form(Props) { - return React.createElement(Prop$ProbExample.ModelForm.make, { - combo: Prop$ProbExample.Combo.fromModel(model), - runModel: run - }); -} - -var Form = { - make: EAFunds_Model$Interface$Form -}; - var Interface = { - model: model, convertChoice: convertChoice, + convertOutput: convertOutput, run: run, - Form: Form + model: model }; exports.PayoutsIfAround = PayoutsIfAround; diff --git a/src/EAFunds/EAFunds_Model.re b/src/EAFunds/EAFunds_Model.re index 4040a173..346fa714 100644 --- a/src/EAFunds/EAFunds_Model.re +++ b/src/EAFunds/EAFunds_Model.re @@ -57,6 +57,34 @@ let go = (group: group, year: float, output: output) => { module Interface = { open Prop; + let convertChoice = (s: string) => + switch (s) { + | "animal" => Fund(ANIMAL_WELFARE) + | "globalHealth" => Fund(GLOBAL_HEALTH) + | "longTerm" => Fund(LONG_TERM_FUTURE) + | "meta" => Fund(META) + | _ => All + }; + + let convertOutput = (s: string) => + switch (s) { + | "donations" => DONATIONS + | _ => PAYOUTS + }; + + let run = (p: Combo.t) => { + let get = Prop.ValueMap.get(p.inputValues); + switch (get("Fund"), get("Year"), get("Output")) { + | ( + Some(SelectSingle(fund)), + Some(FloatPoint(intendedYear)), + Some(SelectSingle(output)), + ) => + Some(go(convertChoice(fund), intendedYear, convertOutput(output))) + | _ => None + }; + }; + let model: Model.t = { name: "Calculate the payments and payouts of EA Funds based on existing data.", author: "George Harrison", @@ -87,31 +115,20 @@ module Interface = { (), ), TypeWithMetadata.currentYear, + TypeWithMetadata.make( + ~name="Output", + ~type_= + SelectSingle({ + default: Some("Output"), + options: [ + {name: "Donations", id: "donations"}, + {name: "Funding", id: "funding"}, + ], + }), + (), + ), |], outputTypes: [||], - }; - - let convertChoice = (s: string) => - switch (s) { - | "animal" => Fund(ANIMAL_WELFARE) - | "globalHealth" => Fund(GLOBAL_HEALTH) - | "longTerm" => Fund(LONG_TERM_FUTURE) - | "meta" => Fund(META) - | _ => All - }; - - let run = (p: Combo.t) => { - let get = Prop.ValueMap.get(p.inputValues); - switch (get("Fund"), get("Year")) { - | (Some(SelectSingle(fund)), Some(FloatPoint(intendedYear))) => - Some(go(convertChoice(fund), intendedYear, DONATIONS)) - | _ => None - }; - }; - - module Form = { - [@react.component] - let make = () => - ; + run, }; }; \ No newline at end of file diff --git a/src/Index.bs.js b/src/Index.bs.js index f65028d8..768d15f2 100644 --- a/src/Index.bs.js +++ b/src/Index.bs.js @@ -2,10 +2,13 @@ var React = require("react"); var ReactDOMRe = require("reason-react/src/ReactDOMRe.js"); +var Prop$ProbExample = require("./Prop.bs.js"); var EAFunds_Model$ProbExample = require("./EAFunds/EAFunds_Model.bs.js"); ((import('./styles/index.css'))); -ReactDOMRe.renderToElementWithId(React.createElement(EAFunds_Model$ProbExample.Interface.Form.make, { }), "app"); +ReactDOMRe.renderToElementWithId(React.createElement(Prop$ProbExample.ModelForm.make, { + model: EAFunds_Model$ProbExample.Interface.model + }), "app"); /* Not a pure module */ diff --git a/src/Index.re b/src/Index.re index 9f620a7e..8f1d3a66 100644 --- a/src/Index.re +++ b/src/Index.re @@ -1,2 +1,5 @@ [%bs.raw {|import('./styles/index.css')|}]; -ReactDOMRe.renderToElementWithId(, "app"); \ No newline at end of file +ReactDOMRe.renderToElementWithId( + , + "app", +); \ No newline at end of file diff --git a/src/Prop.re b/src/Prop.re index 0ed00cb2..1a5bb706 100644 --- a/src/Prop.re +++ b/src/Prop.re @@ -115,6 +115,12 @@ module Model = { author: string, inputTypes: array(TypeWithMetadata.t), outputTypes: array(TypeWithMetadata.t), + run: combo => option(Value.t), + } + and combo = { + model: t, + inputValues: ValueMap.t, + outputValues: ValueMap.t, }; module InputTypes = { @@ -124,11 +130,7 @@ module Model = { }; module Combo = { - type t = { - model: Model.t, - inputValues: ValueMap.t, - outputValues: ValueMap.t, - }; + type t = Model.combo; module InputValues = { let defaults = (t: Model.t) => @@ -136,17 +138,17 @@ module Combo = { |> E.A.fmap((o: TypeWithMetadata.t) => (o.id, Type.default(o.type_))) |> ValueMap.fromOptionalArray; - let isValid = t => + let isValid = (t: t) => t.model |> Model.InputTypes.keys |> E.A.fmap(ValueMap.get(t.inputValues)) |> Belt.Array.some(_, E.O.isNone); - let update = (t, key: string, onUpdate: option(Value.t)) => + let update = (t: t, key: string, onUpdate: option(Value.t)) => ValueMap.update(t.inputValues, key, onUpdate); }; - let updateInputValue = (t, k, u) => { + let updateInputValue = (t: t, k, u) => { ...t, inputValues: InputValues.update(t, k, u), }; @@ -157,7 +159,7 @@ module Combo = { (i, ValueMap.get(t.inputValues, i.id)) ); - let fromModel = (t: Model.t) => { + let fromModel = (t: Model.t): t => { model: t, inputValues: InputValues.defaults(t), outputValues: InputValues.defaults(t), @@ -236,8 +238,8 @@ module ModelForm = { handleChange(ReactEvent.Form.target(event)##value); [@react.component] - let make = (~combo: Combo.t, ~runModel: Combo.t => option(Value.t)) => { - let formState = makeHelpers(combo); + let make = (~model: Model.t) => { + let formState = makeHelpers(Combo.fromModel(model));
{Combo.inputTypeValuePairs(formState.combo) |> E.A.fmap(((type_, value)) => @@ -251,7 +253,7 @@ module ModelForm = { /> ) |> ReasonReact.array} - {runModel(formState.combo) + {model.run(formState.combo) |> E.O.fmap(Value.to_string) |> E.O.default("") |> ReasonReact.string}