<commit_before>import { 
  SET_FILTER_TEXT, 
  SET_FILTER_GROUP, 
  TOGGLE_TAG, 
  CLEAR_FILTERS 
} from './filters.actions';

export default function (state = getInitialFiltersState(), action) {
  var idx;

  switch(action.type) {
    case SET_FILTER_TEXT:
      return {
        ...state,
        text: action.text
      };
    case SET_FILTER_GROUP:
      let filter = state.filter;

      filter[action.name] = action.value;

      if (action.name === 'category_id') {
        filter["subcategory_id"] = []
      }
      
      if (!filter["scope"]) {
        filter["scope"] = [];
      }

      idx = filter["scope"].indexOf("district");

      if (filter["district"] && filter["district"].length > 0) {
        if (idx === -1) {
          filter["scope"] = [...filter["scope"], "district"];
        }
      } else if (idx >= 0) {
        filter["scope"].splice(idx, 1);
      }

      return {
        ...state,
        filter
      };
    case TOGGLE_TAG:
      let tags;

      idx = state.tags.indexOf(action.tag);

      if (idx === -1) {
        tags = [...state.tags, action.tag];
      } else {
        tags = [
          ...state.tags.slice(0, idx),
          ...state.tags.slice(idx + 1)
        ];
      }

      return {
        ...state,
        tags 
      };
    case CLEAR_FILTERS:
      return {
        filter: {},
        text: "",
        tags: []
      };
  }
  return state;
}

function getInitialFiltersState() {
  let filters = {
        filter: {},
        text: "",
        tags: []
      },
      matchData;

  matchData = location.search.match(/filter=([^&]*)/);

  if (matchData) {
    let filterData = decodeURIComponent(matchData[1]).split(":");

    filterData.map(function (data) {
      let [name, values] = data.split("=");

      values = values.split(",");

      if (name !== "scope" && name !== "other" && name !== "source" && name !==
          'reviewer_status' && name !== "date") {
        values = values.map((x) => parseInt(x, 10));
      }

      filters.filter[name] = values;
    });
  }

  matchData = location.search.match(/search=([^&]*)/)

  if (matchData) {
    filters.text = decodeURIComponent(matchData[1]);
  }

  matchData = location.search.match(/tag=([^&]*)/)<commit_msg>Fix filter reload<commit_after>import { 
  SET_FILTER_TEXT, 
  SET_FILTER_GROUP, 
  TOGGLE_TAG, 
  CLEAR_FILTERS 
} from './filters.actions';

export default function (state = getInitialFiltersState(), action) {
  var idx;

  switch(action.type) {
    case SET_FILTER_TEXT:
      return {
        ...state,
        text: action.text
      };
    case SET_FILTER_GROUP:
      let filter = state.filter;

      filter[action.name] = action.value;

      if (action.name === 'category_id') {
        filter["subcategory_id"] = []
      }
      
      if (!filter["scope"]) {
        filter["scope"] = [];
      }

      idx = filter["scope"].indexOf("district");

      if (filter["district"] && filter["district"].length > 0) {
        if (idx === -1) {
          filter["scope"] = [...filter["scope"], "district"];
        }
      } else if (idx >= 0) {
        filter["scope"].splice(idx, 1);
      }

      return {
        ...state,
        filter
      };
    case TOGGLE_TAG:
      let tags;

      idx = state.tags.indexOf(action.tag);

      if (idx === -1) {
        tags = [...state.tags, action.tag];
      } else {
        tags = [
          ...state.tags.slice(0, idx),
          ...state.tags.slice(idx + 1)
        ];
      }

      return {
        ...state,
        tags 
      };
    case CLEAR_FILTERS:
      return {
        filter: {},
        text: "",
        tags: []
      };
  }
  return state;
}

function getInitialFiltersState() {
  let filters = {
        filter: {},
        text: "",
        tags: []
      },
      matchData;

  matchData = location.search.match(/filter=([^&]*)/);

  if (matchData) {
    let filterData = decodeURIComponent(matchData[1]).split(":");

    filterData.map(function (data) {
      let [name, values] = data.split("=");

      values = values.split(",");

      if (name === "district_id" || name === "category_id" || name === "subcategory_id"){
        values = values.map((x) => parseInt(x, 10));
      }

      filters.filter[name] = values;
    });
  }

  matchData = location.search.match(/search=([^&]*)/)

  if (matchData) {
    filters.text = decodeURIComponent(matchData[1]);
  }

  matchData = location.search.match(/tag=([^&]*)/)
{'fraction_non_alphanumeric': 0.17845876520955387, 'fraction_numerical': 0.004957187922487607, 'mean_word_length': 1.9242424242424243, 'pattern_counts': {'":': 2, '<': 3, '<?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': '5512762', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1400, 'n_words': 265}