about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/reducers/modal.js
blob: 2ef0aef24ffc00d1b5938485204e80cf2341f6fa (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
33
34
35
36
37
38
39
import { MODAL_OPEN, MODAL_CLOSE } from 'flavours/glitch/actions/modal';
import { TIMELINE_DELETE } from 'flavours/glitch/actions/timelines';
import { COMPOSE_UPLOAD_CHANGE_SUCCESS } from 'flavours/glitch/actions/compose';
import { Stack as ImmutableStack, Map as ImmutableMap } from 'immutable';

const initialState = ImmutableMap({
  ignoreFocus: false,
  stack: ImmutableStack(),
});

const popModal = (state, { modalType, ignoreFocus }) => {
  if (modalType === undefined || modalType === state.getIn(['stack', 0, 'modalType'])) {
    return state.set('ignoreFocus', !!ignoreFocus).update('stack', stack => stack.shift());
  } else {
    return state;
  }
};

const pushModal = (state, modalType, modalProps) => {
  return state.withMutations(map => {
    map.set('ignoreFocus', false);
    map.update('stack', stack => stack.unshift(ImmutableMap({ modalType, modalProps })));
  });
};

export default function modal(state = initialState, action) {
  switch(action.type) {
  case MODAL_OPEN:
    return pushModal(state, action.modalType, action.modalProps);
  case MODAL_CLOSE:
    return popModal(state, action);
  case COMPOSE_UPLOAD_CHANGE_SUCCESS:
    return popModal(state, { modalType: 'FOCAL_POINT', ignoreFocus: false });
  case TIMELINE_DELETE:
    return state.update('stack', stack => stack.filterNot((modal) => modal.get('modalProps').statusId === action.id));
  default:
    return state;
  }
};