2017-01-02 07:09:57 -06:00
import { connect } from 'react-redux' ;
2017-06-12 05:26:23 -05:00
import { defineMessages , injectIntl } from 'react-intl' ;
2017-01-02 07:09:57 -06:00
import ColumnSettings from '../components/column_settings' ;
2017-12-30 11:45:01 -06:00
import { changeSetting } from 'flavours/glitch/actions/settings' ;
2018-12-18 10:22:01 -06:00
import { setFilter } from 'flavours/glitch/actions/notifications' ;
2020-10-12 17:37:21 -05:00
import { clearNotifications , requestBrowserPermission } from 'flavours/glitch/actions/notifications' ;
2017-12-30 16:42:26 -06:00
import { changeAlerts as changePushNotifications } from 'flavours/glitch/actions/push_notifications' ;
2017-12-04 01:26:40 -06:00
import { openModal } from 'flavours/glitch/actions/modal' ;
2020-10-12 17:37:21 -05:00
import { showAlert } from 'flavours/glitch/actions/alerts' ;
2017-06-12 05:26:23 -05:00
const messages = defineMessages ( {
clearMessage : { id : 'notifications.clear_confirmation' , defaultMessage : 'Are you sure you want to permanently clear all your notifications?' } ,
clearConfirm : { id : 'notifications.clear' , defaultMessage : 'Clear notifications' } ,
2020-10-15 09:24:47 -05:00
permissionDenied : { id : 'notifications.permission_denied_alert' , defaultMessage : 'Desktop notifications can\'t be enabled, as browser permission has been denied before' } ,
2017-06-12 05:26:23 -05:00
} ) ;
2017-01-02 07:09:57 -06:00
const mapStateToProps = state => ( {
2017-05-20 10:31:47 -05:00
settings : state . getIn ( [ 'settings' , 'notifications' ] ) ,
2017-07-13 15:15:32 -05:00
pushSettings : state . get ( 'push_notifications' ) ,
2020-10-12 17:37:21 -05:00
alertsEnabled : state . getIn ( [ 'settings' , 'notifications' , 'alerts' ] ) . includes ( true ) ,
browserSupport : state . getIn ( [ 'notifications' , 'browserSupport' ] ) ,
browserPermission : state . getIn ( [ 'notifications' , 'browserPermission' ] ) ,
2017-01-02 07:09:57 -06:00
} ) ;
2017-06-12 05:26:23 -05:00
const mapDispatchToProps = ( dispatch , { intl } ) => ( {
2017-01-02 07:09:57 -06:00
2018-01-09 09:48:14 -06:00
onChange ( path , checked ) {
if ( path [ 0 ] === 'push' ) {
2020-10-12 17:37:21 -05:00
if ( checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
dispatch ( requestBrowserPermission ( ( permission ) => {
if ( permission === 'granted' ) {
dispatch ( changePushNotifications ( path . slice ( 1 ) , checked ) ) ;
} else {
dispatch ( showAlert ( undefined , messages . permissionDenied ) ) ;
}
} ) ) ;
} else {
dispatch ( changePushNotifications ( path . slice ( 1 ) , checked ) ) ;
}
2018-12-18 10:22:01 -06:00
} else if ( path [ 0 ] === 'quickFilter' ) {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
dispatch ( setFilter ( 'all' ) ) ;
2020-10-12 17:37:21 -05:00
} else if ( path [ 0 ] === 'alerts' && checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
if ( checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
dispatch ( requestBrowserPermission ( ( permission ) => {
if ( permission === 'granted' ) {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
} else {
dispatch ( showAlert ( undefined , messages . permissionDenied ) ) ;
}
} ) ) ;
} else {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
}
2017-07-13 15:15:32 -05:00
} else {
2018-01-09 09:48:14 -06:00
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
2017-07-13 15:15:32 -05:00
}
2017-01-10 10:25:10 -06:00
} ,
2017-06-12 05:26:23 -05:00
onClear ( ) {
dispatch ( openModal ( 'CONFIRM' , {
message : intl . formatMessage ( messages . clearMessage ) ,
confirm : intl . formatMessage ( messages . clearConfirm ) ,
onConfirm : ( ) => dispatch ( clearNotifications ( ) ) ,
} ) ) ;
} ,
2020-10-12 17:37:21 -05:00
onRequestNotificationPermission ( ) {
dispatch ( requestBrowserPermission ( ) ) ;
} ,
2017-01-02 07:09:57 -06:00
} ) ;
2017-06-12 05:26:23 -05:00
export default injectIntl ( connect ( mapStateToProps , mapDispatchToProps ) ( ColumnSettings ) ) ;