<commit_before>import React from "react";
import _ from "lodash";

import SurveyRule from "../components/SurveyRule";
import SvgIcon from "../components/svg/SvgIcon";
import {removeEnumerator} from "../utils/generalUtils";
import {mapObject, mapObjectArray, filterObject} from "../utils/sequence";

export default function SurveyCardList(props) {
    const topLevelNodes = mapObjectArray(
        filterObject(props.surveyQuestions, ([_id, sq]) => sq.parentQuestionId === -1),
        ([id, _sq]) => Number(id)
    );
    return topLevelNodes.map((nodeId, idx) => (
        <SurveyCard
            key={nodeId}
            cardNumber={idx + 1}
            inDesignMode={props.inDesignMode}
            newAnswerComponent={props.newAnswerComponent}
            removeAnswer={props.removeAnswer}
            removeQuestion={props.removeQuestion}
            setProjectDetails={props.setProjectDetails}
            surveyQuestionId={nodeId}
            surveyQuestions={props.surveyQuestions}
            surveyRules={props.surveyRules}
            topLevelNodeIds={topLevelNodes}
        />
    ));
}

class SurveyCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showQuestions: true
        };
    }

    swapId = (val, checkVal, swapVal) => (val === checkVal ? swapVal
        : val === swapVal ? checkVal
            : val);

    swapQuestionIds = upOrDown => {
        const {surveyQuestions, surveyQuestionId, topLevelNodeIds} = this.props;
        const newId = topLevelNodeIds[
            this.props.topLevelNodeIds.indexOf(surveyQuestionId) + upOrDown
        ];
        this.props.setProjectDetails({
            surveyQuestions: mapObject(surveyQuestions, ([key, val]) => [
                this.swapId(Number(key), surveyQuestionId, newId),
                {...val, parentQuestionId: this.swapId(val.parentQuestionId, surveyQuestionId, newId)}
            ])
        });
    };

    getSurveyQuestionText = questionId => {
        const {surveyQuestions} = this.props;
        return _.get(surveyQuestions, [questionId, "question"], "");
    };

    getSurveyAnswerText = (questionId, answerId) => {
        const {surveyQuestions} = this.props;
        return _.get(surveyQuestions, [questionId, "answers", answerId, "answer"], "");
    };<commit_msg>CEO-421 Fixing bugs with Rules when reordering questions.<commit_after>import React from "react";
import _ from "lodash";

import SurveyRule from "../components/SurveyRule";
import SvgIcon from "../components/svg/SvgIcon";
import {removeEnumerator} from "../utils/generalUtils";
import {mapObject, mapObjectArray, filterObject} from "../utils/sequence";

export default function SurveyCardList(props) {
    const topLevelNodes = mapObjectArray(
        filterObject(props.surveyQuestions, ([_id, sq]) => sq.parentQuestionId === -1),
        ([id, _sq]) => Number(id)
    );
    return topLevelNodes.map((nodeId, idx) => (
        <SurveyCard
            key={nodeId}
            cardNumber={idx + 1}
            inDesignMode={props.inDesignMode}
            newAnswerComponent={props.newAnswerComponent}
            removeAnswer={props.removeAnswer}
            removeQuestion={props.removeQuestion}
            setProjectDetails={props.setProjectDetails}
            surveyQuestionId={nodeId}
            surveyQuestions={props.surveyQuestions}
            surveyRules={props.surveyRules}
            topLevelNodeIds={topLevelNodes}
        />
    ));
}

class SurveyCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showQuestions: true
        };
    }

    swapId = (val, checkVal, swapVal) => (val === checkVal ? swapVal
        : val === swapVal ? checkVal
            : val);

    swapIdArray = (arr, checkVal, swapVal) => {
        const newArr = [...arr];
        if (newArr.includes(checkVal) && newArr.includes(swapVal)) {
            return newArr;
        } else if (newArr.includes(checkVal)) {
            const idx = newArr.indexOf(checkVal);
            newArr[idx] = swapVal;
            return newArr;
        } else if (newArr.includes(swapVal)) {
            const idx = newArr.indexOf(swapVal);
            newArr[idx] = checkVal;
            return newArr;
        }
        return newArr;
    };

    updateRuleQuestionIds = (rule, oldQuestionId, newQuestionId) => {
        const allIds = new Set([rule.questionId, rule.questionId1, rule.questionId2]
            .concat(rule.questionIds)
            .concat(rule.questionIds1)
            .concat(rule.questionIds2)
            .concat(rule.questionIds2));
        const newRule = {...rule};
        if (allIds.has(oldQuestionId) || allIds.has(newQuestionId)) {
            // Swap for Numeric Range and Text Match Rules
            if (newRule.questionId !== undefined) {
                newRule.questionId = this.swapId(newRule.questionId, oldQuestionId, newQuestionId);
            }
            // Swap for Incompatible Answers Rules
            if (newRule.questionId1 !== undefined && newRule.questionId2 !== undefined) {
                newRule.questionId1 = this.swapId(newRule.questionId1, oldQuestionId, newQuestionId);
                newRule.questionId2 = this.swapId(newRule.questionId2, oldQuestionId, newQuestionId);
            }
            // Swap for Sum of Answers Rules
            if (newRule.questionIds !== undefined) {
                newRule.questionIds = this.swapIdArray(newRule.questionIds, oldQuestionId, newQuestionId);
            }
            // Swap for Matching Sums Rules
            if (newRule.questionIds1 !== undefined && newRule.questionIds2 !== undefined) {
                newRule.questionIds1 = this.swapIdArray(newRule.questionIds1, oldQuestionId, newQuestionId);
                newRule.questionIds2 = this.swapIdArray(newRule.questionIds2, oldQuestionId, newQuestionId);
            }
        }
        return newRule;
    };

    swapQuestionIds = upOrDown => {
        const {surveyRules, surveyQuestions, surveyQuestionId, topLevelNodeIds} = this.props;
        const newId = topLevelNodeIds[
            topLevelNodeIds.indexOf(surveyQuestionId) + upOrDown
        ];
        this.props.setProjectDetails({
            surveyQuestions: mapObject(surveyQuestions, ([key, val]) => [
                this.swapId(Number(key), surveyQuestionId, newId),
                {...val, parentQuestionId: this.swapId(val.parentQuestionId, surveyQuestionId, newId)}
            ]),
            surveyRules: surveyRules.map(rule =>
                this.updateRuleQuestionIds(rule, surveyQuestionId, newId))
        });
    };

    getSurveyQuestionText = questionId => {
        const {surveyQuestions} = this.props;
        return _.get(surveyQuestions, [questionId, "question"], "");
    };

    getSurveyAnswerText = (questionId, answerId) => {
        const {surveyQuestions} = this.props;
        return _.get(surveyQuestions, [questionId, "answers", answerId, "answer"], "");
    };
{'fraction_non_alphanumeric': 0.11666189521900945, 'fraction_numerical': 0.0034354423131978244, 'mean_word_length': 2.4469659595461275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5195011', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1911, 'n_words': 377}