Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.save to localstorage #9

Open
wants to merge 3 commits into
base: 7.add-components-and=stylings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion client/package.json
Expand Up @@ -37,5 +37,6 @@
"last 1 firefox version",
"last 1 safari version"
]
}
},
"proxy": "http://localhost:5000/"
}
41 changes: 40 additions & 1 deletion client/src/Components/WeatherForm.jsx
@@ -1,20 +1,59 @@
import React, {Component} from "react";
import {Form, Button, Row, Col, ButtonGroup, ToggleButton} from "react-bootstrap";

import axios from 'axios';

class WeatherForm extends Component {
// default state values
state = {
tempMetric: "imperial",
zipCodeInput: "98052"
}

componentDidMount() {
this.refreshSavedWeather();
}

// Refreshes the current weather data for the most recent zip code, if it exists
refreshSavedWeather = () => {
if (localStorage.getItem("zipCode")) {
axios.post("/api/weather", {
zipCode: localStorage.getItem("zipCode"),
tempMetric: localStorage.getItem("tempMetric")
}).then(d => {
localStorage.setItem("CurrentWeatherData", JSON.stringify(d.data));
});
}
}

onChange = (e) => {
this.setState({[e.target.name]: e.target.value});
}

saveFormData = (event) => {
event.preventDefault();

// Gets the weather data from the weather api and returns it to save into local storage and redux store.
axios.post("/api/weather", {
zipCode: this.state.zipCodeInput,
tempMetric: this.state.tempMetric
}).then(response => {
let weatherData = response.data;

this.saveToLocalStorage(weatherData);
});
}

// Save data from form to local storage
saveToLocalStorage = (weatherData) => {
localStorage.setItem("zipCode", this.state.zipCodeInput);
localStorage.setItem("tempMetric", this.state.tempMetric);
localStorage.setItem("CurrentWeatherData", JSON.stringify(weatherData));
}

render() {
return (
<Form className="weather-form" >
<Form className="weather-form" onSubmit={this.saveFormData}>

<Row type="flex" justify="center" align="center" className="zipCode">
<Col>
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,10 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server/server.js"
"start": "node server/server.js",
"server": "nodemon server/server.js",
"client": "cd client && npm run start",
"dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\""
},
"repository": {
"type": "git",
Expand Down