/**
 * React Static Boilerplate
 * https://github.com/koistya/react-static-boilerplate
 * Copyright (c) Konstantin Tarkus (@koistya) | MIT license
 */

import React, { Component } from 'react';
import Images from './../components/Images';
import Spinner from './../components/Spinner';

export default class extends Component {

  constructor(props) {
    super(props);

    this.cx = '014343924111201567214:mctjyj4egne';
    this.apiKey = 'AIzaSyDp08xnKZo4xuPgajY4ZHPi--_hbmx1_Rk';
    this.endpoint = 'https://www.googleapis.com/customsearch/v1';

    this.state = {
      data: null
    }
  }

  isBirthday() {
    var today = new Date();
    var birthday = new Date(today.getFullYear(), 0, 22);
    return birthday.setHours(0,0,0,0) === today.setHours(0,0,0,0);
  }

  componentDidMount() {
    const api = this.endpoint + '?key=' + this.apiKey + '&cx=' + this.cx;
    let keyword = this.isBirthday() ? 'birthday' : 'happy';
    let query = encodeURIComponent(keyword + ' cat'.replace(/ /g, '+'));
    let url =  api + '&imgSize=large&q=' + query;

    this.fetchData(url);
  }

  fetchData(url) {

    (async () => {
      try {
        var response = await fetch(url);
        var data = await response.json();

        this.setData(data);
      } catch (e) {
        console.log("Error: ", e);
      }
    })();
  }

  setData(data) {
    this.setState({data});
  }

  render() {
    if (!this.state.data) {
      return <Spinner/>;
    }

    let data = this.state.data;
    let images = Object.keys(data.items).map(item => {
      let i = data.items[item];
      if (i.pagemap) {
        return i.pagemap.cse_image[0].src;
      }
    });

    let message = isBirthday()
      ? <h2>HAPPY BIRTHDAY BABES!!!</h2>
      : <h2>Noooooo it's you!!!</h2>;

    return (
      <div>
        {message}
        <Images list={images}></Images>
      </div>
    );
  }

}

@@ -368,120 +368,8 @@
 );%0A%0A
-    this.cx = '014343924111201567214:mctjyj4egne';%0A    this.apiKey = 'AIzaSyDp08xnKZo4xuPgajY4ZHPi--_hbmx1_Rk';%0A
     
@@ -397,42 +397,59 @@
 s://
-www.googleapis.com/customsearch/v1
+raw.githubusercontent.com/rpowis/pusscat.lol/master
 ';%0A%0A
@@ -701,259 +701,122 @@
     
-const api = this.endpoint + '?key=' + this.apiKey + '&cx=' + this.cx;%0A    let keyword = this.isBirthday() ? 'birthday' : 'happy';%0A    let query = encodeURIComponent(keyword + ' cat'.replace(/ /g, '+'));%0A    let url =  api + '&imgSize=large&q=' + query
+let keyword = this.isBirthday() ? 'birthday' : 'happy';%0A    let url = this.endpoint + '/data/' + keyword + '.json'
 ;%0A%0A 
@@ -863,17 +863,16 @@
 (url) %7B%0A
-%0A
     (asy
@@ -1219,86 +1219,153 @@
 let 
-data = this.state.data;%0A    let images = Object.keys(
+images = Object.keys(this.state.data).map(item =%3E %7B%0A      let field = this.state.
 data
-.
+%5B
 item
-s).map(item =%3E %7B
+%5D.rgl_link;%0A      let regex = /imgurl=(%5CS+)&imgrefurl/;
 %0A   
@@ -1376,27 +1376,31 @@
 et i
+mg
  = 
-data.items%5Bitem%5D
+field.match(regex)
 ;%0A  
@@ -1408,24 +1408,18 @@
    if (i
-.pagemap
+mg
 ) %7B%0A    
@@ -1434,33 +1434,13 @@
 rn i
-.pagemap.cse_image%5B0%5D.src
+mg%5B1%5D
 ;%0A  

Update endpoint to load json file