github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-magicmirror/web/src/ducks/forecast.js (about)

     1  import yrno from 'yr.no-forecast'
     2  import moment from 'moment'
     3  import { List, Map } from 'immutable'
     4  
     5  export const LOADING = 'forecast/LOADING'
     6  export const SUCCESS = 'forecast/SUCCESS'
     7  
     8  const initialState = Map({
     9    forecast: null,
    10    current: null,
    11    loading: false
    12  })
    13  
    14  export const loadForecast = () => {
    15    return dispatch => {
    16      dispatch({
    17        type: LOADING
    18      })
    19  
    20      const LOCATION = {
    21        lat: 56.870349,
    22        lon: 14.541664
    23      }
    24  
    25      return yrno()
    26        .getWeather(LOCATION)
    27        .then(weather => {
    28          Promise.all([
    29            weather.getFiveDaySummary(),
    30            weather.getForecastForTime(moment().startOf('day'))
    31          ]).then(result => {
    32            dispatch({
    33              type: SUCCESS,
    34              forecast: result[0],
    35              current: result[1]
    36            })
    37          })
    38        })
    39    }
    40  }
    41  
    42  export default (state = initialState, action) => {
    43    switch (action.type) {
    44      case LOADING:
    45        return state.set('loading', true)
    46      case SUCCESS:
    47        return state
    48          .set('forecast', action.forecast)
    49          .set('current', action.current)
    50      default:
    51        return state
    52    }
    53  }