Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                body {
  font-family: 'Open sans', sans-serif;
}

h2.app-title {
  text-align: center;
  padding: 1.6rem;
  font-size: 1.6rem;
  letter-spacing: 1px;
}
ul.offers-container {
  max-width: 480px;
  margin: 0 auto;
  text-align: center;
  background: #ccc;
  min-height: 110vh;
  padding: 0.1rem;
}
li.unit-offer {
  display: inline-block;
  padding-bottom: 0;
  width: 48%;
  margin: 0.8%;
  box-sizing: border-box;
  box-shadow: 0px 1px 3px 0px rgba(50, 50, 50, 0.15);
  background: white;
}
.offer-img__container{
  font-size: 0;
  background: white;
  padding: 1rem;
  box-sizing: border-box;
}
img.offer-img {
  width: 100%;
}
div.detectVwPort {
  height: 1px;
  width: 1px;
  background: red;
}
.offer-name {
  color: white;
  font-size: 0.875rem;
  letter-spacing: 1px;
  font-weight: 100;
  padding: 0.6rem;
  height: 2rem;
  overflow: hidden;
  white-space: nowrap;
  text-align: left;
  text-overflow: ellipsis;
  background: chocolate;
  box-sizing: border-box;
}
.offer-loader {
  background: rgba(0, 0, 0, 0.6);
  color: white;
  width: 100%;
  text-align: center;
  position: fixed;
  z-index: 1;
  bottom: 0;
  padding: 1rem;
  box-sizing: border-box;
  transition: all 0.3s ease;
  box-shadow: 0px -2px 8px 0px rgba(50, 50, 50, 0.35);
  
  visibility: hidden;
  opacity: 0;
  transform: translateY(10px);
}

.offer-loader.true, .offer-loader__loading {
  visibility: visible;
  opacity: 1;
  transform: translateY(0px);
}

h4.offer-loader__title {
  letter-spacing: 1px;
}
              
            
!

JS

              
                console.clear();

const { Component } = React;
const { render } = ReactDOM;

const url =
  "https://mobileapi.snapdeal.com/service/generic/get/getGenericOffer?landingPage=deal-of-the-day&start=1&count=10";

const setOfferImgUrl = offer => {
  if (
    offer.commonMinProductDetailsDTO &&
    offer.commonMinProductDetailsDTO.imgs[0]
  ) {
    return (
      offer.commonMinProductDetailsDTO.imgs[0] ||
      "https://n2.sdlcdn.com/imgs/a/h/u/230X258_sharpened/Solo-Report-Cover-Strip-File-SDL055430870-1-cf691.jpg"
    );
  }
};

// components
const DetectVwport = () => <div className="detectVwPort" />;
const OffersContainerUL = ({ data }) => (
  <ul className="offers-container">
    {data.map(offer => <OfferUnitLi offer={offer} key={offer.pogId} />)}
  </ul>
);
const OfferUnitLi = ({ offer, key }) => (
  <li key={key} className="unit-offer">
    <div className="offer-img__container">
      <img className="offer-img" src={setOfferImgUrl(offer)} />
    </div>
    <div className="offer-name">{offer.offerName || "Product Offer Name"}</div>
  </li>
);
const OfferLoader = ({ loading }) => (
  <div className={`offer-loader ${loading ? "offer-loader__loading" : ""}`}>
    <h4 className='offer-loader__title'>LOADING. . .</h4>
  </div>
);

// main container
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      allLoadComplete: false,
      vwportThreshold: 1.6,
      loading: false,
      currentScrollY: 0,
      detectVwportOffsetTop: 0,
      vwportHeight: 0,
      vwportWidth: 0,
      count: 18,
      countLimit: 200,
      start: 0,
      url:
        "https://mobileapi.snapdeal.com/service/generic/get/getGenericOffer?landingPage=deal-of-the-day&start=0&count=18"
    };
    this.updateNextUrl = this.updateNextUrl.bind(this);
    this.asyncMakeRequestUpdate = this.asyncMakeRequestUpdate.bind(this);
    this.updateDetectVwportOffsetTop = this.updateDetectVwportOffsetTop.bind(
      this
    );
    this.updateVwportDimensions = this.updateVwportDimensions.bind(this);
    this.updateCurrentScrollY = this.updateCurrentScrollY.bind(this);
    this.checkIfDetectVwportInVwport = this.checkIfDetectVwportInVwport.bind(
      this
    );
    this.infiniteLoadScrollEvents = this.infiniteLoadScrollEvents.bind(this);
    this.attachDetachScrollEvents = this.attachDetachScrollEvents.bind(this);
  }
  checkIfDetectVwportInVwport(callback) {
    const {
      allLoadComplete,
      currentScrollY,
      detectVwportOffsetTop,
      vwportHeight,
      vwportThreshold,
      loading
    } = this.state;

    if (allLoadComplete) {
      console.log("removed scrollListenerEvents. . .");
      // window.removeEventListener("scroll", this.infiniteLoadScrollEvents);
      return false;
    } else {
      if (
        currentScrollY >
        (detectVwportOffsetTop - (vwportHeight * vwportThreshold))
      ) {
        console.log("start infinite scrolling. ..");
        // console.log('this.state.start: ', this.state.start)
        if (!this.state.loading) {
          this.asyncMakeRequestUpdate(this.state.url);
        }
      } else {
        console.log("wait . .  .");
      }
    }
  }
  updateCurrentScrollY() {
    const currentScrollY =
      window.pageYOffset || document.documentElement.scrollTop;
    this.setState(
      {
        currentScrollY
      }
    );
  }
  infiniteLoadScrollEvents() {
    this.updateCurrentScrollY();
    this.checkIfDetectVwportInVwport();
  }
  attachDetachScrollEvents() {
    if (this.state.allLoadComplete) {
      window.removeEventListener("scroll", this.infiniteLoadScrollEvents);
    } else {
      window.addEventListener("scroll", this.infiniteLoadScrollEvents);
    }
  }
  updateDetectVwportOffsetTop() {
    const detectVwportOffsetTop = this.detectVwPort.offsetTop;
    this.setState(
      {
        detectVwportOffsetTop
      }
    );
  }
  updateVwportDimensions() {
    const vwportWidth = Math.max(
      document.documentElement.clientWidth,
      window.innerWidth || 0
    );
    const vwportHeight = Math.max(
      document.documentElement.clientHeight,
      window.innerHeight || 0
    );
    this.setState({
      vwportWidth,
      vwportHeight
    });
  }
  updateNextUrl() {
    const { start, countLimit, count } = this.state;
    if (start >= countLimit) {
      this.setState(
        {
          allLoadComplete: true
        }
      );
      return false;
    }
    this.setState(
      prevState => ({
        start: prevState.start + count
      }),
      () => {
        const newUrl = `https://mobileapi.snapdeal.com/service/generic/get/getGenericOffer?landingPage=deal-of-the-day&start=${start}&count=${count -
          1}`;
        this.setState({ url: newUrl });
      }
    );
  }
  asyncMakeRequestUpdate() {
    this.setState({ loading: true });
    const { url } = this.state;
    axios
      .get(url)
      .then(res => {
        const newData = res.data.genericOfferItems.filter(
          offer => offer.pogId !== ""
        );
        
        this.setState(
          {
            data: [...this.state.data, ...newData]
          },
          () => {
            window.addEventListener("scroll", this.attachDetachScrollEvents);
            // update offset of detect-viewport element
            this.updateDetectVwportOffsetTop();
            this.updateNextUrl();
            this.setState({ loading: false });
          }
        );
      })
      .catch(err => console.log(err));
  }
  componentDidMount() {
    this.asyncMakeRequestUpdate();

    // calculate viewport dimensions
    this.updateVwportDimensions();
    // calculate offset of detect-viewport element
    this.updateDetectVwportOffsetTop();
  }
  render() {
    // console.log(this.state);
    const { data } = this.state;
    // console.log(data)
    return (
      <main id="container">
        <h2 className='app-title'>Infinite scroll with React</h2>
        <br />
        <OffersContainerUL data={data} />
        <div
          ref={detectVwPort => {
            this.detectVwPort = detectVwPort;
          }}
          className="detectVwPort"
        />
        <OfferLoader loading={this.state.loading} />
      </main>
    );
  }
}

render(<App />, document.getElementById("root"));

              
            
!
999px

Console