~cytrogen/masto-fe

ref: 20a89f1c8eefa0f766a4650be4d2e5f1fa92ee71 masto-fe/app/javascript/flavours/glitch/reducers/push_notifications.js -rw-r--r-- 1.6 KiB
20a89f1c — Cytrogen [feature] Bookmark folders UI 9 days ago
                                                                                
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Immutable from "immutable";

import { SET_BROWSER_SUPPORT, SET_SUBSCRIPTION, CLEAR_SUBSCRIPTION, SET_ALERTS } from "flavours/glitch/actions/push_notifications";
import { STORE_HYDRATE } from "flavours/glitch/actions/store";

const initialState = Immutable.Map({
  subscription: null,
  alerts: new Immutable.Map({
    follow: false,
    follow_request: false,
    favourite: false,
    reblog: false,
    mention: false,
    poll: false,
  }),
  isSubscribed: false,
  browserSupport: false,
});

export default function push_subscriptions(state = initialState, action) {
  switch(action.type) {
    case STORE_HYDRATE: {
      const push_subscription = action.state.get("push_subscription");

      if (push_subscription) {
        return state
          .set("subscription", new Immutable.Map({
            id: push_subscription.get("id"),
            endpoint: push_subscription.get("endpoint"),
          }))
          .set("alerts", push_subscription.get("alerts") || initialState.get("alerts"))
          .set("isSubscribed", true);
      }

      return state;
    }
    case SET_SUBSCRIPTION:
      return state
        .set("subscription", new Immutable.Map({
          id: action.subscription.id,
          endpoint: action.subscription.endpoint,
        }))
        .set("alerts", new Immutable.Map(action.subscription.alerts))
        .set("isSubscribed", true);
    case SET_BROWSER_SUPPORT:
      return state.set("browserSupport", action.value);
    case CLEAR_SUBSCRIPTION:
      return initialState;
    case SET_ALERTS:
      return state.setIn(action.path, action.value);
    default:
      return state;
  }
}