about summary refs log tree commit diff
path: root/app/assets/javascripts/components/reducers/modal.jsx
blob: 07da65771d8afc49388edb47b2a04047c6675c57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {
  MEDIA_OPEN,
  MODAL_CLOSE,
  MODAL_INDEX_DECREASE,
  MODAL_INDEX_INCREASE
} from '../actions/modal';
import Immutable from 'immutable';

const initialState = Immutable.Map({
  media: null,
  index: 0,
  open: false
});

export default function modal(state = initialState, action) {
  switch(action.type) {
  case MEDIA_OPEN:
    return state.withMutations(map => {
      map.set('media', action.media);
      map.set('index', action.index);
      map.set('open', true);
    });
  case MODAL_CLOSE:
    return state.set('open', false);
  case MODAL_INDEX_DECREASE:
    return state.update('index', index => Math.max(index - 1, 0));
  case MODAL_INDEX_INCREASE:
    return state.update('index', index => Math.min(index + 1, state.get('media').size - 1));
  default:
    return state;
  }
};