Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import React, { useEffect } from 'react';
  2. import { bindActionCreators } from 'redux';
  3. import { injectIntl, intlShape } from 'react-intl';
  4. import { useSelector, useDispatch } from 'react-redux';
  5. import { findIndex } from 'lodash';
  6.  
  7. import { saveParams } from 'redux-core/modules/search';
  8. import { getPlans } from 'redux-core/modules/plans';
  9.  
  10. import { PaddedContainer } from 'components/designSystem';
  11. import FilterOptionHeader from './components/FilterOptionHeader';
  12. import Slider from './components/Slider';
  13.  
  14. const PlansFilter = ({ intl }) => {
  15. const dispatch = useDispatch();
  16.  
  17. const plans = useSelector(state => state.plans.items);
  18. const isLoading = useSelector(state => state.plans.isLoading);
  19.  
  20. const minMonthlyValue =
  21. useSelector(state => parseFloat(state.search.params.min_monthly_value));
  22.  
  23. const maxMonthlyValue =
  24. useSelector(state => parseFloat(state.search.params.max_monthly_value));
  25.  
  26. const getPlansAction = bindActionCreators(getPlans, dispatch);
  27. const saveParamsAction = bindActionCreators(saveParams, dispatch);
  28.  
  29. const handleOnChange = ([min, max]) => {
  30. const minPlan = plans[min];
  31. const maxPlan = plans[max];
  32.  
  33. saveParamsAction({
  34. min_monthly_value: minPlan.value,
  35. max_monthly_value: maxPlan.value,
  36. });
  37. };
  38.  
  39. const getValues = () => {
  40. const minIndex = findIndex(plans, plan => Number(plan.value) === minMonthlyValue);
  41. const maxIndex = findIndex(plans, plan => Number(plan.value) === maxMonthlyValue);
  42.  
  43. const index = (minIndex > -1 && maxIndex > -1) ? [minIndex, maxIndex] : [0, 0];
  44.  
  45. return index;
  46. };
  47.  
  48. useEffect(() => {
  49. if (!plans.length) getPlansAction();
  50. }, []);
  51.  
  52. return (
  53. !isLoading ?
  54. (
  55. <PaddedContainer>
  56. <FilterOptionHeader
  57. title={intl.formatMessage({
  58. id: 'gym_search.filters_modal.plans.title',
  59. })}
  60. />
  61.  
  62. <Slider
  63. plansLength={plans.length}
  64. onChange={handleOnChange}
  65. values={getValues()}
  66. />
  67. </PaddedContainer>
  68. ) : null
  69. );
  70. };
  71.  
  72. PlansFilter.propTypes = {
  73. intl: intlShape.isRequired,
  74. };
  75.  
  76. export default injectIntl(PlansFilter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement