import { Template } from '../store/types'
import * as types from '../constants/template'

export interface TemplateState {
  templates: Template[]
  selectedTemplate: Template | null
}

const initialState: TemplateState = {
  templates: [],
  selectedTemplate: null
}

export default function templateReducer(state = initialState, action: any): TemplateState {
  switch (action.type) {
    case types.INIT_TEMPLATES:
      return {
        ...state,
        templates: action.payload
      }

    case types.SET_SELECTED_TEMPLATE:
      return {
        ...state,
        selectedTemplate: action.payload
      }

    default:
      return state
  }
}
