Skip to content
Snippets Groups Projects
presentationReducer.ts 2.27 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { AnyAction } from 'redux'
    import Types from '../actions/types'
    
    Björn Modée's avatar
    Björn Modée committed
    import { Slide } from '../interfaces/ApiModels'
    
    robban64's avatar
    robban64 committed
    import { Timer } from '../interfaces/Timer'
    
    import { RichCompetition } from './../interfaces/ApiRichModels'
    
    
    /** Define a type for the presentation state */
    
    interface PresentationState {
      competition: RichCompetition
      slide: Slide
    
    Victor Löfgren's avatar
    Victor Löfgren committed
      code: string
      timer: Timer
    
    /** Define the initial values for the presentation state */
    
    const initialState: PresentationState = {
      competition: {
        name: '',
    
        background_image: undefined,
    
        id: -1,
    
        background_image: undefined,
    
    Victor Löfgren's avatar
    Victor Löfgren committed
      code: '',
      timer: {
        enabled: false,
        value: 0,
      },
    
    /** Intercept actions for presentation state and update the state */
    
    export default function (state = initialState, action: AnyAction) {
      switch (action.type) {
        case Types.SET_PRESENTATION_COMPETITION:
          return {
            ...state,
            competition: action.payload as RichCompetition,
          }
    
    Victor Löfgren's avatar
    Victor Löfgren committed
        case Types.SET_PRESENTATION_CODE:
          return {
            ...state,
            code: action.payload,
          }
    
        case Types.SET_PRESENTATION_SLIDE:
          return {
            ...state,
            slide: action.payload as Slide,
          }
        case Types.SET_PRESENTATION_SLIDE_PREVIOUS:
          if (state.slide.order - 1 >= 0) {
            return {
              ...state,
              slide: state.competition.slides[state.slide.order - 1],
            }
          }
          return state
        case Types.SET_PRESENTATION_SLIDE_NEXT:
          if (state.slide.order + 1 < state.competition.slides.length) {
            return {
              ...state,
              slide: state.competition.slides[state.slide.order + 1],
            }
          }
          return state
    
    Victor Löfgren's avatar
    Victor Löfgren committed
        case Types.SET_PRESENTATION_SLIDE_BY_ORDER:
          if (0 <= action.payload && action.payload < state.competition.slides.length)
            return {
              ...state,
              slide: state.competition.slides[action.payload],
            }
          return state
        case Types.SET_PRESENTATION_TIMER:
          if (action.payload.value == 0) {
            action.payload.enabled = false
          }
          return {
            ...state,
            timer: action.payload,
          }