import graphql from '../utils/graphql' ; import * as GlobalData from '../../common/global-data' ; export function setOnlineUserCount ( online ) { return ( dispatch , getState ) => { dispatch ( { type : 'SET_ONLINE_STATUS' , online } ) ; } ; } export function saveTab ( tab ) { return ( dispatch , getState ) => { dispatch ( { type : 'SET_TAB' , tab } ) ; } ; } export function requestNotificationPermission ( ) { return ( dispatch , getState ) => { if ( 'Notification' in window ) { Notification . requestPermission ( ( result ) => { if ( result == 'granted' ) { dispatch ( { type : 'SET_NOTIFICATION_PERMISSION' , status : true } ) ; } } ) ; } } ; } export function sendNotification ( { content , option } ) { return async ( dispatch , getState ) => { if ( typeof Notification == 'undefined' ) { return ; } let notification = new Notification ( content , option ) ; let href = '' ; notification . onclick = ( res ) => { switch ( res . target . tag ) { case 'message' : href = '/sessions' ; break ; case 'comment' : href = '/comment/' + ( option . data . comment_id . parent_id ? option . data . comment_id . parent_id : option . data . comment_id . _id ) ; break ; } if ( href ) { const history = GlobalData . get ( 'history' ) ; if ( history ) history . push ( href ) ; } notification . close ( ) ; } ; } ; } export function loadOperatingStatus ( ) { return ( dispatch , getState ) => { return new Promise ( async ( resolve ) => { let [ err , res ] = await graphql ( { apis : [ { api : 'countPosts' , args : { } , fields : `count` } , { api : 'countUsers' , args : { } , fields : `count` } , { api : 'countComments' , args : { parent_id : 'not-exists' } , fields : `count` } , { api : 'countComments' , args : { parent_id : 'exists' } , fields : `count` , aliases : 'countReply' } ] } ) ; dispatch ( { type : 'SET_OPERATING_STATUS' , data : res } ) ; resolve ( [ err , res ] ) ; } ) ; } ; }