import dotProp from 'dot-prop-immutable'
import * as types from './weatherActionTypes'

const initialState = {
  weather: {
    data: {},
    app: {
      location: 5128581 // nyc usa
    },
    ui: {}
  }
}

const updateAppIsFetching = (state, action, value) => {
  return dotProp.set(state, 'weather.app.isFetching', value)
}

const updateData = (state, action) => {
  // take data from api
  let weather = {}
  weather.name = action.payload.name
  weather.country = action.payload.sys.country

  weather.temperature = action.payload.main.temp
  weather.temperatureMin = action.payload.main.temp_min
  weather.temperatureMax = action.payload.main.temp_max

  weather.weatherMain = action.payload.weather[0].main
  weather.weatherDescription = action.payload.weather[0].description
  weather.weatherIcon = action.payload.weather[0].icon

  weather.updatedTime = new Date().toString()

  weather.windDegree = action.payload.wind.deg
  weather.windSpeed = action.payload.wind.speed

  weather.visibility = action.payload.visibility

/*
weather: [
{
id: 802,
main: "Clouds",
description: "scattered clouds",
icon: "03n"
}
],

*/

  return dotProp.set(state, 'weather.data', weather)
}

function weatherReducer (state = initialState, action) {
  switch (action.type) {
    case types.GET_WEATHER_PENDING:
      return updateAppIsFetching(state, action, true)
    case types.GET_WEATHER_FULFILLED:
      updateAppIsFetching(state, action, false)
      return updateData(state, action)
    default:
      return state
  }
}

export default weatherReducer

@@ -368,34 +368,41 @@
 %7B%0A  
-// take data from api%0A  le
+const %7B payload %7D = action%0A  cons
 t we
@@ -414,34 +414,19 @@
  = %7B
-%7D
 %0A  
-weather.name = action.
+  name: 
 payl
@@ -437,36 +437,23 @@
 name
+,
 %0A  
-weather.country = action.
+  country: 
 payl
@@ -467,28 +467,22 @@
 .country
-%0A%0A  weather.
+,%0A    
 temperat
@@ -484,26 +484,18 @@
 perature
+:
  
-= action.
 payload.
@@ -503,27 +503,22 @@
 ain.temp
+,
 %0A  
-weather.
+  
 temperat
@@ -523,26 +523,18 @@
 atureMin
+:
  
-= action.
 payload.
@@ -546,27 +546,22 @@
 temp_min
+,
 %0A  
-weather.
+  
 temperat
@@ -566,26 +566,18 @@
 atureMax
+:
  
-= action.
 payload.
@@ -589,28 +589,22 @@
 temp_max
-%0A%0A  weather.
+,%0A    
 weatherM
@@ -606,26 +606,18 @@
 therMain
+:
  
-= action.
 payload.
@@ -631,27 +631,22 @@
 %5B0%5D.main
-%0A  weather.
+,%0A    
 weatherD
@@ -655,26 +655,18 @@
 cription
+:
  
-= action.
 payload.
@@ -687,27 +687,22 @@
 cription
+,
 %0A  
-weather.
+  
 weatherI
@@ -704,26 +704,18 @@
 therIcon
+:
  
-= action.
 payload.
@@ -729,28 +729,22 @@
 %5B0%5D.icon
-%0A%0A  weather.
+,%0A    
 updatedT
@@ -746,18 +746,17 @@
 atedTime
- =
+:
  new Dat
@@ -769,28 +769,22 @@
 String()
-%0A%0A  weather.
+,%0A    
 windDegr
@@ -785,26 +785,18 @@
 ndDegree
+:
  
-= action.
 payload.
@@ -803,27 +803,22 @@
 wind.deg
+,
 %0A  
-weather.
+  
 windSpee
@@ -818,26 +818,18 @@
 indSpeed
+:
  
-= action.
 payload.
@@ -842,20 +842,14 @@
 peed
-%0A%0A  weather.
+,%0A    
 visi
@@ -858,18 +858,10 @@
 lity
+:
  
-= action.
 payl
@@ -879,104 +879,11 @@
 ity%0A
-%0A/*%0Aweather: %5B%0A%7B%0Aid: 802,%0Amain: %22Clouds%22,%0Adescription: %22scattered clouds%22,%0Aicon: %2203n%22%0A%7D%0A%5D,%0A%0A*/%0A
+  %7D
 %0A  r

Add better object decostructing