<commit_before>import {searchForQuery} from '../search/actions';
import {onProductsResponse, onServerProducts} from './actions';
import {register, unregister} from '../dispatcher';
import {state, productsCursor, totalCursor} from '../state';
import axios from 'axios';
import {Record} from 'immutable';

// Isomorphic store has to be state-less.
const Product = Record({
  title: '',
  productNumber: '',
  normalizedName: ''
});


function storeProductsInState(serverProducts) {
  productsCursor(products => {
    return products.withMutations(list => {
      list.clear();
      serverProducts.forEach((serverProduct) => {
        list.push(new Product({
          title: serverProduct.websiteDescription,
          productNumber: serverProduct.productNumber,
          normalizedName: serverProduct.normalizedName
        }));
      });
    });
  });
}

export const dispatchToken = register(({action, data}) => {
  switch (action) {
    case searchForQuery:
      const {resolve, reject, query} = data;
      if (query.q) {
        onProductsResponse(axios.get('http://localhost:8000/nlbe/api/products?q=' + query.q + '&page=' + query.page)
          .then((response) => {
            return {
              response: response,
              resolve: resolve
            };
          }))
          .catch(reject);
      } else {
        setTimeout(() =>
          onProductsResponse({
            resolve: resolve
          }));
      }
      break;
    case onProductsResponse:
      const {response, resolve} = data;
      if (response && response.data) {
        const productsResponse = response.data;
        storeProductsInState(productsResponse.products);
        totalCursor(() => productsResponse.total);
      } else {
        storeProductsInState([]);
        totalCursor(() => 0);
      }
      resolve();
      break;
  }
});

export function getProducts() {
  return productsCursor();
}

export function getTotal() {
  return totalCursor();
}<commit_msg>Make store code more readable<commit_after>import {searchForQuery} from '../search/actions';
import {onProductsResponse, onServerProducts} from './actions';
import {register, unregister} from '../dispatcher';
import {state, productsCursor, totalCursor} from '../state';
import axios from 'axios';
import {Record} from 'immutable';

// Isomorphic store has to be state-less.
const Product = Record({
  title: '',
  productNumber: '',
  normalizedName: ''
});


function storeProductsInState(serverProducts) {
  productsCursor(products => {
    return products.withMutations(list => {
      list.clear();
      serverProducts.forEach((serverProduct) => {
        list.push(new Product({
          title: serverProduct.websiteDescription,
          productNumber: serverProduct.productNumber,
          normalizedName: serverProduct.normalizedName
        }));
      });
    });
  });
}

export const dispatchToken = register(({action, data}) => {
  switch (action) {
    case searchForQuery:
      const {query} = data;
      if (query.q) {
        onProductsResponse(
          axios.get('http://localhost:8000/nlbe/api/products?q=' + query.q + '&page=' + query.page)
            .then((response) => {
              return {
                response: response,
                resolve: data.resolve
              };
            }))
          .catch(data.reject);
      } else {
        setTimeout(() =>
          onProductsResponse({
            resolve: data.resolve
          }));
      }
      break;
    case onProductsResponse:
      const {response} = data;
      if (response && response.data) {
        const productsResponse = response.data;
        storeProductsInState(productsResponse.products);
        totalCursor(() => productsResponse.total);
      } else {
        storeProductsInState([]);
        totalCursor(() => 0);
      }
      data.resolve();
      break;
  }
});

export function getProducts() {
  return productsCursor();
}

export function getTotal() {
  return totalCursor();
}
{'fraction_non_alphanumeric': 0.13351772692984662, 'fraction_numerical': 0.002514458134272064, 'mean_word_length': 2.536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4984184', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1051, 'n_words': 240}