Created
September 4, 2020 23:02
-
-
Save jinwook-k/65eedb4abe82eeb8bbfb09282d776cb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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