/* eslint-disable lodash/prefer-lodash-method */
import React from 'react';
import PropTypes from 'prop-types';
import {Text, View} from 'react-native';
import {filter, toNumber, isNil, sum, isNaN} from 'lodash';

import QuestionText from './QuestionText';
import styles from './styles';

const getTotal = (section, question, callback) => {
    const addends = filter(
        question.fieldsToAdd,
        field => !isNil(section[field])
    ).map(
        field => toNumber(section[field])
    );
    let total = sum(addends);
    if (isNaN(total)) {
        total = 0;
    }
    callback({[question.name]: total});
    return total;
};

const Total = ({section, question, onChange}) => (
    <View style={styles.rowContainer}>
        <QuestionText question={question}/>
        <Text>{getTotal(section, question, onChange)}</Text>
    </View>
);

Total.propTypes = {
    section: PropTypes.shape({}).isRequired,
    question: PropTypes.shape({}).isRequired,
    onChange: PropTypes.func.isRequired
};

export default Total;

@@ -571,16 +571,90 @@
 ;%0A    %7D%0A
+%0A    if (section%5Bquestion.name%5D === total) %7B%0A        return total;%0A    %7D%0A%0A
     call

fix total callback (#19)