Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.     MoreFilterSection1,
  3.     MoreFilterSection2,
  4. }
  5. from 'modules/business/components/filterForm/MoreBusinessFilterModal/MoreFilterSections'
  6.  
  7. import BusinessActions from 'modules/business/actions'
  8. import BusinessFilterFormUtils from 'modules/business/utils/BusinessFilterFormUtils'
  9. import BusinessValueSelectors, {} from 'modules/business/selectors'
  10. import CategoryFilterFloatModal
  11. from 'modules/business/components/filterForm/CategoryFilterFloatModal'
  12. import CategoryValueSelectors from 'modules/category/selectors'
  13. import { DomainValue } from 'modules/business/constants'
  14. import DropdownButton from 'common/components/buttons/DropdownButton'
  15. import FilterBar from 'modules/business/components/filterForm/FilterBar'
  16. import I18n from 'common/I18n'
  17. import Immutable from 'immutable'
  18. import LocationFilterFloatModal from 'modules/business/components/filterForm/LocationFilterFloatModal'
  19. import MoreBusinessFilterModal
  20. from 'modules/business/components/filterForm/MoreBusinessFilterModal'
  21. import PriceRangeSelect
  22. from 'modules/business/components/filterForm/PriceRangeSelect'
  23. import PropTypes from 'prop-types'
  24. import React from 'react'
  25. import { RegionType } from 'modules/region/constants'
  26. import RegionValueSelectors from 'modules/region/selectors'
  27. import Scroll from 'react-scroll'
  28. import ToggleButton from 'common/components/buttons/ToggleButton'
  29. import cn from 'classnames'
  30. import { connect } from 'react-redux'
  31. import connectToggleFormUiWithForm from 'modules/business/components/filterForm/HoC/connectToggleFormUI/connectToggleFormUiWithForm'
  32. import s from './style.scss'
  33. import tracker from 'common/lib/Tracker'
  34.  
  35. const EnhancedToggleButton = connectToggleFormUiWithForm(
  36.     ToggleButton
  37. )
  38.  
  39. export function createCategoryTitle(form, queryParams) {
  40.     const categories = queryParams && queryParams.filter(param => param.get('name') === 'categories')
  41.  
  42.     if (categories && !categories.isEmpty()) {
  43.         return categories.map(category => category.get('description')).join(', ')
  44.     }
  45.  
  46.     const domain = Number(form.get('domain'))
  47.     if (domain) {
  48.         return domain === DomainValue.FOOD ? I18n.t('business.restaurant.title') : I18n.t('business.beauty-and-spa.title')
  49.     }
  50.  
  51.     return I18n.t('business.categories.label')
  52. }
  53.  
  54. export function createLocationTitle(form, queryParams) {
  55.     if (BusinessFilterFormUtils.isCurrentLocationActive(form)) {
  56.         return I18n.t('region.city-selector-modal.options.current-location')
  57.     }
  58.  
  59.     const regionsFromQueryParams = queryParams.filter(param => param.get('name') === 'regions')
  60.     const selectedRegionsFromQueryParams = regionsFromQueryParams
  61.         .map(regionFromQueryParam => regionFromQueryParam.get('description'))
  62.         .join(', ')
  63.  
  64.     return selectedRegionsFromQueryParams.length > 0 ? selectedRegionsFromQueryParams : I18n.t('business.location.title')
  65. }
  66.  
  67. function createSelectedPriceButtons(form) {
  68.     return form.get('features.priceRanges')
  69.         .map((value, index) =>
  70.             createSelectedToggleButton({
  71.                 form,
  72.                 name: 'features.priceRanges',
  73.                 title: createBaht(value),
  74.                 value,
  75.                 key: index
  76.             }))
  77. }
  78.  
  79. function createSelectedFilterButtons(form) {
  80.     const moreFilters = [...MoreFilterSection1, ...MoreFilterSection2]
  81.     return moreFilters.map((filter, index) =>
  82.             createSelectedToggleButton({ form, name: filter.name, title: filter.title, key: index }))
  83.         .filter(button => !!button)
  84. }
  85.  
  86. function createSelectedToggleButton({ form, name, title, value, key }) {
  87.     if (!form.get(name)) { return null }
  88.  
  89.     return (
  90.         <EnhancedToggleButton
  91.             className={s.selectedToggleButton}
  92.             name={name}
  93.             title={title}
  94.             value={value}
  95.             showCloseButton
  96.             key={key}
  97.         />
  98.     )
  99. }
  100.  
  101. function createBaht(value) {
  102.     let bahts = ''
  103.  
  104.     for (let i = 0; i < value; i++) {
  105.         bahts = bahts.concat('฿')
  106.     }
  107.  
  108.     return bahts
  109. }
  110.  
  111. export function isCategoryDropdownActive(form) {
  112.     return form.get('categories').size > 0
  113. }
  114.  
  115. export function isLocationDropDownActive(region, form) {
  116.     if (BusinessFilterFormUtils.isCurrentLocationActive(form)) {
  117.         return true
  118.     }
  119.  
  120.     if (!region) {
  121.         return false
  122.     }
  123.  
  124.     // Bangkok and Metropolitan should not be active
  125.     if (region.get('id') === 9681) {
  126.         return false
  127.     }
  128.  
  129.     if (region.getIn(['type', 'value']) === RegionType.City) {
  130.         return false
  131.     }
  132.  
  133.     return true
  134. }
  135.  
  136. export function shouldAllowChangeLocation(region) {
  137.     // Bangkok and Metropolitan(9681) should allow to select bangkok's district and neightborhood
  138.     if (!region || region.get('id') === 9681) {
  139.         return true
  140.     }
  141.  
  142.     return region.getIn(['type', 'value']) !== RegionType.ViewGroup
  143. }
  144.  
  145. const resizeFilterBar = () => {
  146.     const ele = document.getElementById('business-filter')
  147.     const bgEle = document.getElementById('business-filter-bg')
  148.     const eleWidth = ele.offsetWidth
  149.     const bodyWidth = document.body.offsetWidth
  150.     const marginX = (bodyWidth - eleWidth) / 2
  151.     const percentX = (marginX / eleWidth) * 100
  152.     bgEle.style.right = `-${percentX}%`
  153.     bgEle.style.left = `-${percentX}%`
  154. }
  155.  
  156. class BusinessFilterBar extends React.PureComponent {
  157.     static propTypes = {
  158.         region: PropTypes.instanceOf(Immutable.Map),
  159.         queryParams: PropTypes.instanceOf(Immutable.List)
  160.     }
  161.  
  162.     componentDidMount() {
  163.         window.addEventListener('resize', resizeFilterBar)
  164.         resizeFilterBar()
  165.     }
  166.  
  167.     openCategoryFilterModal = () => {
  168.         if (createCategoryTitle(this.props.form, this.props.queryParams) !== I18n.t('business.categories.label')) {
  169.             Scroll.scroller.scrollTo('BusinessFilterBar', {
  170.                 duration: 400,
  171.                 delay: 10,
  172.                 smooth: true,
  173.             })
  174.         }
  175.  
  176.         this.props.openCategoryFilterModal(!this.props.isCategoryFilterModalOpen)
  177.         tracker.trackEvent('QuickBusinessFilter', 'Click', 'Category')
  178.     }
  179.  
  180.     openLocationFilterModal = () => {
  181.         if (!shouldAllowChangeLocation(this.props.region)) {
  182.             return
  183.         }
  184.  
  185.         if (createLocationTitle(this.props.form, this.props.queryParams) !== I18n.t('business.location.title')) {
  186.             Scroll.scroller.scrollTo('BusinessFilterBar', {
  187.                 duration: 400,
  188.                 delay: 10,
  189.                 smooth: true,
  190.             })
  191.         }
  192.  
  193.         this.props.openLocationFilterModal(!this.props.isLocationFilterModalOpen)
  194.         tracker.trackEvent('QuickBusinessFilter', 'Click', 'Location')
  195.     }
  196.  
  197.     openMoreBusinessFilterModal = () => {
  198.         this.props.openMoreBusinessFilterModal(!this.props.isMoreBusinessFilterModalOpen)
  199.         tracker.trackEvent('QuickBusinessFilter', 'Click', 'More')
  200.     }
  201.  
  202.     render() {
  203.         const { form, foodCategoryGroups, region, cityIds, queryParams } = this.props
  204.  
  205.         const isFoodFilterForm = BusinessFilterFormUtils.isFoodFilter(form.get('domain'), form.get('categories'), foodCategoryGroups)
  206.  
  207.         const categoryTitle = createCategoryTitle(form, queryParams)
  208.         const locationTitle = createLocationTitle(form, queryParams)
  209.  
  210.         const selectedFilterButtons = createSelectedFilterButtons(form)
  211.         return (
  212.             <Scroll.Element name="BusinessFilterBar">
  213.                 <FilterBar>
  214.                     {/* In iOS, somehow floatModal appear cutoff when it is inside container with '-webkit-overflow-scrolling' */}
  215.                     { /* Mobile - Modal */ }
  216.                     <div className="visible-xs visible-sm">
  217.                         <CategoryFilterFloatModal />
  218.                         <LocationFilterFloatModal queryParams={queryParams} region={region} />
  219.                     </div>
  220.                     <ul className={s.container} id="business-filter" >
  221.                         <li className={s.item}>
  222.                             <div className="visible-md visible-lg"><Scroll.Element name="Category"><CategoryFilterFloatModal /></Scroll.Element></div>
  223.                             <DropdownButton
  224.                                 title={categoryTitle}
  225.                                 variant={isCategoryDropdownActive(form, cityIds) ? 'active' : 'inactive'}
  226.                                 onClick={this.openCategoryFilterModal}
  227.                             />
  228.                         </li>
  229.                         <li className={s.item}>
  230.                             <div className="visible-md visible-lg"><Scroll.Element name="Location"><LocationFilterFloatModal queryParams={queryParams} region={region} /></Scroll.Element></div>
  231.                             <DropdownButton
  232.                                 title={locationTitle}
  233.                                 variant={isLocationDropDownActive(region, form
  234.                                 ) ? 'active' : 'inactive'}
  235.                                 shouldShowDropdownIcon={shouldAllowChangeLocation(region)}
  236.                                 onClick={this.openLocationFilterModal}
  237.                             />
  238.                         </li>
  239.                         {isFoodFilterForm && <li className={cn(s.item, s.showOnlyDesktop)}>
  240.                             <PriceRangeSelect trackEventParams={{category: 'QuickBusinessFilter', action: 'Click'}} />
  241.                         </li>}
  242.                         {isFoodFilterForm && <li className={s.item}>
  243.                             <EnhancedToggleButton
  244.                                 name="features.foodOrder"
  245.                                 iconName="delivery-black"
  246.                                 activeIconName="delivery-white"
  247.                                 title={I18n.t('business.order-delivery.title')}
  248.                                 trackEventParams={{category: 'QuickBusinessFilter', action: 'Click', label: 'Delivery'}}
  249.                             />
  250.                         </li>}
  251.                         <li className={s.item}>
  252.                             <EnhancedToggleButton
  253.                                 name="features.open"
  254.                                 iconName="clock"
  255.                                 activeIconName="clock-white"
  256.                                 title={I18n.t('business.open-now.title')}
  257.                                 trackEventParams={{category: 'QuickBusinessFilter', action: 'Click', label: 'OpenNow'}}
  258.                             />
  259.                         </li>
  260.                    
  261.                         { /* Mobile - Selected Price && More Filters */ }
  262.                         <li className={cn(s.item, s.showOnlyMobile)}>
  263.                             {createSelectedPriceButtons(form)}
  264.                         </li>
  265.                         <li className={cn(s.item, s.showOnlyMobile)}>{selectedFilterButtons}</li>
  266.  
  267.                         <li className={s.item}>
  268.                             <div className={s.advancedSearchButtonDesktopC} onClick={this.openMoreBusinessFilterModal}>
  269.                                 <ToggleButton iconName="black-filter" title={I18n.t('business.more-filters.title')} />
  270.                             </div>
  271.                             <div className={s.advancedSearchButtonMobileC} onClick={this.openMoreBusinessFilterModal}>
  272.                                 <ToggleButton iconName="three-dots" title={I18n.t('business.more-filters.title')} />
  273.                             </div>
  274.                             <MoreBusinessFilterModal />
  275.                         </li>
  276.                     </ul>
  277.  
  278.                     { /* Desktop - Selected More Filters */ }
  279.                     {selectedFilterButtons.length > 0 &&
  280.                     <ul className={cn(s.selectedFilterContainer, s.showOnlyDesktop)}>
  281.                         <li className={s.item}>{selectedFilterButtons}</li>
  282.                     </ul>
  283.                 }
  284.                 </FilterBar>
  285.             </Scroll.Element>
  286.         )
  287.     }
  288. }
  289.  
  290. const mapStateToProps = state => ({
  291.     form: BusinessValueSelectors.getBusinessFilterForm(state),
  292.     cityIds: RegionValueSelectors.getCityIds(state),
  293.     foodCategoryGroups: CategoryValueSelectors.getFoodCategoryGroups(state),
  294.     beautyCategories: CategoryValueSelectors.getBeautyCategories(state),
  295.     isCategoryFilterModalOpen: BusinessValueSelectors.getCategoryFilterModal(state).get('isOpen'),
  296.     isLocationFilterModalOpen: BusinessValueSelectors.getLocationFilterModal(state).get('isOpen'),
  297.     isMoreBusinessFilterModalOpen: BusinessValueSelectors.getMoreBusinessFilterModalOpen(state)
  298. })
  299.  
  300. const mapDispatchToProps = {
  301.     openCategoryFilterModal: BusinessActions.openCategoryFilterModal,
  302.     openLocationFilterModal: BusinessActions.openLocationFilterModal,
  303.     openMoreBusinessFilterModal: BusinessActions.openMoreBusinessFilterModal
  304. }
  305.  
  306. export default connect(mapStateToProps, mapDispatchToProps)(BusinessFilterBar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement