<commit_before>angular.module('voteApp').controller('electionController',
['$scope', 'electionService', 'alertService', 'voteService', 'localStorageService', 'socketIOService',
function($scope, electionService, alertService, voteService, localStorageService, socketIOService) {

    $scope.activeElection = null;
    $scope.selectedAlternative = null;

    // Initialize local storage
    var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
    if (!alreadyVotedElections) localStorageService.set('alreadyVotedElections', []);

    /**
     * Tries to find an active election
     */
    function getActiveElection() {
        return electionService.getElections()
            .success(function(elections) {
                $scope.elections = elections;

                var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
                $scope.elections.some(function(election) {
                    if (election.active && alreadyVotedElections.indexOf(election._id) === -1) {
                        $scope.activeElection = election;
                        return true;
                    }
                    $scope.activeElection = null;
                });
            })
            .error(function(err) {
                alertService.addError(err.message);
            });
    }
    getActiveElection();
    socketIOService.listen('election', getActiveElection);

    /**
     * Add the given electionId to local storage, to ensure that
     * it won't show up the next time the page is loaded
     * @param {Object} electionId
     */
    function addVotedElection(electionId) {
        $scope.activeElection = null;
        var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
        localStorageService.set('alreadyVotedElections', alreadyVotedElections.concat(electionId));
    }

    /**
     * Sets the given alternative to $scope
     * @param  {Object} alternative
     */
    $scope.selectAlternative = function(alternative) {
        $scope.selectedAlternative = alternative;
    };

    /**
     * Persists votes to the backend
     */
    $scope.vote = function() {
        voteService.vote($scope.selectedAlternative._id)
            .success(function(result) {
                alertService.addSuccess('Takk for din stemme!');
                addVotedElection($scope.activeElection._id);
            })
            .error(function(error) {
                switch (error.name) {
                    case 'InactiveElectionError':
                        alertService.addError('Denne avstemningen ser ut til å være deaktivert, vennligst prøv igjen.');
                        getActiveElection();
                        break;
                    case 'AlreadyVotedError':
                        addVotedElection($scope.activeElection._id);
                        alertService.addError('Du kan bare stemme en gang per avstemning!');
                        break;
                    case 'InactiveUserError':
                        alertService.addError('Brukeren din er deaktivert, vennligst henvend deg til skranken.');
                        break;
                    default:
                        alertService.addError('Noe gikk galt, vennligst prøv igjen.');
                }
            });
    };

    /**
     * Helper function to determine if an alternative is selected
     * @param  {Object}  alternative
     * @return {Boolean}
     */
    $scope.isChosen = function(alternative) {
        return alternative === $scope.selectedAlternative;
    };
}]);
<commit_msg>Try loading new elections after voting, no matter the success/error outcome<commit_after>angular.module('voteApp').controller('electionController',
['$scope', 'electionService', 'alertService', 'voteService', 'localStorageService', 'socketIOService',
function($scope, electionService, alertService, voteService, localStorageService, socketIOService) {

    $scope.activeElection = null;
    $scope.selectedAlternative = null;

    // Initialize local storage
    var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
    if (!alreadyVotedElections) localStorageService.set('alreadyVotedElections', []);

    /**
     * Tries to find an active election
     */
    function getActiveElection() {
        return electionService.getElections()
            .success(function(elections) {
                $scope.elections = elections;

                var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
                $scope.elections.some(function(election) {
                    if (election.active && alreadyVotedElections.indexOf(election._id) === -1) {
                        $scope.activeElection = election;
                        return true;
                    }
                    $scope.activeElection = null;
                });
            })
            .error(function(err) {
                alertService.addError(err.message);
            });
    }
    getActiveElection();
    socketIOService.listen('election', getActiveElection);

    /**
     * Add the given electionId to local storage, to ensure that
     * it won't show up the next time the page is loaded
     * @param {Object} electionId
     */
    function addVotedElection(electionId) {
        $scope.activeElection = null;
        var alreadyVotedElections = localStorageService.get('alreadyVotedElections');
        localStorageService.set('alreadyVotedElections', alreadyVotedElections.concat(electionId));
    }

    /**
     * Sets the given alternative to $scope
     * @param  {Object} alternative
     */
    $scope.selectAlternative = function(alternative) {
        $scope.selectedAlternative = alternative;
    };

    /**
     * Persists votes to the backend
     */
    $scope.vote = function() {
        voteService.vote($scope.selectedAlternative._id)
            .success(function(result) {
                alertService.addSuccess('Takk for din stemme!');
                addVotedElection($scope.activeElection._id);
                getActiveElection();
            })
            .error(function(error) {
                getActiveElection();
                switch (error.name) {
                    case 'InactiveElectionError':
                        alertService.addError('Denne avstemningen ser ut til å være deaktivert, vennligst prøv igjen.');
                        break;
                    case 'AlreadyVotedError':
                        addVotedElection($scope.activeElection._id);
                        alertService.addError('Du kan bare stemme en gang per avstemning!');
                        break;
                    case 'InactiveUserError':
                        alertService.addError('Brukeren din er deaktivert, vennligst henvend deg til skranken.');
                        break;
                    default:
                        alertService.addError('Noe gikk galt, vennligst prøv igjen.');
                }
            });
    };

    /**
     * Helper function to determine if an alternative is selected
     * @param  {Object}  alternative
     * @return {Boolean}
     */
    $scope.isChosen = function(alternative) {
        return alternative === $scope.selectedAlternative;
    };
}]);

{'fraction_non_alphanumeric': 0.09921563231044447, 'fraction_numerical': 0.0002752167331773772, 'mean_word_length': 2.039732329569218, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7541959', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1798, 'n_words': 383}