All files / src/react-components nutricalc-req.js

100% Statements 80/80
96.97% Branches 32/33
100% Functions 19/19
100% Lines 80/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561                                                                                                                                                                                        28x 28x 28x 14x 3x     14x   28x                   3x                                               2x                                     2x                           28x 28x 28x 28x   28x 10x 10x 18x 13x 13x         28x 28x       28x 28x                       28x 2x 2x 2x                         26x 13x 13x 13x                           28x 8x 8x 8x                                 28x         11x 11x       11x       11x 11x 11x 11x 1x 1x   11x         11x 11x 1x       11x         11x                               28x 26x 26x 12x                         12x     28x                   28x 28x 28x 28x 13x     28x                                                       1x                           1x                               1x                                           1x                                         1x                           1x                           1x                                                                                         20x                 29x                     4x         6x     2x 2x     1x 1x     5x              
/**
 * @prettier
 */
/*
 * Part of Pleiar.no - a collection of tools for nurses
 *
 * Copyright (C) Eskild Hustvedt 2017-2018
 * Copyright (C) Fagforbundet 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
// @flow
import * as React from "react";
import { connect } from "react-redux";
import { Row, Col, Form, Input, Label } from "reactstrap";
import {
    CalcExplanation,
    CalcExplanationLine,
    CalcExplanationDescription,
    CalcExplanationCalculation,
    CalcExplanationFormula,
    Fraction,
    FractionResult,
    FractionComponent,
} from "./calculators-shared.js";
import { setValue, toggleValue } from "../actions/nutricalc-req";
import { setWeight } from "../actions/nutricalc-shared";
import { numbers } from "../helper.numbers";
import { PageTitle } from "./layout";
import { ExternalLink } from "./shared/links";
import type { ReduxEventOrStringCallback } from "../types/callbacks";
import type {
    nutriCalcBoolFields,
    nutriCalcSetFields,
    nutriCalcReqAgeGroup,
    nutriCalcReqPatientState,
} from "../reducers/nutricalc-req";
import type { CalcResultWithExplanation } from "../types/calculator-types";
 
// Props for the class itself (ie. nothing)
type NutriCalcReqOwnProps = {||};
// Redux dispatch function props
type NutriCalcReqReduxDispatch = {|
    onEnumSettingUpdate: (
        nutriCalcSetFields,
        nutriCalcReqPatientState | nutriCalcReqAgeGroup,
    ) => void,
    onTempUpdate: ReduxEventOrStringCallback,
    onWeightUpdate: (SyntheticEvent<HTMLInputElement>) => void,
    onBooleanUpdate: (nutriCalcBoolFields) => mixed,
|};
// Props from redux state
type NutriCalcReqPropsFromRedux = {|
    weight: number | "",
    ageGroup: string,
    febrile: boolean,
    skinny: boolean,
    temp: number,
    round: boolean,
    patientState: string,
|};
 
/**
 * Props for {@link NutriCalcReq}
 */
type NutriCalcReqProps = {|
    ...NutriCalcReqOwnProps,
    ...NutriCalcReqPropsFromRedux,
    ...NutriCalcReqReduxDispatch,
|};
 
/**
 * Calculates the required calories per day for a patient
 */
class NutriCalcReq extends React.PureComponent<NutriCalcReqProps> {
    /**
     * Renders the "additional values" part of the UI
     */
    renderAdditionalValues(): React.Node {
        let febrileColor: string = "";
        let febrileFieldColor: string = "";
        if (this.props.febrile) {
            if (this.props.temp === "" || this.props.temp < 38) {
                febrileFieldColor = "is-invalid";
            }
        } else {
            febrileColor = "text-muted";
        }
        return (
            <div>
                <Row>
                    <Col className="align-self-center form-inline form-check">
                        <Label check className="ml-3 ml-sm-0">
                            <Input
                                name="febrileState"
                                type="checkbox"
                                checked={this.props.febrile}
                                onChange={() => {
                                    this.props.onBooleanUpdate("febrile");
                                }}
                            />{" "}
                            <b>Febril</b>&nbsp;
                            <span
                                className={"d-none d-inline-md " + febrileColor}
                            >
                                med ein temperatur på
                            </span>
                            &nbsp;
                        </Label>
                        <div
                            className={
                                "align-self-center form-inline " + febrileColor
                            }
                        >
                            <Input
                                type="number"
                                step="0.1"
                                min="38"
                                max="41"
                                className={"mr-2 ml-2 " + febrileFieldColor}
                                value={this.props.temp}
                                onChange={(e) => {
                                    this.props.onTempUpdate(e);
                                }}
                                disabled={!this.props.febrile}
                                id="temp"
                            />
                            <Label htmlFor="temp" className="mr-2">
                                grader
                            </Label>
                        </div>
                    </Col>
                </Row>
                <Row>
                    <Col className="form-inline form-check">
                        <Label check className="ml-3 ml-sm-0">
                            <Input
                                name="skinnyState"
                                type="checkbox"
                                checked={this.props.skinny}
                                onChange={() => {
                                    this.props.onBooleanUpdate("skinny");
                                }}
                            />
                            Pasienten er&nbsp;<b>mager</b>
                        </Label>
                    </Col>
                </Row>
            </div>
        );
    }
    /**
     * Perform the calculation
     */
    getResult(): CalcResultWithExplanation {
        const descr = [];
        let kcal: number = 0;
        let basePerKG: number = 33;
        let baseGroup: string = "oppegåande pasientar";
 
        if (this.props.patientState == "bed") {
            basePerKG = 29;
            baseGroup = "sengeliggjande pasientar";
        } else if (this.props.patientState == "building") {
            basePerKG = 40;
            baseGroup = "pasientar i ein oppbygjande fase";
        }
 
        // We permit an empty string as the weight, so we pretend it's 0 if
        // it's an empty string
        const weight = this.props.weight === "" ? 0 : this.props.weight;
        const base = numbers.conditionalRound(
            weight * basePerKG,
            this.props.round,
        );
        kcal += base;
        descr.push(
            <CalcExplanationLine key="basisbehov">
                <CalcExplanationDescription>
                    Basisbehov {basePerKG} kcal/kg for {baseGroup}
                </CalcExplanationDescription>
                <CalcExplanationCalculation>
                    {weight} × 33 = {base}
                </CalcExplanationCalculation>
                <CalcExplanationFormula>vekt × 33</CalcExplanationFormula>
            </CalcExplanationLine>,
        );
 
        if (this.props.ageGroup == "18-30") {
            const prev = kcal;
            kcal = numbers.conditionalRound(kcal * 1.1, this.props.round);
            descr.push(
                <CalcExplanationLine key="18-30">
                    <CalcExplanationDescription>
                        Ekstrabehov 10% for 18-30
                    </CalcExplanationDescription>
                    <CalcExplanationCalculation>
                        {prev} × 1.10 = {kcal}
                    </CalcExplanationCalculation>
                    <CalcExplanationFormula>
                        basisbehov × 1.10
                    </CalcExplanationFormula>
                </CalcExplanationLine>,
            );
        } else if (this.props.ageGroup == "70+") {
            const prev = kcal;
            kcal -= numbers.conditionalRound(base * 0.1, this.props.round);
            descr.push(
                <CalcExplanationLine key="70%">
                    <CalcExplanationDescription>
                        Redusert behov på 10% for 70+
                    </CalcExplanationDescription>
                    <CalcExplanationCalculation>
                        basisbehov × 0.90
                    </CalcExplanationCalculation>
                    <CalcExplanationFormula>
                        {prev} × 0.90 = {kcal}
                    </CalcExplanationFormula>
                </CalcExplanationLine>,
            );
        }
        if (this.props.skinny) {
            const prev = kcal;
            kcal += numbers.conditionalRound(base * 0.1, this.props.round);
            descr.push(
                <CalcExplanationLine key="ekstraMager">
                    <CalcExplanationDescription>
                        Ekstrabehov 10% for magre pasientar
                    </CalcExplanationDescription>
                    <CalcExplanationCalculation>
                        <FractionComponent>{prev} + </FractionComponent>
                        <Fraction top={base} bottom="10" />
                        <FractionResult>= {kcal}</FractionResult>
                    </CalcExplanationCalculation>
                    <CalcExplanationFormula>
                        <FractionComponent>behov + </FractionComponent>
                        <Fraction top="basisbehov" bottom="10" />
                    </CalcExplanationFormula>
                </CalcExplanationLine>,
            );
        }
        if (
            this.props.febrile &&
            this.props.temp !== "" &&
            this.props.temp >= 38
        ) {
            const prev = kcal;
            const temp = numbers.conditionalRound(
                this.props.temp - 37.0,
                this.props.round,
            );
            kcal += numbers.conditionalRound(
                base * 0.1 * temp,
                this.props.round,
            );
            const calculation: Array<React.Node> = [];
            let wrapRight: string = "";
            let wrapLeft: string = "";
            if (temp !== 1) {
                wrapLeft = " (";
                wrapRight = ") ";
            }
            calculation.push(
                <FractionComponent key="left">
                    {prev} +{wrapLeft}
                </FractionComponent>,
            );
            calculation.push(<Fraction key="base/10" top={base} bottom="10" />);
            if (temp !== 1) {
                calculation.push(
                    <FractionResult key="tempAbove1">× {temp}</FractionResult>,
                );
            }
            calculation.push(
                <FractionResult key="result">
                    {wrapRight} = {kcal}
                </FractionResult>,
            );
            descr.push(
                <CalcExplanationLine key="ekstraFebril">
                    <CalcExplanationDescription>
                        Ekstrabehov 10% pr. grad over 37
                    </CalcExplanationDescription>
                    <CalcExplanationCalculation>
                        {calculation}
                    </CalcExplanationCalculation>
                    <CalcExplanationFormula>
                        <FractionComponent>behov + </FractionComponent>
                        <Fraction top="basisbehov" bottom="10" />× grader over
                        37
                    </CalcExplanationFormula>
                </CalcExplanationLine>,
            );
        }
        if (this.props.round) {
            const rounded = numbers.roundNearestTen(kcal);
            if (rounded != kcal) {
                descr.push(
                    <CalcExplanationLine key="rounding">
                        <CalcExplanationDescription>
                            Rund av
                        </CalcExplanationDescription>
                        <CalcExplanationCalculation>
                            {kcal} ≈ {rounded}
                        </CalcExplanationCalculation>
                        <CalcExplanationFormula>
                            (rund av til nærmaste 10)
                        </CalcExplanationFormula>
                    </CalcExplanationLine>,
                );
                kcal = rounded;
            }
        }
        return {
            result: kcal,
            description: <CalcExplanation>{descr}</CalcExplanation>,
        };
    }
    /**
     * Main rendering
     */
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        const additionalValues = this.renderAdditionalValues();
        const result = this.getResult();
        let veierLabel: string = "Vekt";
        if (this.props.patientState == "building") {
            veierLabel = "Ynskjeleg vekt";
        }
 
        return (
            <div>
                <PageTitle title="Ernæringskalkulator - behov" />
                <Row>
                    <Col>
                        <h3 className="d-md-none">Energibehov</h3>
                        <h3 className="d-none d-md-block">
                            Ernæringskalkulator - energibehov
                        </h3>
                    </Col>
                </Row>
                <Row>
                    <Col>
                        <h4 className="d-none d-md-block">Pasientdata</h4>
                    </Col>
                </Row>
                <Form className="smallInputElements boldColLabels">
                    <Row>
                        <Label for="patientState" sm="3" md="2">
                            Pasienten er
                        </Label>
                        <Col className="align-self-center form-inline form-check">
                            <Label check className="mr-2 ml-3 ml-0-sm">
                                <Input
                                    type="radio"
                                    name="patientState"
                                    checked={this.props.patientState == "bed"}
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "patientState",
                                            "bed",
                                        );
                                    }}
                                />{" "}
                                sengeliggjande
                            </Label>
                            <Label check className="mr-2 ml-3 ml-0-sm">
                                <Input
                                    type="radio"
                                    name="patientState"
                                    checked={this.props.patientState == "up"}
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "patientState",
                                            "up",
                                        );
                                    }}
                                />{" "}
                                oppegåande
                            </Label>
                            <Label check className="mr-2 ml-3 ml-0-sm">
                                <Input
                                    type="radio"
                                    name="patientState"
                                    checked={
                                        this.props.patientState == "building"
                                    }
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "patientState",
                                            "building",
                                        );
                                    }}
                                />{" "}
                                i ein oppbyggjande fase (målet er vektoppgang)
                            </Label>
                        </Col>
                    </Row>
                    <Row>
                        <Label for="weight" xs="3" sm="3" md="2">
                            {veierLabel}
                        </Label>
                        <Col className="form-inline form-check">
                            <Input
                                type="number"
                                min="1"
                                max="300"
                                className="mr-2"
                                value={this.props.weight}
                                onChange={(e) => {
                                    this.props.onWeightUpdate(e);
                                }}
                                id="weight"
                            />{" "}
                            kg
                        </Col>
                    </Row>
                    <Row>
                        <Label for="ageGroup" xs="3" sm="3" md="2">
                            <span className="d-none d-md-block">
                                Aldersgruppe
                            </span>
                            <span className="d-md-none">Alder</span>
                        </Label>
                        <Col className="align-self-center form-inline form-check">
                            <Label check className="mr-2 ml-3 ml-sm-0">
                                <Input
                                    type="radio"
                                    name="ageGroup"
                                    checked={this.props.ageGroup == "18-30"}
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "ageGroup",
                                            "18-30",
                                        );
                                    }}
                                />{" "}
                                18-30
                            </Label>
                            <Label check className="mr-2 ml-3 ml-sm-0">
                                <Input
                                    type="radio"
                                    name="ageGroup"
                                    checked={this.props.ageGroup == "31-69"}
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "ageGroup",
                                            "31-69",
                                        );
                                    }}
                                />{" "}
                                31-69
                            </Label>
                            <Label check className="mr-2 ml-3 ml-sm-0">
                                <Input
                                    type="radio"
                                    name="ageGroup"
                                    checked={this.props.ageGroup == "70+"}
                                    onChange={() => {
                                        this.props.onEnumSettingUpdate(
                                            "ageGroup",
                                            "70+",
                                        );
                                    }}
                                />{" "}
                                70+
                            </Label>
                        </Col>
                    </Row>
                    {additionalValues}
                </Form>
                <Row className="mt-4">
                    <Col>
                        <h4>Resultat</h4>
                        Pasienten har behov for{" "}
                        <b id="nutriCalcReqResult">{result.result} kcal/døgn</b>
                    </Col>
                </Row>
                {result.description}
                <Row className="text-secondary mt-4">
                    <Col>
                        <h5>Kjelde</h5>
                        <small>
                            Formelen er henta frå s.12 i: Statens ernæringsråd
                            (1995).{" "}
                            <i>
                                <ExternalLink
                                    className="text-secondary"
                                    href="https://www.nb.no/items/URN:NBN:no-nb_digibok_2013070205012"
                                >
                                    Statens ernæringsråds retningslinjer for
                                    kostholdet i helseinstitusjoner
                                </ExternalLink>
                            </i>
                            . Oslo: Universitetsforlaget.
                        </small>
                    </Col>
                </Row>
            </div>
        );
    }
}
 
const NutriCalcReqContainer: React.AbstractComponent<NutriCalcReqOwnProps> =
    connect<
        NutriCalcReqProps,
        NutriCalcReqOwnProps,
        NutriCalcReqPropsFromRedux,
        NutriCalcReqReduxDispatch,
        _,
        _,
    >(
        (state): NutriCalcReqPropsFromRedux => {
            return {
                weight: state.nutriCalc.shared.weight,
                ageGroup: state.nutriCalc.req.ageGroup,
                patientState: state.nutriCalc.req.patientState,
                febrile: state.nutriCalc.req.febrile,
                skinny: state.nutriCalc.req.skinny,
                temp: state.nutriCalc.req.temp,
                round: state.global.round,
            };
        },
        (dispatch): NutriCalcReqReduxDispatch => {
            return {
                onEnumSettingUpdate(
                    key: nutriCalcSetFields,
                    val: nutriCalcReqPatientState | nutriCalcReqAgeGroup,
                ) {
                    dispatch(setValue(key, val));
                },
                onTempUpdate(ev: SyntheticEvent<HTMLInputElement> | string) {
                    const value = numbers.floatishInRangeFromThing(ev, 0, 41);
                    dispatch(setValue("temp", value));
                },
                onWeightUpdate(ev: SyntheticEvent<HTMLInputElement>) {
                    const value = numbers.floatishInRangeFromThing(ev, 0, 400);
                    dispatch(setWeight(value));
                },
                onBooleanUpdate(key: nutriCalcBoolFields) {
                    dispatch(toggleValue(key));
                },
            };
        },
    )(NutriCalcReq);
 
export default NutriCalcReqContainer;