import React from 'react'; import classNames from 'classnames'; import './size_chart_v2.scss'; const sizeChartExceptionsListData = [ { sizeScale: 'Generic/P-4XL', size: 'XS', newSize: 'XS/P' } ]; interface SizeChart { chart: any; country_code_or_key: string; selected_country_code: string; size_scale: string; } interface Props { available_sizes: Array; mobile: boolean; product: any; size_chart: SizeChart; } interface State { mobileDropdownOpen: boolean; selectedCountry: string; sizeScale: any; } class SizeChartV2 extends React.Component { private availableSizesIndexes: Array; private womenShoesITSizes: any; constructor(props: Props) { super(props); const selectedCountry = props.size_chart.country_code_or_key === props.size_chart.selected_country_code ? Object.keys(this.props.size_chart.chart).find( country => country !== 'SIZE_SCALE' && country !== 'GENERIC' && country !== props.size_chart.selected_country_code ) : props.size_chart.selected_country_code; this.state = { sizeScale: undefined, selectedCountry, mobileDropdownOpen: false }; this.convertITsizesToEUIT(); this.getAvailableSizes(); } render() { return (
Size Guide -{' '} {this.rmvUnderscoreAndUpCase( Array.isArray(this.props.product.category) ? this.props.product.category[0] : this.props.product.category )} {this.subtitleTextBuilder()}
{this.reorderSizeChart()}
); } componentDidUpdate(prevProps: Props) { if ( prevProps.size_chart.selected_country_code !== this.props.size_chart.selected_country_code ) { this.setState({ selectedCountry: this.props.size_chart.selected_country_code }); } } reorderSizeChart = () => { const [ generic, sizeScale, selectedCountry, ...others ] = this.buildSizesFromProps(); const rings = this.props.product.subcategory.toString().toUpperCase() === 'RINGS'; if (this.props.mobile) { return this.mobileSizeOrganizer( generic, sizeScale, selectedCountry, others, rings ); } return this.desktopSizeOrganizer( generic, sizeScale, selectedCountry, others, rings ); }; rmvUnderscoreAndUpCase = (text: string) => { if (text) { return text.replace('_', ' ').replace(/\w\S*/g, (txt: string) => { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } }; private convertITsizesToEUIT() { this.womenShoesITSizes = this.props.size_chart.size_scale.indexOf('Womens/Shoes') !== -1 && this.props.size_chart.chart['IT']; if (this.womenShoesITSizes) { delete this.props.size_chart.chart['IT']; } } private handleMobileDropdownToggler = () => { this.setState({ mobileDropdownOpen: !this.state.mobileDropdownOpen }); }; private selectCountryForMobileSizeChart = (selectedCountry: string) => () => { this.setState({ selectedCountry }); }; private subtitleTextBuilder = () => ( Size and cut may vary between brands; use this chart as a general guide. This product is based on  {this.props.size_chart.country_code_or_key ? ( this.props.size_chart.country_code_or_key === 'SIZE_SCALE' ? ( {`${this.props.size_chart.size_scale}.`} ) : ( {`${this.props.size_chart.country_code_or_key} size.`} ) ) : ( {`${this.props.size_chart.size_scale}.`} )} ); private getAvailableSizes = () => { const sizeChartExceptionsList = sizeChartExceptionsListData.filter(o => { if (this.state.sizeScale === o.sizeScale) { return o.newSize; } }); const availableSizesList: Array = new Array(); this.props.available_sizes.forEach(sizesObject => { let object = sizesObject; if ( sizeChartExceptionsList.length > 0 && sizeChartExceptionsList[0].size === sizesObject.size ) { object = Object.assign({}, sizesObject); object.size = sizeChartExceptionsList[0].newSize; } availableSizesList.push(object.size); }); const grabIndexesFromSizeScale = (size: string) => { const itemList: Array = new Array(); this.props.size_chart.chart.SIZE_SCALE.forEach( (item: any, index: number) => { if ( ((size === 'XS' || size === 'XS/P') && (item === 'XS' || item === 'XS/P')) || item === size ) { itemList.push(index); } } ); return itemList; }; this.availableSizesIndexes = []; availableSizesList.forEach( availableSize => (this.availableSizesIndexes = this.availableSizesIndexes.concat( grabIndexesFromSizeScale(availableSize) )) ); }; private tableHeaderSetter(title: string, generic: string) { switch (title) { case 'SIZE_SCALE': if ( this.props.size_chart.size_scale.toLowerCase() === 'men/clothing/0-6' || this.props.size_chart.size_scale.toLowerCase() === 'men/generic/3xl' ) { return 'Size'; } return generic ? this.props.size_chart.country_code_or_key : 'Standard'; case 'GENERIC': return 'Standard'; default: return title; } } private getSizeScaleByKey(size: string) { return Object.keys(this.props.size_chart.chart).find(key => key === size); } private buildSizesFromProps = () => { const generic = this.getSizeScaleByKey('GENERIC'); const sizeScale = this.getSizeScaleByKey('SIZE_SCALE'); const others: any = Object.keys(this.props.size_chart.chart).filter( key => key !== generic && key !== sizeScale && key !== this.state.selectedCountry && key !== this.props.size_chart.country_code_or_key ); if (this.womenShoesITSizes) { delete others['IT']; } return [generic, sizeScale, this.state.selectedCountry, ...others]; }; desktopSizeOrganizer = ( generic: string, sizeScale: string, selectedCountry: string, others: any, rings: any ) => { return [generic, sizeScale, selectedCountry, ...others] .filter(Boolean) .map((country: any) => (
{this.tableHeaderSetter(country, generic)} {this.props.size_chart.chart[country].map( (size: string, index: number) => { if (this.availableSizesIndexes.includes(index) || rings) { return ( {size} ); } }, this )}
)); }; mobileSizeOrganizer = ( generic: 'GENERIC' | undefined, sizeScale: 'SIZE_SCALE' | undefined, selectedCountry: string, others: any, rings: any ) => { return [generic, sizeScale, selectedCountry, ...others] .filter(Boolean) .map(t => { if (t === generic || t === sizeScale) { return this.genericAndSizeScaleBuilder(t, rings, generic); } else if (t === selectedCountry) { return this.otherSizesBuilder(generic, sizeScale, t, rings); } }); }; private genericAndSizeScaleBuilder(t: any, rings: any, generic: string) { return (
{this.tableHeaderSetter(t, generic)} {this.props.size_chart.chart[t].map((size: string, index: number) => { if (this.availableSizesIndexes.includes(index) || rings) { return ( {size} ); } }, this)}
); } otherSizesBuilder = ( generic: 'GENERIC' | undefined, sizeScale: 'SIZE_SCALE' | undefined, t: any, rings: any ) => { const source = this.props.size_chart.chart[this.state.selectedCountry]; const content = source.map((size: string, index: number) => { if (this.availableSizesIndexes.includes(index) || rings) { return ( {size} ); } }); return (
{this.state.selectedCountry} {this.mobileListForCountryTitles(generic, sizeScale)}
{content}
); }; mobileListForCountryTitles = ( generic: 'GENERIC' | undefined, sizeScale: 'SIZE_SCALE' | undefined ) => { const listItems = Object.keys(this.props.size_chart.chart).map(key => { if ( key !== generic && key !== sizeScale && key !== this.props.size_chart.country_code_or_key ) { return (
  • {key}
  • ); } }); return (
      {listItems}
    ); }; } export default SizeChartV2;