Skip to content

Instantly share code, notes, and snippets.

@jinwook-k
Created September 4, 2020 23:02
Show Gist options
  • Save jinwook-k/65eedb4abe82eeb8bbfb09282d776cb3 to your computer and use it in GitHub Desktop.
Save jinwook-k/65eedb4abe82eeb8bbfb09282d776cb3 to your computer and use it in GitHub Desktop.
// Get initial state from the previously saved data in local storage
let getHistoryFromLocal = () => {
let value = localStorage.getItem('WeatherHistory')
return JSON.parse(value) || [];
}
// Maintain a history list of queried weather data of 10
let getUpdatedHistory = (history, value) => {
let updateList = [...history];
if (updateList.length >= 10) {
updateList.shift();
}
updateList.push(value);
return updateList;
}
const history = (state = getHistoryFromLocal(), action) => {
switch (action.type) {
case "UPDATE_HISTORY":
return getUpdatedHistory(state, action.payload);
default:
return state;
}
};
export default history;
import { combineReducers } from 'redux'
import weather from './weather';
import zipCode from './zipCode';
import temperature from "./temperature";
import history from "./history";
export default combineReducers({
zipCode,
weather,
temperature,
history
})
// Default temperature to use Fahrenheit (imperial, °F)
const temperature = (state = "imperial", action) => {
switch (action.type) {
case "SAVE_TEMPERATURE":
return action.payload;
default:
return state;
}
};
export default temperature;
// Preloads saved weather data from local storage, if available
let getWeatherFromLocal = () => {
let value = localStorage.getItem('CurrentWeatherData');
return JSON.parse(value) || "";
}
const weather = (state = getWeatherFromLocal(), action) => {
switch (action.type) {
case "SAVE_WEATHER_DATA":
return action.payload;
default:
return state;
}
};
export default weather;
// Default zipcode will be 10001
const zipCode = (state = 10001, action) => {
switch (action.type) {
case "SAVE_ZIP":
return action.payload
default:
return state;
}
};
export default zipCode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment