2017-07-13 21:15:32 +01:00
|
|
|
import axios from 'axios';
|
2017-12-09 13:18:45 +00:00
|
|
|
import { pushNotificationsSetting } from '../settings';
|
2017-07-13 21:15:32 +01:00
|
|
|
|
|
|
|
export const SET_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT';
|
|
|
|
export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION';
|
|
|
|
export const CLEAR_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_CLEAR_SUBSCRIPTION';
|
|
|
|
export const ALERTS_CHANGE = 'PUSH_NOTIFICATIONS_ALERTS_CHANGE';
|
|
|
|
|
|
|
|
export function setBrowserSupport (value) {
|
|
|
|
return {
|
|
|
|
type: SET_BROWSER_SUPPORT,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setSubscription (subscription) {
|
|
|
|
return {
|
|
|
|
type: SET_SUBSCRIPTION,
|
|
|
|
subscription,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearSubscription () {
|
|
|
|
return {
|
|
|
|
type: CLEAR_SUBSCRIPTION,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changeAlerts(key, value) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: ALERTS_CHANGE,
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(saveSettings());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveSettings() {
|
|
|
|
return (_, getState) => {
|
|
|
|
const state = getState().get('push_notifications');
|
|
|
|
const subscription = state.get('subscription');
|
|
|
|
const alerts = state.get('alerts');
|
2017-12-09 01:31:37 +00:00
|
|
|
const data = { alerts };
|
2017-07-13 21:15:32 +01:00
|
|
|
|
|
|
|
axios.put(`/api/web/push_subscriptions/${subscription.get('id')}`, {
|
2017-12-09 01:31:37 +00:00
|
|
|
data,
|
|
|
|
}).then(() => {
|
|
|
|
const me = getState().getIn(['meta', 'me']);
|
|
|
|
if (me) {
|
2017-12-09 13:18:45 +00:00
|
|
|
pushNotificationsSetting.set(me, data);
|
2017-12-09 01:31:37 +00:00
|
|
|
}
|
2017-07-13 21:15:32 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|