cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              /* Variables */
$gray: #989898;
$blue: #7ED2E9;

body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}
/* Grid Items */
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto auto auto auto auto;
  grid-gap: 5px;
  width: 80%;
  padding: 0px 10%;
  & div {
    background-color: rgba(255, 255, 255, 0.5);
    text-align: center;
    font-size: 30px;
    @media only screen and (max-width: 600px) {
     padding: 0 25%;
     }
  }
  &  span::after {
    content: "\00b0";
  }
  &:nth-child(1), .app-degrees {
    border-top: 1px solid $gray;
  }
  @media only screen and (max-width: 900px) {
    grid-template-columns: auto auto auto auto;
    }
    @media only screen and (max-width: 600px) {
      grid-template-columns: auto;
      grid-row-gap: 35px;
     }
}
/* Parent Div */
.app {
  text-align: center;
 
&-logo {
  animation: logo-fade 2s linear;
 width:125px;
  }
&-header {
  height: 30%;
  padding: 20px;
  color: #FFF;
  font-family: sans-serif;
  & h2 {
    color: $blue;
    line-height: .3;
    font-family: 'Tenali Ramakrishna', sans-serif;
    font-size:3em;
  }
  & p {
   font-size: 1.5em
  } 
}
&-header, &-sms {
  background-color: #222;
}
&-title {
  font-size: 2em;
  font-weight: 100;
  line-height: 0;
  color: #fff;
  padding-bottom: 65px;
  margin: 1px;
}
&-intro {
  line-height: 0;
}
&-not {
 line-height: 0;
 color: darkgray;
 display: inline-block;
}
&-weather {
 height: 35%;
}
&-temp {
  font-size: 4em;
  color: $blue;
  padding: 15px;
  margin: 0;
  &>span {
    color: $gray;
    font-weight: 400;
    text-transform: lowercase;
    padding-bottom: .5em;
    display: inline-block;
  }
  &:before {
    content: "\B0";
    position: relative;
    left: 100px;
  }
}
  
&-time, &-degrees {
   margin:-5px;
   padding:10px 0px
}
&-time {
  font-size: 0.7em;
  border-top: 1px solid $gray;
}
&-degrees {
  color: $gray;
  font-size: 0.7em;
}
&-icons {
  width: 30px;
  mix-blend-mode: color-dodge;
}
&-btn {
  padding: 6px 50px;
  border: transparent;
  border-radius: 2px;
 }
&-sms {
 height: 200px;
 & button {
    color: #fff;
    background: #7ED2E9;
    padding: 10px 64px;
    border: transparent;
    border-radius: 1px;
    margin: -7px;
    display: inline-block;
    text-decoration: none;
    font-size: 1em;
 }
 & input {
  min-width: 125px;
  padding: 7px 27px;
  margin-bottom: 14px;
 }
 & p {
  color: #fff;
  padding: 1.5em 0;
  font-weight: 200;
  font-size: 1.2em;
  margin: 2.25em 0 0 0;
  }
 }
}

@keyframes logo-fade {
 from {
    transform: translateX(50px);
    opacity: 0;
  }
  to {
    transform: translateX(0px);
    opacity: 1;
  }
}
            
          
!
            
              class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { location: '', degree: '', forecast: [], main: '' }
  }

  temperatureConverter = (degree) => {
    return Math.round(degree * 9 / 5 + 32)
  }

  componentDidMount() {
    this.getWeather();
  }
// Make sure we have access to users location
  getWeather = () => {
    if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition(this.successWeather, this.errorWeather);
    }
  }
// Get location coordinates
  successWeather = (position) => {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    const url = `https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=c00cca645196f43959e6a78d0ae0bdaa&units=metric`;

    axios.get(url).then((res) => {
      const data = res.data;
      const temp = this.temperatureConverter(data.main.temp);
      let imgSource = `http://openweathermap.org/img/w/${ data.weather[0].icon }.png`;
   
      this.setState({ location: `${data.name}, ${data.sys.country}`, degree: temp, main: data.weather[0].main, img: imgSource })
    })

    const urlDuplicate = `https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/forecast?appid=c00cca645196f43959e6a78d0ae0bdaa&lat=${latitude}&lon=${longitude}&units=metric&cnt=8`
    let weatherList = [];

    axios.get(urlDuplicate).then(( res ) => {
      res.data.list.forEach((obj) => {
        let utc = obj.dt;
        let d = new Date(0);
        d.setUTCSeconds(utc);
        let hour = d.getHours();
        if(hour < 13 && hour >= 0) {
          hour = `${hour} AM`
        } else {
          hour = `${hour - 12} PM`
        }
        const temp = this.temperatureConverter(obj.main.temp);
        const description = obj.weather[0].main;         
        const prefix = 'http://res.cloudinary.com/marvel451/image/upload/';
        let weatherIcons =
          [
            'v1525070596/sunny.svg',
            'v1525070599/cloud-one_iiddgh.svg',
            'v1525070596/cloud-three_isrrou.svg',
            'v1525070596/thunder_hw7uyx.svg',
            'v1525140183/snowy.svg',
            'v1525070596/slightly-cloudy-day_ljjffu.svg',
            'v1525070596/slightly-cloudy-night_xchkwe.svg'
          ];

        let img = null;
        
        if (description == "clear sky" ) {
          img = `${ prefix + weatherIcons[0] }`
        }   
        if (description == "rain" ) {
          img = `${ prefix + weatherIcons[1] }`
        }
        if (description == "shower rain" ) {
          img = `${ prefix + weatherIcons[2] }`
        }
        if (description == "thunderstorm" ) {
          img = `${ prefix + weatherIcons[3] }`
        }
        if (description == "snow" ) {
          img = `${ prefix + weatherIcons[4] }`
        }
        if (description == "clouds" || "scattered clouds" || "few clouds" ) {
          img = `${ prefix + weatherIcons[5] }`
        }
        if (hour == "8 PM" || hour == "11 PM" || hour ==  "2 AM" || hour ==  "5 AM"  ) {
          img = `${ prefix + weatherIcons[6] }`
        }

        weatherList.push({hour, temp, description, img});
       })
      this.setState({ forecast: weatherList });
    })
  }

   registerUser = () => {
      let x = document.forms["updates"]["sms"].value;
     // make sure input is not blank
      if (x == "" ) {
          alert("Please give us your phone number");
          return;
      } 
     //validate only 10 digits
      if (x.match(/\d/g).length === 10 ) {
         alert("You have been registered!");
     }
     else {
         alert("Please double check your numbers");
        return;
     }
   }
   reloadApp = () => {
      this.setState({ state: this.state });
   }

  errorWeather = (er) => {
    alert("We could not determine your location");
          console.log(er);
  }

  render() {
    return (
      <div className="app">
        <header className="app-header">
          <img src='http://res.cloudinary.com/marvel451/image/upload/v1525133241/sun-cloud_s558lu.png'  className="app-logo" alt="logo" />
          <h1 className="app-title">weathur.ly</h1>
          <p className="app-intro"> We found you in </p>
          <h2>{this.state.location} </h2>
          <a href="#" className="app-not" onClick={this.reloadApp}>Not here?</a>
        </header>
 
        <section className="app-weather">
          <h2 className="app-temp">{ this.state.degree }  <br/>
            <span className="app-condition">{ this.state.main } </span>
          </h2>
         
          <div className="grid-container">
            { 
              this.state.forecast.map(( obj) => {            
                return <div key={ obj.id }>
                          <p className="app-time"> { obj.hour } </p>
                          <p className="app-degrees"> { obj.temp } F  </p>
                          <img className="app-icons"  src={ obj.img }   />
                      </div>
              }) 
            }
          </div>
        </section>

        <section className="app-sms">
          <p> Register for SMS alerts for this location</p>
          <form className="form-inline" onSubmit={(e) => {this.registerUser(); e.preventDefault();}}  name="updates">
            <input  type="text" name="sms" placeholder="555-555-555" /><br />
            <button type="submit" className="btn btn-primary">Get alerts </button>
          </form>  
        </section>
      </div> // app

    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console