import _ from "lodash" import PropTypes from "prop-types" import React from "react" import $ from "jquery" import aquagenxCBTSVGString from "./AquagenxCBTSVG" import ModalPopupComponent from "@mwater/react-library/lib/ModalPopupComponent" import { getHealthRiskString } from "./aquagenxCBTUtils" import { InnerFormContext } from "../formContext" const possibleCombinations: { [key: string]: { mpn: number, confidence: number, healthRisk: "safe" | "probably_safe" | "possibly_safe" | "possibly_unsafe" | "probably_unsafe" | "unsafe" }} = { "false,false,false,false,false": { mpn: 0.0, confidence: 2.87, healthRisk: "safe" }, "false,false,false,true,false": { mpn: 1.0, confidence: 5.14, healthRisk: "probably_safe" }, "false,false,false,false,true": { mpn: 1.0, confidence: 4.74, healthRisk: "probably_safe" }, "true,false,false,false,false": { mpn: 1.1, confidence: 5.16, healthRisk: "probably_safe" }, "false,true,false,false,false": { mpn: 1.2, confidence: 5.64, healthRisk: "probably_safe" }, "false,false,true,false,false": { mpn: 1.5, confidence: 7.81, healthRisk: "probably_safe" }, "false,false,false,true,true": { mpn: 2.0, confidence: 6.32, healthRisk: "probably_safe" }, "true,false,false,true,false": { mpn: 2.1, confidence: 6.85, healthRisk: "probably_safe" }, "true,false,false,false,true": { mpn: 2.1, confidence: 6.64, healthRisk: "probably_safe" }, "false,true,false,true,false": { mpn: 2.4, confidence: 7.81, healthRisk: "probably_safe" }, "false,true,false,false,true": { mpn: 2.4, confidence: 8.12, healthRisk: "probably_safe" }, "true,true,false,false,false": { mpn: 2.6, confidence: 8.51, healthRisk: "probably_safe" }, "true,false,false,true,true": { mpn: 3.2, confidence: 8.38, healthRisk: "probably_safe" }, "false,true,false,true,true": { mpn: 3.7, confidence: 9.7, healthRisk: "probably_safe" }, "false,false,true,false,true": { mpn: 3.1, confidence: 11.36, healthRisk: "possibly_safe" }, "false,false,true,true,false": { mpn: 3.2, confidence: 11.82, healthRisk: "possibly_safe" }, "true,false,true,false,false": { mpn: 3.4, confidence: 12.53, healthRisk: "possibly_safe" }, "true,true,false,false,true": { mpn: 3.9, confidence: 10.43, healthRisk: "possibly_safe" }, "true,true,false,true,false": { mpn: 4.0, confidence: 10.94, healthRisk: "possibly_safe" }, "false,true,true,false,false": { mpn: 4.7, confidence: 22.75, healthRisk: "possibly_safe" }, "false,false,true,true,true": { mpn: 5.2, confidence: 14.73, healthRisk: "possibly_safe" }, "true,true,false,true,true": { mpn: 5.4, confidence: 12.93, healthRisk: "possibly_safe" }, "true,false,true,false,true": { mpn: 5.6, confidence: 17.14, healthRisk: "possibly_safe" }, "true,false,true,true,false": { mpn: 5.8, confidence: 16.87, healthRisk: "possibly_safe" }, "true,false,true,true,true": { mpn: 8.4, confidence: 21.19, healthRisk: "possibly_safe" }, "false,true,true,false,true": { mpn: 9.1, confidence: 37.04, healthRisk: "possibly_safe" }, "false,true,true,true,false": { mpn: 9.6, confidence: 37.68, healthRisk: "possibly_safe" }, "true,true,true,false,false": { mpn: 13.6, confidence: 83.06, healthRisk: "possibly_unsafe" }, "false,true,true,true,true": { mpn: 17.1, confidence: 56.35, healthRisk: "possibly_unsafe" }, "true,true,true,false,true": { mpn: 32.6, confidence: 145.55, healthRisk: "probably_unsafe" }, "true,true,true,true,false": { mpn: 48.3, confidence: 351.91, healthRisk: "probably_unsafe" }, "true,true,true,true,true": { mpn: 100, confidence: 9435.1, healthRisk: "unsafe" } } export interface AquagenxCBTPopupComponentProps { value?: any questionId: string onSave: any onClose: any formCtx: InnerFormContext } interface AquagenxCBTPopupComponentState { value: any } export default class AquagenxCBTPopupComponent extends React.Component< AquagenxCBTPopupComponentProps, AquagenxCBTPopupComponentState > { main: HTMLDivElement | null = null constructor(props: any) { super(props) const value = _.clone(props.value) || {} if (value.cbt == null) { const cbt = _.clone(possibleCombinations["false,false,false,false,false"]) as any cbt.c1 = cbt.c2 = cbt.c3 = cbt.c4 = cbt.c5 = false value.cbt = cbt } this.state = { value } } componentDidMount() { const main = this.main! $(main) .find("#compartment1") .click((ev: any) => { return this.handleCompartmentClick("c1") }) $(main) .find("#compartment2") .click((ev: any) => { return this.handleCompartmentClick("c2") }) $(main) .find("#compartment3") .click((ev: any) => { return this.handleCompartmentClick("c3") }) $(main) .find("#compartment4") .click((ev: any) => { return this.handleCompartmentClick("c4") }) return $(main) .find("#compartment5") .click((ev: any) => { return this.handleCompartmentClick("c5") }) } handleCompartmentClick(compartmentField: any) { return this.setState((prevState) => { const value = _.clone(prevState.value) value.cbt = _.clone(value.cbt) const cbtValues = value.cbt cbtValues[compartmentField] = !cbtValues[compartmentField] const compartmentValues = [cbtValues.c1, cbtValues.c2, cbtValues.c3, cbtValues.c4, cbtValues.c5] const computedValues = _.clone(possibleCombinations[`${compartmentValues}`]) cbtValues.mpn = computedValues.mpn cbtValues.confidence = computedValues.confidence cbtValues.healthRisk = computedValues.healthRisk return { value } }) } handleSaveClick = () => { return this.props.onSave(this.state.value) } renderStyle() { const mainId = `#cbtPopup${this.props.questionId}` const cbtValues = this.state.value.cbt const compartmentValues = [cbtValues.c1, cbtValues.c2, cbtValues.c3, cbtValues.c4, cbtValues.c5] const compartmentColors = _.map(compartmentValues, function (c) { if (c) { return "#32a89b" } else { return "#ebe7c2" } }) const hoverColors = _.map(compartmentValues, function (c) { if (c) { return "#62c5bb" } else { return "#fcf8d6" } }) return ( ) } renderInfo() { const cbtValues = this.state.value.cbt let { mpn } = cbtValues if (mpn === 100) { mpn = ">100" } return (
{this.props.formCtx.T("MPN/100ml") + ": "}{mpn}
{this.props.formCtx.T("Upper 95% Confidence Interval/100ml") + ": "}{cbtValues.confidence}
{this.props.formCtx.T("Health Risk Category Based on MPN and Confidence Interval") + ": "} {getHealthRiskString(cbtValues.healthRisk, this.props.formCtx.T)}
) } render() { return ( )} header={this.props.formCtx.T("Click on the compartments to change color")} >
{ this.main = c }} id={`cbtPopup${this.props.questionId}`} > {this.renderStyle()}
{this.renderInfo()}
) } }