<commit_before>/*
 *
 * EnergiesPage reducer
 *
 */

import _ from 'lodash';
import * as constants from './constants';

const initialState = {
  selectedReaction: {},
  matchingReactions: [],
  reactionSystems: [],
  searchSubmitted: false,
  searchParams: {},
  searchString: '',
  filter: {},
  search: {},
  resultSize: 0,
  withGeometry: true,
  simpleSearch: false,
  dbError: false,
  searchQuery: '',
  order: 'desc',
  orderBy: '',
};

let order = 'desc';
let matchingReactions;
let orderBy;


function energiesPageReducer(state = initialState, action) {
  const update = {};
  switch (action.type) {
    case constants.HANDLE_REQUEST_SORT:
      orderBy = action.payload.property;
      if (state.orderBy === orderBy && state.order === 'desc') {
        order = 'asc';
      }

      if (order === 'desc') {
        matchingReactions = state.matchingReactions.sort((a, b) => (b.node[orderBy] < a.node[orderBy] ? -1 : 1));
      } else {
        matchingReactions = state.matchingReactions.sort((a, b) => (b.node[orderBy] > a.node[orderBy] ? -1 : 1));
      }
      return {
        ...state,
        order,<commit_msg>Fixed bug in reducer for sorting both ways: ascending and descending<commit_after>/*
 *
 * EnergiesPage reducer
 *
 */

import _ from 'lodash';
import * as constants from './constants';

const initialState = {
  selectedReaction: {},
  matchingReactions: [],
  reactionSystems: [],
  searchSubmitted: false,
  searchParams: {},
  searchString: '',
  filter: {},
  search: {},
  resultSize: 0,
  withGeometry: true,
  simpleSearch: false,
  dbError: false,
  searchQuery: '',
  order: 'desc',
  orderBy: '',
};

let order;
let matchingReactions;
let orderBy;


function energiesPageReducer(state = initialState, action) {
  const update = {};
  switch (action.type) {
    case constants.HANDLE_REQUEST_SORT:
      order = 'desc';
      orderBy = action.payload.property;
      if (state.orderBy === orderBy && state.order === 'desc') {
        order = 'asc';
      }

      if (order === 'desc') {
        matchingReactions = state.matchingReactions.sort((a, b) => (b.node[orderBy] < a.node[orderBy] ? -1 : 1));
      } else {
        matchingReactions = state.matchingReactions.sort((a, b) => (b.node[orderBy] > a.node[orderBy] ? -1 : 1));
      }
      return {
        ...state,
        order,
{'fraction_non_alphanumeric': 0.15676141257536608, 'fraction_numerical': 0.004306632213608958, 'mean_word_length': 3.019031141868512, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3853905', 'n_tokens_mistral': 768, 'n_tokens_neox': 744, 'n_words': 172}