React - goBack() function

0 votes
198 views
added May 17, 2019 in React by LC Marshal Captain (25,790 points)
recategorized Apr 21, 2021 by LC Marshal
// going back - history page
goBack() {
  const { listingProperties, resp, forBack } = this.props;
  if (window.history.length <= 1) {
    let pType = listingProperties.PropertyListingType;
    if (pType === 'rental' || pType === 'room_rental') {
      pType = 'rent';
    } else if (pType === 'sale') {
      pType = 'buy';
    }
    let targetPath;
    if (resp.ref === undefined || (resp.ref.indexOf('/listing/') !== -1) ) {
      const titleBackSearch = forBack.title.replace(/[^\x00-\x7F]/g, "");
      let params = `keyword=${titleBackSearch}`;
      if (Object.entries(forBack.asset).length !== 0) {
        const ast = forBack.asset['und'][0]['target_id'];
        params += `&asset_id=${ast}`;
      }
      targetPath = `/${pType}?${params}`;
    } else {
      targetPath = resp.ref;
    }
    let search = '';
    if (targetPath.indexOf('?')) {
      const t = targetPath.split('?');
      targetPath = t[0];
      search = t[1];
    }
    search = (search === undefined)?'':search;
    Router.push({
      pathname: targetPath,
      search: `?${search}`,
    });
  } else {
    Router.back();
  }
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...