<commit_before>import update from 'immutability-helper';

import {SEND_ANSWER, TOGGLE_ANSWER, STOP_QUESTION} from '../constants';

const initialState = {
    answers: [
        {
            teamName: 'team1',

        }
    ]
};

const answerReducer = (state = initialState, action) => {
    let index;
    switch (action.type) {
        case SEND_ANSWER:
            //TODO fix this, cannot be used with multiple submissions
            index = -1;
            state.answers.forEach(function (answer, i) {
                if (action.teamName === state.teamName) {
                    index = i;
                }
            });

            // if teamName already exists update the answer
            if (index > -1) {
                return update(state, {
                    answers: {[index]: {$set: action.answer}}
                });
            }
            // if teamName is new add a new answer
            return update(state, {
                answers: {$push: [action.answer]}
            });
        case TOGGLE_ANSWER:
            index = -1;
            state.answers.forEach(function (answer, i) {
                if (action.teamName === answer.teamName) {
                    index = i;
                }
            });

            return update(state, {
                answers: {[index]: {approved: {$set: !state.answers[index].approved}}}
            });
        case STOP_QUESTION:
            return initialState;
        default:
            return state;
    }
};

export default answerReducer;
<commit_msg>kwizmeestert fix only one answer visible while rating answers<commit_after>import update from 'immutability-helper';

import {SEND_ANSWER, TOGGLE_ANSWER, STOP_QUESTION} from '../constants';

const initialState = {
    answers: []
};

const answerReducer = (state = initialState, action) => {
    let index;
    switch (action.type) {
        case SEND_ANSWER:
            index = -1;
            state.answers.forEach(function (answer, i) {
                if (action.answer.teamName === answer.teamName) {
                    index = i;
                }
            });

            // if teamName already exists update the answer
            if (index > -1) {
                return update(state, {
                    answers: {[index]: {$set: action.answer}}
                });
            }
            // if teamName is new add a new answer
            return update(state, {
                answers: {$push: [action.answer]}
            });
        case TOGGLE_ANSWER:
            index = -1;
            state.answers.forEach(function (answer, i) {
                if (action.teamName === answer.teamName) {
                    index = i;
                }
            });

            return update(state, {
                answers: {[index]: {approved: {$set: !state.answers[index].approved}}}
            });
        case STOP_QUESTION:
            return initialState;
        default:
            return state;
    }
};

export default answerReducer;

{'fraction_non_alphanumeric': 0.10989010989010989, 'fraction_numerical': 0.002331002331002331, 'mean_word_length': 1.416733708769107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6190526', 'n_tokens_mistral': 835, 'n_tokens_neox': 798, 'n_words': 204}