Advertisement
Guest User

React snippet2

a guest
Jan 24th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import cookie from 'react-cookie';
  5. import Listing from './Listing';
  6. import { BubbleLoader, ListingProfileModal } from 'components';
  7. import { changeSearchParams, silentlySaveSearchParams, changeListingPage, setParams } from 'redux/modules/search-params';
  8. import { clear, setResults, setFilterId } from 'redux/modules/search-results';
  9. import { load as loadUser } from 'redux/modules/user';
  10. import { get } from 'lodash';
  11. import Helmet from 'react-helmet';
  12. import { load as loadListing } from 'redux/modules/listing';
  13. import errorBoundary from '../../decorators/ErrorBoundary';
  14.  
  15. @connect(({ listing, searchResults, searchParams, globalConfig, auth, currencies }) => ({
  16.   listing: listing.data,
  17.   search: searchResults.data,
  18.   listing_count: searchResults.listing_count,
  19.   currencies: currencies.data,
  20.   isMobile: globalConfig.isMobile,
  21.   user: auth.user,
  22.   searchParams,
  23.   searchResults
  24. }), {
  25.   clear,
  26.   changeSearchParams,
  27.   silentlySaveSearchParams,
  28.   changeListingPage,
  29.   loadUser,
  30.   setParams,
  31.   setResults,
  32.   setFilterId,
  33.   loadListing
  34. })
  35.  
  36. @errorBoundary()
  37.  
  38. export default class ListingMobile extends Component {
  39.   constructor(props) {
  40.     super(props);
  41.  
  42.     this.state = {
  43.       minVisible: 0,
  44.       maxVisible: 1,
  45.       listing: {},
  46.       currentListing: {},
  47.       isRenderMap: false
  48.     };
  49.   }
  50.  
  51.   componentDidMount() {
  52.     document.getElementsByClassName('wrapper')[0].classList.add('one-listing-mobile');
  53.     document.getElementsByClassName('wsmenucontainer')[0].classList.add('wsmenucontainer-one-listing-mobile');
  54.     const { listing, params, loadListing, loadUser } = this.props;
  55.     if (!listing) {
  56.       loadListing(params ? params.listing_id : Number(location.pathname.split('/')[2])).then((listing) => {
  57.         loadUser(listing.user_id).then(user => {
  58.           const currentListing = { listing, user };
  59.           this.setState({ listing: currentListing, currentListing });
  60.         });
  61.       });
  62.     } else {
  63.       loadUser(listing.user_id).then(user => {
  64.         const currentListing = { listing, user };
  65.         this.setState({ listing: currentListing, currentListing });
  66.       });
  67.     }
  68.  
  69.     __CLIENT__ && window.addEventListener('scroll', () => this.setState({ isRenderMap: true }), { once: true });
  70.   }
  71.  
  72.   componentWillUnmount() {
  73.     document.getElementsByClassName('wrapper')[0].classList.remove('one-listing-mobile');
  74.     document.getElementsByClassName('wsmenucontainer')[0].classList.remove('wsmenucontainer-one-listing-mobile');
  75.   }
  76.  
  77.   render() {
  78.     const { currencies } = this.props;
  79.     const { listing, currentListing, isRenderMap } = this.state;
  80.     const currentCurrency = cookie.load('currency');
  81.     const currencyData = currencies.find(el => el.currency === currentCurrency);
  82.  
  83.     return (
  84.       <div>
  85.         {
  86.           currentListing.listing &&
  87.           <Helmet title={currentListing.listing && `${currentListing.listing.headline}, ${currencyData.prefix}${currentListing.listing.rates.requested_monthly_rate}${currencyData.postfix}, ${currentListing.listing.geo_location.google_formatted_address}` || ''}
  88.                   titleTemplate="%s"
  89.                   link={[{'rel': 'canonical', 'href': `${location.protocol || 'https:'}//${location.host}${location.pathname}`}]}
  90.                   meta={[
  91.                     {
  92.                       name: 'description',
  93.                       content: currentListing.listing && currentListing.listing.description || ''
  94.                     },
  95.                     {
  96.                       property: 'og:title',
  97.                       content: currentListing.listing && `${currentListing.listing.headline}, ${currencyData.prefix}${currentListing.listing.rates.requested_monthly_rate}${currencyData.postfix}, ${currentListing.listing.geo_location.google_formatted_address}` || ''
  98.                     },
  99.                     {
  100.                       property: 'og:description',
  101.                       content: currentListing.listing && currentListing.listing.description || ''
  102.                     },
  103.                     {
  104.                       property: 'og:url',
  105.                       content: `https://www.example.com/listings/${currentListing.listing && currentListing.listing.listing_id}`
  106.                     },
  107.                     {
  108.                       property: 'og:image',
  109.                       content: (currentListing.listing && Array.isArray(currentListing.listing.photos) && !!currentListing.listing.photos.length)
  110.                         ? currentListing.listing.photos[0].path_medium
  111.                         : ''
  112.                     }
  113.                   ]} />
  114.         }
  115.         {
  116.           (!listing.listing || !listing.user) &&
  117.           <BubbleLoader isOverlay style={{ backgroundColor: '#fff' }} />
  118.         }
  119.         { (listing.listing && listing.user) && <Listing listing={listing.listing} listingUser={listing.user} isRenderMap={isRenderMap} /> }
  120.         <ListingProfileModal currencyData={currencyData}
  121.                              listing={currentListing.listing}
  122.                              listingUser={currentListing.user}
  123.                              listingType={get(currentListing, 'listing.service_type') || 1}/>
  124.       </div>
  125.     );
  126.   }
  127. }
  128.  
  129. ListingMobile.propTypes = {
  130.   currencies: PropTypes.array.isRequired,
  131.   listing: PropTypes.object.isRequired,
  132.   loadListing: PropTypes.func.isRequired,
  133.   loadUser: PropTypes.func.isRequired,
  134.   params: PropTypes.object.isRequired
  135. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement