se-proj/sustain/frontend/src/components/App.js
//update graph if utility or year changes
update graph if utility or year changes
update graph if utility or year changes
componentDidUpdate(p, p2) {
    if ((this.state.util != p2.util) || (this.state.year != p2.year)) {
    fetch("api/getdata/")
      .then(response => {
        if (response.status > 400) {
          return this.setState(() => {
            return { placeholder: "Something went wrong!" };
          });
        }
        return response.json();
      })
      .then(data => {
        data = data.filter(x=>{
          return(x.date.slice(0,4)==this.state.year);
        })
        data = data.map(x=>{
          if (this.state.util=="Water")
            return {
              date:x.date,
              meas:x.water
            };
          else if (this.state.util=="Electric")
            return {
              date:x.date,
              meas:x.elec
            };
          else {
            return {
              date:x.date,
              meas:x.gas
            };
          }})
        this.setState(() => {
          return {
            data:data,
            loaded: true
          };
        });
      });
    }
  }
componentDidUpdate ( p , p2 ) { if ( ( this . state . util != p2 . util ) || ( this . state . year != p2 . year ) ) { fetch ( "api/getdata/" ) . then ( response => { if ( response . status > 400 ) { return this . setState ( ( ) => { return { placeholder : "Something went wrong!" } ; } ) ; } return response . json ( ) ; } ) . then ( data => { data = data . filter ( x => { return ( x . date . slice ( 0 , 4 ) == this . state . year ) ; } ) data = data . map ( x => { if ( this . state . util == "Water" ) return { date : x . date , meas : x . water } ; else if ( this . state . util == "Electric" ) return { date : x . date , meas : x . elec } ; else { return { date : x . date , meas : x . gas } ; } } ) this . setState ( ( ) => { return { data : data , loaded : true } ; } ) ; } ) ; } }
update graph if utility or year changes
update graph if utility or year changes
//filter by desired year /*
        //sorting, not necessary rn
        data = data.sort((a,b)=>{
          return(months.indexOf(a.date)-months.indexOf(b.date));
        })
        */