<commit_before>import api from './api'
import queryString from 'query-string'
import {getParsedProductFilter, getProductFilterForCategory, getProductFilterForSearch} from '../shared/actions'
import {PAGE, PRODUCT_CATEGORY, PRODUCT, RESERVED, SEARCH} from '../shared/pageTypes'

const getCurrentPage = path => {
  return api.sitemap.retrieve({ path: path, enabled: true })
    .then(sitemapResponse => {
      if(sitemapResponse.status === 200) {
        return sitemapResponse.json;
      } else if(sitemapResponse.status === 404) {
        return {
          type: 404,
          path: path,
          resource: null
        }
      } else {
        return Promise.reject(`Page response code = ${sitemapResponse.status}`)
      }
    })
}

const getProducts = (currentPage, productFilter) => {
  if (currentPage.type === PRODUCT_CATEGORY || currentPage.type === SEARCH) {
    const filter = getParsedProductFilter(productFilter);
    return api.products.list(filter).then(({status, json}) => json);
  } else {
    return null;
  }
}

const getProduct = currentPage => {
  if (currentPage.type === PRODUCT) {
    return api.products.retrieve(currentPage.resource).then(({status, json}) => json);
  } else {
    return {};
  }
}

const getPage = currentPage => {
  if (currentPage.type === PAGE) {
    return api.pages.retrieve(currentPage.resource).then(({status, json}) => json);
  } else {
    return {};
  }
}

const getThemeSettings = () => {
  return api.theme.settings.retrieve()
  .then(({status, json}) => json)
  .catch(err => ({}));
}

const getAllData = (currentPage, productFilter, cookie) => {
  return Promise.all([
    api.checkoutFields.list().then(({status, json}) => json),
    api.productCategories.list({enabled: true}).then(({status, json}) => json),
    api.ajax.cart.retrieve(cookie).then(({status, json}) => json),
    getProducts(currentPage, productFilter),
    getProduct(currentPage),
    getPage(currentPage),
    getThemeSettings()
  ])
  .then(([
    checkoutFields,
    categories,
    cart,
    products,
    product,
    page,
    themeSettings
  ]) => {
    let categoryDetails = null;
    if (currentPage.type === PRODUCT_CATEGORY) {
      categoryDetails = categories.find(c => c.id === currentPage.resource);
    }
    return {
      checkoutFields,
      categories,
      cart,
      products,
      product,
      page,
      categoryDetails,
      themeSettings
    }
  })
}

const getState = (currentPage, settings, allData, location, productFilter) => {
  const {
    checkoutFields,
    categories,
    cart,
    products,
    product,
    page,
    categoryDetails,
    themeSettings
  } = allData;

  let productsTotalCount = 0;
  let productsHasMore = false;
  let productsMinPrice = 0;
  let productsMaxPrice = 0;
  let productsAttributes = [];

  if(products){
    productsTotalCount = products.total_count;
    productsHasMore = products.has_more;
    productsAttributes = products.attributes;

    if(products.price) {
      productsMinPrice = products.price.min;
      productsMaxPrice = products.price.max;
    }
  }

  const state = { app: {
      settings: settings,
      location: location,
      currentPage: currentPage,
      pageDetails: page,
      categoryDetails: categoryDetails,
      productDetails: product,
      categories: categories,
      products: products && products.data ? products.data : [],
      productsTotalCount: productsTotalCount,
      productsHasMore: productsHasMore,
      productsMinPrice: productsMinPrice,
      productsMaxPrice: productsMaxPrice,
      productsAttributes: productsAttributes,
      paymentMethods: [],
      shippingMethods: [],
      loadingProducts: false,
      loadingMoreProducts: false,
      loadingShippingMethods: false,
      loadingPaymentMethods: false,
      processingCheckout: false,
      productFilter: {
        onSale: null,
        search: productFilter.search || '',
        categoryId: productFilter.categoryId,
        priceFrom: productFilter.priceFrom || 0,
        priceTo: productFilter.priceTo || 0,
        attributes: productFilter.attributes,
        sort: settings.default_product_sorting
      },
      cart: cart,
      order: null,
      checkoutFields: checkoutFields,
      themeSettings: themeSettings
    }
  }

  return state;
}

const getFilter = (currentPage, urlQuery, settings) => {
  let productFilter = {};

  if(currentPage.type === PRODUCT_CATEGORY){
    productFilter = getProductFilterForCategory(urlQuery);
    productFilter.categoryId = currentPage.resource;
    productFilter.sort = settings.default_product_sorting;
  } else if(currentPage.type === SEARCH){
    productFilter = getProductFilterForSearch(urlQuery);
  }

  return productFilter;
}

export const loadState = (req) => {
  const cookie = req.get('cookie');
  const urlPath = req.path;
  const urlQuery = req.url.includes('?') ? req.url.substring(req.url.indexOf('?')) : '';
  const location = {
    hasHistory: false,
    pathname: urlPath,
    search: urlQuery,
    hash: ''
  }

  return Promise.all([
    getCurrentPage(req.path),
    api.settings.retrieve().then(({status, json}) => json)
  ])
  .then(([currentPage, settings]) => {
    const productFilter = getFilter(currentPage, urlQuery, settings);

    return getAllData(currentPage, productFilter, cookie)
    .then(allData => getState(currentPage, settings, allData, location, productFilter));
  })
}
<commit_msg>Remove unused category fields on render<commit_after>import api from './api'
import queryString from 'query-string'
import {getParsedProductFilter, getProductFilterForCategory, getProductFilterForSearch} from '../shared/actions'
import {PAGE, PRODUCT_CATEGORY, PRODUCT, RESERVED, SEARCH} from '../shared/pageTypes'

const getCurrentPage = path => {
  return api.sitemap.retrieve({ path: path, enabled: true })
    .then(sitemapResponse => {
      if(sitemapResponse.status === 200) {
        return sitemapResponse.json;
      } else if(sitemapResponse.status === 404) {
        return {
          type: 404,
          path: path,
          resource: null
        }
      } else {
        return Promise.reject(`Page response code = ${sitemapResponse.status}`)
      }
    })
}

const getProducts = (currentPage, productFilter) => {
  if (currentPage.type === PRODUCT_CATEGORY || currentPage.type === SEARCH) {
    const filter = getParsedProductFilter(productFilter);
    return api.products.list(filter).then(({status, json}) => json);
  } else {
    return null;
  }
}

const getProduct = currentPage => {
  if (currentPage.type === PRODUCT) {
    return api.products.retrieve(currentPage.resource).then(({status, json}) => json);
  } else {
    return {};
  }
}

const getPage = currentPage => {
  if (currentPage.type === PAGE) {
    return api.pages.retrieve(currentPage.resource).then(({status, json}) => json);
  } else {
    return {};
  }
}

const getThemeSettings = () => {
  return api.theme.settings.retrieve()
  .then(({status, json}) => json)
  .catch(err => ({}));
}

const getAllData = (currentPage, productFilter, cookie) => {
  return Promise.all([
    api.checkoutFields.list().then(({status, json}) => json),
    api.productCategories.list({enabled: true}).then(({status, json}) => {
      // delete unused propetries
      return json.map(category => {
        delete category.date_created;
        delete category.date_updated;
        delete category.enabled;
        delete category.slug;
        return category;
      });
    }),
    api.ajax.cart.retrieve(cookie).then(({status, json}) => json),
    getProducts(currentPage, productFilter),
    getProduct(currentPage),
    getPage(currentPage),
    getThemeSettings()
  ])
  .then(([
    checkoutFields,
    categories,
    cart,
    products,
    product,
    page,
    themeSettings
  ]) => {
    let categoryDetails = null;
    if (currentPage.type === PRODUCT_CATEGORY) {
      categoryDetails = categories.find(c => c.id === currentPage.resource);
    }
    return {
      checkoutFields,
      categories,
      cart,
      products,
      product,
      page,
      categoryDetails,
      themeSettings
    }
  })
}

const getState = (currentPage, settings, allData, location, productFilter) => {
  const {
    checkoutFields,
    categories,
    cart,
    products,
    product,
    page,
    categoryDetails,
    themeSettings
  } = allData;

  let productsTotalCount = 0;
  let productsHasMore = false;
  let productsMinPrice = 0;
  let productsMaxPrice = 0;
  let productsAttributes = [];

  if(products){
    productsTotalCount = products.total_count;
    productsHasMore = products.has_more;
    productsAttributes = products.attributes;

    if(products.price) {
      productsMinPrice = products.price.min;
      productsMaxPrice = products.price.max;
    }
  }

  const state = { app: {
      settings: settings,
      location: location,
      currentPage: currentPage,
      pageDetails: page,
      categoryDetails: categoryDetails,
      productDetails: product,
      categories: categories,
      products: products && products.data ? products.data : [],
      productsTotalCount: productsTotalCount,
      productsHasMore: productsHasMore,
      productsMinPrice: productsMinPrice,
      productsMaxPrice: productsMaxPrice,
      productsAttributes: productsAttributes,
      paymentMethods: [],
      shippingMethods: [],
      loadingProducts: false,
      loadingMoreProducts: false,
      loadingShippingMethods: false,
      loadingPaymentMethods: false,
      processingCheckout: false,
      productFilter: {
        onSale: null,
        search: productFilter.search || '',
        categoryId: productFilter.categoryId,
        priceFrom: productFilter.priceFrom || 0,
        priceTo: productFilter.priceTo || 0,
        attributes: productFilter.attributes,
        sort: settings.default_product_sorting
      },
      cart: cart,
      order: null,
      checkoutFields: checkoutFields,
      themeSettings: themeSettings
    }
  }

  return state;
}

const getFilter = (currentPage, urlQuery, settings) => {
  let productFilter = {};

  if(currentPage.type === PRODUCT_CATEGORY){
    productFilter = getProductFilterForCategory(urlQuery);
    productFilter.categoryId = currentPage.resource;
    productFilter.sort = settings.default_product_sorting;
  } else if(currentPage.type === SEARCH){
    productFilter = getProductFilterForSearch(urlQuery);
  }

  return productFilter;
}

export const loadState = (req) => {
  const cookie = req.get('cookie');
  const urlPath = req.path;
  const urlQuery = req.url.includes('?') ? req.url.substring(req.url.indexOf('?')) : '';
  const location = {
    hasHistory: false,
    pathname: urlPath,
    search: urlQuery,
    hash: ''
  }

  return Promise.all([
    getCurrentPage(req.path),
    api.settings.retrieve().then(({status, json}) => json)
  ])
  .then(([currentPage, settings]) => {
    const productFilter = getFilter(currentPage, urlQuery, settings);

    return getAllData(currentPage, productFilter, cookie)
    .then(allData => getState(currentPage, settings, allData, location, productFilter));
  })
}

{'fraction_non_alphanumeric': 0.12288939051918736, 'fraction_numerical': 0.002528216704288939, 'mean_word_length': 3.200227531285552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, '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': '2204234', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3204, 'n_words': 685}