@ -1,9 +1,33 @@
import {
addCustomerPreference ,
getCustomerPreference ,
removeCustomerPreference ,
setCustomerPreference
} from 'src/api/subscriber'
import { RequestState } from 'src/store/common'
async function updateBooleanPreference ( context , { customerId , key , value } ) {
await savePreference ( context , ( ) => {
const preferences = Object . keys ( context . state . customerPreferences ? ? [ ] )
if ( preferences . includes ( key ) ) {
return value
? setCustomerPreference ( customerId , key , value )
: removeCustomerPreference ( customerId , key )
}
return addCustomerPreference ( customerId , key , value )
} )
}
async function savePreference ( context , apiFn ) {
try {
const result = await apiFn ( )
context . commit ( 'customerPreferencesUpdateLoaded' , result )
} catch ( err ) {
context . commit ( 'customerPreferencesLoadingFailed' , err . message )
}
}
export default {
namespaced : true ,
state : {
@ -14,7 +38,7 @@ export default {
} ,
getters : {
ignoreMembers ( state ) {
return state . ignoreMembers
return state . customerPreferences? . ignore _members _when _hunting
}
} ,
mutations : {
@ -51,67 +75,88 @@ export default {
} )
} )
} ,
updateIgnoreMembers ( context , options ) {
setCustomerPreference ( options . customerId , options . ignore _cf , 'ignore_cf_when_hunting' ) . then ( ( customerPreference ) => {
c ontext. commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context. commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateIgnoreMembers ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'ignore_cf_when_hunting' ,
value: data . ignore _cf _when _hunting
} )
} ,
updateBlockInMode ( context , options ) {
setCustomerPreference ( options . customerId , options . block _in _mode , 'block_in_mode' ) . then ( ( customerPreference ) => {
c ontext. commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context. commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockInMode ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'block_in_mode' ,
value: data . block _in _mode
} )
} ,
updateBlockInClir ( context , options ) {
setCustomerPreference ( options . customerId , options . block _in _clir , 'block_in_clir' ) . then ( ( customerPreference ) => {
c ontext. commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context. commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockInClir ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'block_in_clir' ,
value: data . block _in _clir
} )
} ,
updateBlockOutMode ( context , options ) {
setCustomerPreference ( options . customerId , options . block _out _mode , 'block_out_mode' ) . then ( ( customerPreference ) => {
c ontext. commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context. commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockOutMode ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'block_out_mode' ,
value: data . block _out _mode
} )
} ,
updateBlockInList ( context , options ) {
setCustomerPreference ( options . customerId , options . block _in _list , 'block_in_list' ) . then ( ( customerPreference ) => {
context . commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context . commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockInList ( context , data ) {
await savePreference ( context , ( ) => {
if ( ! Array . isArray ( data . block _in _list ) || ! data . block _in _list . length ) {
return removeCustomerPreference ( data . customerId , 'block_in_list' )
}
const preferences = Object . keys ( context . state . customerPreferences ? ? [ ] )
if ( preferences . includes ( 'block_in_list' ) ) {
return setCustomerPreference ( data . customerId , 'block_in_list' , data . block _in _list )
}
return addCustomerPreference ( data . customerId , 'block_in_list' , data . block _in _list )
} )
} ,
updateBlockOutList ( context , options ) {
setCustomerPreference ( options . customerId , options . block _out _list , 'block_out_list' ) . then ( ( customerPreference ) => {
context . commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context . commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockOutList ( context , data ) {
await savePreference ( context , ( ) => {
if ( ! Array . isArray ( data . block _out _list ) || ! data . block _out _list . length ) {
return removeCustomerPreference ( data . customerId , 'block_out_list' )
}
const preferences = Object . keys ( context . state . customerPreferences ? ? [ ] )
if ( preferences . includes ( 'block_out_list' ) ) {
return setCustomerPreference ( data . customerId , 'block_out_list' , data . block _out _list )
}
return addCustomerPreference ( data . customerId , 'block_out_list' , data . block _out _list )
} )
} ,
updateBlockOutOverridePin ( context , options ) {
setCustomerPreference ( options . customerId , options . block _out _override _pin , 'block_out_override_pin' ) . then ( ( customerPreference ) => {
context . commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context . commit ( 'customerPreferencesLoadingFailed' , err . message )
async updateBlockOutOverridePin ( context , data ) {
await savePreference ( context , ( ) => {
if ( ! data . block _out _override _pin ? . trim ( ) ) {
return removeCustomerPreference ( data . customerId , 'block_out_override_pin' )
}
const preferences = Object . keys ( context . state . customerPreferences ? ? [ ] )
if ( preferences . includes ( 'block_out_override_pin' ) ) {
return setCustomerPreference ( data . customerId , 'block_out_override_pin' , data . block _out _override _pin )
}
return addCustomerPreference ( data . customerId , 'block_out_override_pin' , data . block _out _override _pin )
} )
} ,
updatePlayAnnounceBeforeCallSetup ( context , options ) {
setCustomerPreference ( options . customerId , options . play _announce _before _call _setup , 'play_announce_before_call_setup' ) . then ( ( customerPreference ) => {
context . commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context . commit ( 'customerPreferencesLoadingFailed' , err . message )
async updatePlayAnnounceBeforeCallSetup ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'play_announce_before_call_setup' ,
value: data . play _announce _before _call _setup
} )
} ,
updatePlayAnnounceToCallee ( context , options ) {
setCustomerPreference ( options . customerId , options . play _announce _to _callee , 'play_announce_to_callee' ) . then ( ( customerPreference ) => {
c ontext. commit ( 'customerPreferencesUpdateLoaded' , customerPreference )
} ) . catch ( ( err ) => {
context. commit ( 'customerPreferencesLoadingFailed' , err . message )
async updatePlayAnnounceToCallee ( context , data ) {
return updateBooleanPreference ( context , {
c ustomerId: data . customerId ,
key : 'play_announce_to_callee' ,
value: data . play _announce _to _callee
} )
}
}