import { AppActionTypes } from './actions';

const initialAppsState: any = {
  apps: [],
  app: {},
  appModalLoading: false,
  appsLoading: false,
};

const appsReducer = (state: any = initialAppsState, action: any) => {
  switch (action.type) {
    case AppActionTypes.FETCH_APPS:
      return {
        ...state,
        apps: action.payload,
      };
    case AppActionTypes.SET_APP_MODAL_LOADING:
      return {
        ...state,
        appModalLoading: action.payload,
      };
    case AppActionTypes.SET_APPS_LOADING:
      return {
        ...state,
        appsLoading: action.payload,
      };
    case AppActionTypes.FETCH_APP:
    case AppActionTypes.UPDATE_APP:
    case AppActionTypes.CREATE_APP:
    case AppActionTypes.CREATE_BATCH_APPS:
      return {
        ...state,
        isModalVisible: false,
        app: action.payload,
      };
    case AppActionTypes.DELETE_APP:
      return {
        ...state,
        app: {},
      };
    default:
      return state;
  }
};

export default appsReducer;
