bigbuffet-rw/app/soapbox/reducers/push_notifications.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Map as ImmutableMap, Record as ImmutableRecord } from 'immutable';
2022-01-10 14:01:24 -08:00
import { SET_BROWSER_SUPPORT, SET_SUBSCRIPTION, CLEAR_SUBSCRIPTION, SET_ALERTS } from '../actions/push_notifications';
2020-03-27 13:59:38 -07:00
import type { AnyAction } from 'redux';
const SubscriptionRecord = ImmutableRecord({
id: '',
endpoint: '',
});
const ReducerRecord = ImmutableRecord({
subscription: null as Subscription | null,
alerts: ImmutableMap<string, boolean>({
2021-11-18 13:11:50 -08:00
follow: true,
follow_request: true,
favourite: true,
reblog: true,
mention: true,
poll: true,
2020-03-27 13:59:38 -07:00
}),
isSubscribed: false,
browserSupport: false,
});
type Subscription = ReturnType<typeof SubscriptionRecord>;
export default function push_subscriptions(state = ReducerRecord(), action: AnyAction) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case SET_SUBSCRIPTION:
return state
.set('subscription', SubscriptionRecord({
2022-05-11 14:06:35 -07:00
id: action.subscription.id,
endpoint: action.subscription.endpoint,
}))
.set('alerts', ImmutableMap(action.subscription.alerts))
2022-05-11 14:06:35 -07:00
.set('isSubscribed', true);
case SET_BROWSER_SUPPORT:
return state.set('browserSupport', action.value);
case CLEAR_SUBSCRIPTION:
return ReducerRecord();
2022-05-11 14:06:35 -07:00
case SET_ALERTS:
return state.setIn(action.path, action.value);
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}