import React from 'react'; import { get } from 'lodash'; import { connect } from "react-redux"; import { NAME } from "../../../state"; import { Value as StyledValue } from "../../../primitives/Summary"; import { RenderableNode, State } from "../../../index"; import { isOfType } from "../../../utils/is-of-type"; function hasAllValues(values: Array, data: State) { return values.reduce( (acc, currentValue) => get(data, currentValue) !== undefined && acc, true ); } type Props = { node: RenderableNode data: State } function Sum({ node, data }: Props) { if (!isOfType(node, ["Sum"])) { return null } const { minimum, operations, values } = node; if (!hasAllValues(values, data)) { return * Mangler verdi for utregning; } const sum = values.reduce((accumulator, currentValue, currentIndex) => { if (operations && operations[currentIndex] === "-") { return Math.max(accumulator - get(data, currentValue), minimum!); } else if (operations && operations[currentIndex] === "*") { return Math.max(accumulator * get(data, currentValue), minimum!); } else if (operations && operations[currentIndex] === "/") { return Math.max(accumulator / get(data, currentValue), minimum!); } return Math.max(accumulator + get(data, currentValue), minimum!); }, 0); return {sum}; } const ConnectedSum = connect((state: State) => ({ data: state[NAME], }))(Sum); export default ConnectedSum;