import { types } from '@redux//actions'

const initialState = {
  loggedIn: false,
  loading: false,
  error: false,
  forms: [],
  questions: [],
  isCompanyChosen: false,
  selectedCompany: { name: '', scriptId: '' },
  isClientLoaded: false,
  errorMessage: '',
  numberOfWahou: 0,
  numberOfOK: 0,
  numberOfKO: 0
}

export default function reducer (state = initialState, action = {}) {
  switch (action.type) {
    case types.GOOGLE_CLIENT_INIT.REQUEST:
      return {
        ...state,
        loading: true
      }
    case types.GOOGLE_CLIENT_INIT.SUCCESS:
      return {
        ...state,
        loading: false,
        loggedIn: action.isLoggedIn,
        isClientLoaded: true,
        error: false
      }
    case types.LOGIN.REQUEST:
      return {
        ...state,
        loading: true
      }
    case types.LOGIN.SUCCESS:
      return {
        ...state,
        loading: false,
        loggedIn: true,
        error: false
      }
    case types.LOGOUT.REQUEST:
      return {
        ...state,
        loading: true
      }
    case types.LOGOUT.SUCCESS:
      return initialState
    case types.FETCH_FORMS.REQUEST:
      return {
        ...state,
        loading: true
      }
    case types.FETCH_FORMS.SUCCESS:
      const forms = action.payload.responses
      let numberOfWahou = 0
      let numberOfOK = 0
      let numberOfKO = 0
      forms.forEach(form => {
        if (form['satisfaction'] === 'Waouh') {
          numberOfWahou += 1
        }
        if (form['satisfaction'] === 'OK') {
          numberOfOK += 1
        }
        if (form['satisfaction'] === 'KO') {
          numberOfKO += 1
        }
      })
      return {
        ...state,
        loading: false,
        forms,
        questions: action.payload.questions,
        error: false,
        numberOfKO,
        numberOfOK,
        numberOfWahou
      }
    case types.LOGIN.FAILURE:
    case types.GOOGLE_CLIENT_INIT.FAILURE:
    case types.LOGOUT.FAILURE:
    case types.FETCH_FORMS.FAILURE:
      return {
        ...state,
        loading: false,
        error: true,
        errorMessage: action.error
      }
    case types.COMPANY_SELECTED:
      return {
        ...state,
        isCompanyChosen: true,
        selectedCompany: action.company
      }
    default:
      return state
  }
}

@@ -405,24 +405,54 @@
 ion.type) %7B%0A
+    case types.LOGIN.REQUEST:%0A
     case typ
@@ -474,32 +474,99 @@
 T_INIT.REQUEST:%0A
+    case types.LOGOUT.REQUEST:%0A    case types.FETCH_FORMS.REQUEST:%0A
       return %7B%0A 
@@ -631,180 +631,245 @@
 pes.
-GOOGLE_CLIENT_INIT.SUCCESS:%0A      return %7B%0A        ...state,%0A        loading: false,%0A        loggedIn: action.isLoggedIn,%0A        isClientLoaded: true,%0A        error: false
+LOGIN.FAILURE:%0A    case types.GOOGLE_CLIENT_INIT.FAILURE:%0A    case types.LOGOUT.FAILURE:%0A    case types.FETCH_FORMS.FAILURE:%0A      return %7B%0A        ...state,%0A        loading: false,%0A        error: true,%0A        errorMessage: action.error
 %0A   
@@ -890,31 +890,31 @@
 types.LOGIN.
-REQUEST
+SUCCESS
 :%0A      retu
@@ -957,410 +957,318 @@
 ng: 
-true%0A      %7D%0A    case types.LOGIN.SUCCESS:%0A      return %7B%0A        ...state,%0A        loading: false,%0A        loggedIn: true,%0A        error: false%0A      %7D%0A    case types.LOGOUT.REQUEST:%0A      return %7B%0A        ...state,%0A        loading: true%0A      %7D%0A    case types.LOGOUT.SUCCESS:%0A      return initialState%0A    case types.FETCH_FORMS.REQUEST:%0A      return %7B%0A        ...state,%0A        loading: true%0A      %7D
+false,%0A        loggedIn: true,%0A        error: false%0A    %7D%0A    case types.GOOGLE_CLIENT_INIT.SUCCESS:%0A      return %7B%0A        ...state,%0A        loading: false,%0A        loggedIn: action.isLoggedIn,%0A        isClientLoaded: true,%0A        error: false%0A      %7D%0A    case types.LOGOUT.SUCCESS:%0A      return initialState
 %0A   
@@ -1924,269 +1924,8 @@
   %7D%0A
-    case types.LOGIN.FAILURE:%0A    case types.GOOGLE_CLIENT_INIT.FAILURE:%0A    case types.LOGOUT.FAILURE:%0A    case types.FETCH_FORMS.FAILURE:%0A      return %7B%0A        ...state,%0A        loading: false,%0A        error: true,%0A        errorMessage: action.error%0A      %7D%0A
     

Simplify the reducer