diff --git a/src/api/call-forward.js b/src/api/call-forward.js index 5f75b5a6..6b5be070 100644 --- a/src/api/call-forward.js +++ b/src/api/call-forward.js @@ -88,7 +88,13 @@ export function getDestinationsets(id) { return Promise.resolve(result); } }).then((result) => { - resolve(getJsonBody(result.body)._embedded['ngcp:cfdestinationsets']); + if(getJsonBody(result.body)._embedded){ + resolve(getJsonBody(result.body)._embedded['ngcp:cfdestinationsets']); + } + else{ + resolve([]); + } + }).catch((err) => { reject(err); }); diff --git a/src/components/pages/NewCallForward/CscCallForwardGroup.vue b/src/components/pages/NewCallForward/CscCallForwardGroup.vue index 3f1b4171..c9f98811 100644 --- a/src/components/pages/NewCallForward/CscCallForwardGroup.vue +++ b/src/components/pages/NewCallForward/CscCallForwardGroup.vue @@ -1,25 +1,33 @@ <template> <div class="csc-cf-group" + v-if="group.destinations.length > 0" > + + <!-- <csc-object-spinner + /> --> <div v-if="group.id !== 'unconditional'" class="csc-cf-group-title" > {{ group.title }} </div> + <div v-for="(destination, index) in group.destinations" :key="genKey()" > <csc-new-call-forward-destination - :destination="destination" + :destination="getDestination(index)" :index="index" :groupId="group.id" :groupName="group.name" + :allCallsFwd="group.name == 'csc-unconditional' && index === 0" /> </div> - <div class="row csc-cf-destination-cont"> + <div + class="row csc-cf-destination-cont" + > <div class="col col-xs-12 col-md-4 text-right"></div> <div class="col col-xs-12 col-md-2 text-left" @@ -47,58 +55,13 @@ </div> <div class="col col-xs-12 col-md-6 "></div> </div> - - <!-- <div - class="row" - > - <div - class="csc-cf-destination-label col col-4" - > - {{ $t('pages.newCallForward.allCallsForwardedTo') }} - </div> - <div - class="csc-cf-destination-value col col-2" - > - <span - class="csc-text-action" - > - {{ $t('pages.newCallForward.addDestinationLabel') }} - <q-popover - ref="groupMenu" - :disable="true" - > - <q-list - link - no-border - > - <q-item - > - <q-item-side - icon="number" - /> - <q-item-main> - Number - </q-item-main> - </q-item> - </q-list> - </q-popover> - </span> - </div> - <div - class="csc-cf-destination-actions col col-6" - > - <q-icon - name="delete" - color="negative" - size="24px" - /> - </div> - </div> --> </div> </template> <script> - + import { + mapGetters, + } from 'vuex' import { QSpinnerDots, QIcon, @@ -108,7 +71,7 @@ QItemMain, QItemSide } from 'quasar-framework' - + import CscObjectSpinner from "../../CscObjectSpinner"; import CscNewCallForwardDestination from './CscNewCallForwardDestination' export default { @@ -124,6 +87,7 @@ QItem, QItemMain, QItemSide, + CscObjectSpinner, CscNewCallForwardDestination }, data () { @@ -132,6 +96,9 @@ }; }, computed: { + ...mapGetters('newCallForward', [ + 'getOwnPhoneTimeout' + ]), showAddDestBtn(){ const destinations = this.group.destinations; for(let dest of destinations){ @@ -157,6 +124,16 @@ await this.$store.dispatch('newCallForward/loadForwardGroups'); this.destinationInCreation = false; + }, + getDestination(index){ + let destination = {...this.group.destinations[index]} + if(index === 0){ + destination.timeout = this.getOwnPhoneTimeout; + } + else { + destination.timeout = this.group.destinations[index-1].timeout; + } + return destination; } } } diff --git a/src/components/pages/NewCallForward/CscNewCallForward.vue b/src/components/pages/NewCallForward/CscNewCallForward.vue index 2bf977f0..3db2a0e9 100644 --- a/src/components/pages/NewCallForward/CscNewCallForward.vue +++ b/src/components/pages/NewCallForward/CscNewCallForward.vue @@ -18,16 +18,23 @@ <div class="col col-xs-12 col-md-6" - ></div> + + > + <q-toggle + v-if="showSwitcher" + v-model="toggleDefaultNumber" + @change="toggleChange" /> + + </div> </div> <div class="csc-cf-row row"> <div class="column col col-xs-12 col-md-4 items-end" > + <div class="csc-text-action" - @click="addForward" > <q-icon name="add" @@ -41,8 +48,21 @@ color="primary" :size="24" /> + + <q-popover + ref="destsetTypeForm" + anchor="top right" + @close="addForwardGroup()" + + > + <csc-new-call-forward-destinationset-type-select + ref="destsetTypeForm" + /> + </q-popover> </div> + + </div> </div> <div @@ -51,6 +71,7 @@ :key="forwardGroup.id" > <csc-cf-group + v-if="!groupInCreation" :group="forwardGroup" /> </div> @@ -90,6 +111,7 @@ import CscPage from '../../CscPage' import CscNewCallForwardDestination from './CscNewCallForwardDestination' import CscNewCallForwardAddDestinationForm from './CscNewCallForwardAddDestinationForm' + import CscNewCallForwardDestinationsetTypeSelect from './CscNewCallForwardDestinationsetTypeSelect' import CscCfGroup from "./CscCallForwardGroup"; export default { components: { @@ -97,6 +119,7 @@ CscPage, CscNewCallForwardDestination, CscNewCallForwardAddDestinationForm, + CscNewCallForwardDestinationsetTypeSelect, QSpinnerDots, QField, QToggle, @@ -110,12 +133,22 @@ data () { return { groupInCreation: false, - groupsLoading: false + groupsLoading: false, + toggleDefaultNumber: true }; }, async mounted(){ this.groupsLoading = true; - await this.$store.dispatch('newCallForward/loadForwardGroups'); + try{ + await this.$store.dispatch('newCallForward/loadMappings'); + await this.$store.dispatch('newCallForward/loadForwardGroups'); + let unconditionalGroup = await this.$store.dispatch('newCallForward/getForwardGroupByName', 'unconditional'); + this.toggleDefaultNumber = !unconditionalGroup || unconditionalGroup.destinations.length < 1; + } + catch(err){ + console.log(err) + } + this.groupsLoading = false; }, @@ -123,23 +156,34 @@ ...mapGetters('newCallForward', [ 'primaryNumber', 'subscriberDisplayName', - 'forwardGroups' + 'forwardGroups', + 'showSwitcher' ]), primaryNumberEnabled(){ return true; }, toggleLabel(){ - return `${this.$t('pages.newCallForward.primarNumberEnabled')}`; + return this.toggleDefaultNumber ? `${this.$t('pages.newCallForward.primarNumberEnabled')}` : `${this.$t('pages.newCallForward.primarNumberDisabled')}`; } }, methods: { - async addForward(){ + async addForwardGroup(){ this.groupInCreation = true; - const unconditionalFwdGroup = await this.$store.dispatch('newCallForward/getForwardGroupByName', 'unconditional'); - if(!unconditionalFwdGroup){ - await this.$store.dispatch('newCallForward/addForwardGroup', 'unconditional'); - await this.$store.dispatch('newCallForward/loadForwardGroups'); + if(this.toggleDefaultNumber){ + const timeoutFwdGroup = await this.$store.dispatch('newCallForward/getForwardGroupByName', 'timeout'); + if(!timeoutFwdGroup){ + await this.$store.dispatch('newCallForward/addForwardGroup', 'timeout'); + await this.$store.dispatch('newCallForward/loadForwardGroups'); + } + } + else{ + const unconditionalFwdGroup = await this.$store.dispatch('newCallForward/getForwardGroupByName', 'unconditional'); + if(!unconditionalFwdGroup){ + await this.$store.dispatch('newCallForward/addForwardGroup', 'unconditional'); + await this.$store.dispatch('newCallForward/loadForwardGroups'); + } } + this.groupInCreation = false; }, togglePrimaryNumber(){}, @@ -147,7 +191,12 @@ this.$refs.destinationType.close(); this.$refs.addDestinationForm.add(); }, - addVoicemail(){}, + async toggleChange(){ + this.groupInCreation = true; + await this.$store.dispatch('newCallForward/forwardAllCalls', !this.toggleDefaultNumber); + this.groupInCreation = false; + }, + addVoicemail(){} } } </script> diff --git a/src/components/pages/NewCallForward/CscNewCallForwardAddDestinationForm.vue b/src/components/pages/NewCallForward/CscNewCallForwardAddDestinationForm.vue index af8f9e94..4d6d5b9d 100644 --- a/src/components/pages/NewCallForward/CscNewCallForwardAddDestinationForm.vue +++ b/src/components/pages/NewCallForward/CscNewCallForwardAddDestinationForm.vue @@ -78,6 +78,7 @@ 'index', 'disable', 'loading', + 'groupName' ], validations: { number: { @@ -96,7 +97,7 @@ }, methods: { async save() { - const forwardGroupName = 'unconditional'; // gonna be dynamic + const forwardGroupName = this.groupName; const forwardGroup = await this.$store.dispatch('newCallForward/getForwardGroupByName', forwardGroupName); if (this.numberError || this.saveDisabled) { diff --git a/src/components/pages/NewCallForward/CscNewCallForwardDestination.vue b/src/components/pages/NewCallForward/CscNewCallForwardDestination.vue index 6d2897fc..085aab49 100644 --- a/src/components/pages/NewCallForward/CscNewCallForwardDestination.vue +++ b/src/components/pages/NewCallForward/CscNewCallForwardDestination.vue @@ -4,8 +4,11 @@ :class="removed" > <div class="col col-xs-12 col-md-4 text-right"> - {{ $t('pages.newCallForward.destinationTimeoutLabel') }} - <span class='csc-cf-timeout'> + {{ this.allCallsFwd ? '' : $t('pages.newCallForward.destinationTimeoutLabel') }} + <span + v-if="!this.allCallsFwd" + class='csc-cf-timeout' + > {{this.destinationTimeout}} <q-popover ref="timeoutForm" @@ -18,13 +21,13 @@ label label-always :step="5" - :min="0" + :min="5" :max="300" snap /> </q-popover> </span> - {{ $t('pages.newCallForward.destinationNumberLabel') }} + {{ this.allCallsFwd ? $t('pages.newCallForward.allCallsForwardedTo') : $t('pages.newCallForward.destinationNumberLabel') }} </div> <div class="col text-left col-xs-12 col-md-2 csc-cf-dest-number-cont"> @@ -32,19 +35,33 @@ {{ !this.destinationNumber || this.destinationNumber.length < 2 ? $t('pages.newCallForward.destinationLabel') : this.destinationNumber}} + <q-popover + ref="destTypeForm" + anchor="top right" + @open="showDestTypeForm()" + @close="showNumberFormPopover()" + > + <csc-new-call-forward-destination-type-form + ref="selectDestinationType" + /> + </q-popover> <q-popover ref="numberForm" anchor="top right" class="csc-cf-number-form" + v-bind:class="{ 'csc-cf-popover-hide': toggleNumberForm }" + v-if="" @open="showNumberForm()" > <csc-new-call-forward-add-destination-form ref="addDestinationForm" :index="this.destinationIndex" :destination="this.destinationNumber" + :groupName="this.groupName" /> </q-popover> </div> + </div> <div class="col col-xs-12 col-md-5 csc-cf-destination-actions"> <q-icon @@ -53,6 +70,11 @@ size="24px" @click="showConfirmDialog" /> + <q-spinner-dots + v-if="showDots" + color="primary" + :size="24" + /> <csc-confirm-dialog ref="confirmDialog" title-icon="delete" @@ -66,6 +88,9 @@ </template> <script> + import { + mapGetters, + } from 'vuex' import { QIcon, QBtn, @@ -73,10 +98,12 @@ QSlider, QList, QItem, - QItemMain + QItemMain, + QSpinnerDots } from 'quasar-framework' import CscConfirmDialog from "../../CscConfirmationDialog"; import CscNewCallForwardAddDestinationForm from './CscNewCallForwardAddDestinationForm' + import CscNewCallForwardDestinationTypeForm from './CscNewCallForwardDestinationTypeForm' export default { name: 'csc-new-call-forward-destination', components: { @@ -87,10 +114,13 @@ QList, QItem, QItemMain, + QSpinnerDots, CscConfirmDialog, + CscNewCallForwardDestinationTypeForm, CscNewCallForwardAddDestinationForm }, props: [ + 'allCallsFwd', 'groupId', 'groupName', 'destination', @@ -104,23 +134,35 @@ destinationTimeout: 0, destinationNumber: null, destinationIndex: null, - isRemoved: false + showDots: false, + toggleNumberForm: true } }, computed: { + ...mapGetters('newCallForward', [ + 'getOwnPhoneTimeout' + ]), removed(){ - return this.isRemoved ? "csc-cf-removed-destination" : ""; + return this.showDots ? "csc-cf-removed-destination" : ""; } }, methods: { updateValues(destination){ - this.destinationTimeout = destination.timeout; + this.destinationTimeout = this.index === 0 && this.groupName === 'csc-timeout' ? this.getOwnPhoneTimeout : destination.timeout; this.destinationNumber = destination.simple_destination; this.destinationIndex = this.index; }, + showNumberFormPopover(){ // temporarily called onClose + this.toggleNumberForm = false; + this.$refs.numberForm.open(); + }, showNumberForm(){ this.$refs.addDestinationForm.add(); }, + showDestTypeForm(){ + this.toggleNumberForm = true; + this.$refs.selectDestinationType.add(); + }, async saveTimeout(){ this.$store.dispatch('newCallForward/editTimeout', { index: this.destinationIndex, @@ -131,15 +173,13 @@ showConfirmDialog(){ this.$refs.confirmDialog.open(); }, - deleteDestination(){ - this.isRemoved = true; - setTimeout(async ()=>{ - await this.$store.dispatch('newCallForward/removeDestination', { - destination: this.destination, - forwardGroupId: this.groupId - }); - await this.$store.dispatch('newCallForward/loadForwardGroups'); - }, 1200); + async deleteDestination(){ + this.showDots = true; + await this.$store.dispatch('newCallForward/removeDestination', { + destination: this.destination, + forwardGroupId: this.groupId + }); + await this.$store.dispatch('newCallForward/loadForwardGroups'); } } } @@ -160,8 +200,8 @@ cursor pointer .csc-cf-timeout-form, .csc-cf-number-form - min-width 120px padding 0 20px 0 20px + min-width 120px .csc-cf-dest-number-cont padding-left 30px .csc-cf-destination-actions @@ -171,4 +211,6 @@ visibility hidden opacity 0 transition visibility 0s 1s, opacity 1s linear + .csc-cf-popover-hide + display none </style> diff --git a/src/components/pages/NewCallForward/CscNewCallForwardDestinationTypeForm.vue b/src/components/pages/NewCallForward/CscNewCallForwardDestinationTypeForm.vue new file mode 100644 index 00000000..483f5474 --- /dev/null +++ b/src/components/pages/NewCallForward/CscNewCallForwardDestinationTypeForm.vue @@ -0,0 +1,61 @@ +<template> + <div + v-if="enabled" + > + <div + class="csc-cf-dest-type" + @click="showNumberForm()" + > + {{ $t('pages.newCallForward.numberLabel') }} + </div> + <div + class="csc-cf-dest-type" + > + {{ $t('pages.newCallForward.voiceMailLabel') }} + </div> + </div> +</template> + +<script> + import CscSpinner from '../../CscSpinner' + import { } from 'quasar-framework' + + export default { + name: 'csc-new-call-forward-destination-type-form', + components: { + CscSpinner + }, + data () { + return { + enabled: false + } + }, + props: [ + ], + computed: {}, + methods: { + showNumberForm(){ + this.$parent.close() + }, + cancel() { + this.enabled = false; + }, + add() { + this.enabled = true; + }, + close() { + this.enabled = false; + }, + } + } +</script> + +<style lang="stylus" rel="stylesheet/stylus"> + @import '../../../themes/app.common.styl' + .csc-cf-dest-type + min-width 100px + padding 10px + cursor pointer + .csc-cf-dest-type:hover + background $main-menu-item-hover-background +</style> diff --git a/src/components/pages/NewCallForward/CscNewCallForwardDestinationsetTypeSelect.vue b/src/components/pages/NewCallForward/CscNewCallForwardDestinationsetTypeSelect.vue new file mode 100644 index 00000000..5b981c9c --- /dev/null +++ b/src/components/pages/NewCallForward/CscNewCallForwardDestinationsetTypeSelect.vue @@ -0,0 +1,53 @@ +<template> + <div + v-if="enabled" + > + <div + class="csc-cf-dest-type" + @click="addDestination()" + > + {{ $t('pages.newCallForward.uncoditionalLabel') }} + </div> + </div> +</template> + +<script> + import CscSpinner from '../../CscSpinner' + import { } from 'quasar-framework' + + export default { + name: 'csc-new-call-forward-destinationset-type-select', + components: { + CscSpinner + }, + data () { + return { + enabled: true + } + }, + methods: { + addDestination(){ + this.$parent.close() + }, + cancel() { + this.enabled = false; + }, + add() { + this.enabled = true; + }, + close() { + this.enabled = false; + }, + } + } +</script> + +<style lang="stylus" rel="stylesheet/stylus"> + @import '../../../themes/app.common.styl' + .csc-cf-dest-type + min-width 100px + padding 10px + cursor pointer + .csc-cf-dest-type:hover + background $main-menu-item-hover-background +</style> diff --git a/src/locales/en.json b/src/locales/en.json index 306ce5d2..48e42543 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -212,7 +212,7 @@ }, "newCallForward": { "primarNumberEnabled": "All calls go to the primary number", - "primarNumberDisabled": "No call goes to", + "primarNumberDisabled": "No call goes to primary number", "forwardBtnLabel": "Add forwarding", "numberLabel": "Number", "voiceMailLabel": "Voicemail", @@ -222,7 +222,8 @@ "addDestinationLabel": "Add destination", "allCallsForwardedTo": "All calls forwarded to", "cancelDialogTitle": "Delete from {groupName} forwarding", - "cancelDialogText": "You are about to delete {destination} from {groupName} call forwarding" + "cancelDialogText": "You are about to delete {destination} from {groupName} call forwarding", + "uncoditionalLabel": "Unconditional" }, "callForward": { "titles": { diff --git a/src/store/new-call-forward.js b/src/store/new-call-forward.js index 1ae572f7..2fc8bb7b 100644 --- a/src/store/new-call-forward.js +++ b/src/store/new-call-forward.js @@ -1,49 +1,36 @@ 'use strict'; import Vue from 'vue' - -// import _ from 'lodash'; -// import { RequestState } from './common' -// import { i18n } from '../i18n'; - import { - // getSourcesets, + getMappings, getDestinationsets, addNewDestinationsetWithName, - // deleteDestinationFromDestinationset, + deleteDestinationsetById, addDestinationToDestinationset, - // addDestinationToEmptyGroup, - // addDestinationToExistingGroup, - // changePositionOfDestination, - // moveDestinationUp, - // moveDestinationDown, - // loadTimesetTimes, - // deleteTimeFromTimeset, - // deleteTimesetById, - // resetTimesetByName, - // createTimesetWithTime, - // appendTimeToTimeset, - // loadDestinations, - // createSourcesetWithSource, - // appendSourceToSourceset, - // deleteSourcesetById, - // deleteSourceFromSourcesetByIndex, - // flipCfuAndCft, - // getOwnPhoneTimeout, - // updateOwnPhoneTimeout + addNewMapping, + updateOwnPhoneTimeout } from '../api/call-forward'; const ForwardGroup = { - unconditional: 'unconditional' + unconditional: { + name : 'csc-unconditional', + mapping: 'cfu' + }, + timeout: { + name: 'csc-timeout', + mapping: 'cft' + } }; export default { namespaced: true, state: { - forwardGroups: [] + mappings: [], + forwardGroups: [], + showSwitcher: true }, getters: { primaryNumber(state, getters, rootState, rootGetters) { - let subscriber = rootGetters['user/getSubscriber']; + const subscriber = rootGetters['user/getSubscriber']; if(subscriber !== null) { return subscriber.primary_number; @@ -55,11 +42,14 @@ export default { subscriberDisplayName(state, getters, rootState, rootGetters) { return rootGetters['user/getUsername']; }, - // destinations(state) { - // return state.destinations; - // }, forwardGroups(state){ return state.forwardGroups; + }, + getOwnPhoneTimeout(state){ + return parseInt(state.mappings.cft_ringtimeout); + }, + showSwitcher(state){ + return state.showSwitcher; } }, mutations: { @@ -82,48 +72,127 @@ export default { let group = state.forwardGroups.find((group)=>{ return group.id === data.forwardGroupId; }); - let destination = group.destinations.slice(data.index, data.index+1)[0]; + let destination = group.destinations.slice(data.index-1, data.index)[0]; destination.timeout = data.timeout; - Vue.set(group.destinations, data.index, destination) + Vue.set(group.destinations, data.index-1, destination) + }, + loadMappings(state, mappings){ + state.mappings = mappings; }, loadForwardGroups(state, forwardGroups){ state.forwardGroups = forwardGroups; }, + setShowSwitcher(state){ + const forwardGroups = state.forwardGroups; + const timeoutGroup = forwardGroups.find(($forwardGroup) => { + return $forwardGroup.name === 'csc-timeout'; + }); + const unconditionalGroup = forwardGroups.find(($forwardGroup) => { + return $forwardGroup.name === 'csc-unconditional'; + }); + state.showSwitcher = (timeoutGroup && timeoutGroup.destinations.length > 0) || (unconditionalGroup && unconditionalGroup.destinations.length > 0); + } }, actions: { + async loadMappings(context) { + try{ + const mappings = await getMappings(localStorage.getItem('subscriberId')); + context.commit('loadMappings', mappings); + } + catch(err){ + console.log(err) + } + }, async loadForwardGroups(context) { try{ const forwardGroups = await getDestinationsets(localStorage.getItem('subscriberId')); context.commit('loadForwardGroups', forwardGroups); + context.commit('setShowSwitcher'); + return forwardGroups; + } + catch(err){ + console.log(err) + } + }, + async editMapping(context, data){ + try{ + const subscriberId = localStorage.getItem('subscriberId'); + const groupMappingId = ForwardGroup[data.name].mapping; + const allMappings = await getMappings(subscriberId); + let groupMappings = allMappings[groupMappingId]; + + groupMappings.push({ + "destinationset_id": data.groupId, + "sourceset_id":null, + "timeset_id":null + }); + + await addNewMapping({ + mappings: groupMappings, + group: groupMappingId, + subscriberId: subscriberId + }); + + context.dispatch('loadMappings'); } catch(err){ console.log(err) } + }, async addForwardGroup(context, name) { try{ + const newForwardGroupId = await addNewDestinationsetWithName(ForwardGroup[name].name); const destination = { "announcement_id": null, "simple_destination": " ", "destination": " ", "priority": 1, - "timeout": 20 + "timeout": 5 }; - const newForwardGroupId = await addNewDestinationsetWithName(ForwardGroup[name]); + + await context.dispatch('editMapping', { + name: name, + groupId: newForwardGroupId + }); + await addDestinationToDestinationset({ id: newForwardGroupId, data: [destination] }); + + // setting cft_ringtimeout in case it is + // not set while creating timeout group + if(name === 'timeout' && !context.getters.getOwnPhoneTimeout){ + await context.dispatch('editRingTimeout', 5); + } + return newForwardGroupId; } catch(err){ console.log(err) } }, + async deleteForwardGroup(context, group) { + try{ + const subscriberId = localStorage.getItem('subscriberId'); + const groupMappingId = group.name === 'csc-unconditional' ? 'cfu' : 'cft'; + await deleteDestinationsetById(group.id); + await addNewMapping({ + mappings: [], + group: groupMappingId, + subscriberId: subscriberId + }); + context.dispatch('loadMappings'); + } + catch(err){ + console.log(err) + } + }, getForwardGroupByName(context, name){ let forwardGroups = context.getters.forwardGroups; forwardGroups = forwardGroups.filter(($forwardGroup) => { - return $forwardGroup.name === name; + return ForwardGroup[name] ? $forwardGroup.name === ForwardGroup[name].name : $forwardGroup.name === name; }); return forwardGroups.length > 0 ? forwardGroups[0] : null; }, @@ -137,7 +206,7 @@ export default { "simple_destination": data.destination, "destination": data.destination, "priority": 1, - "timeout": 20 + "timeout": 5 }; await addDestinationToDestinationset({ id: data.forwardGroupId, @@ -148,20 +217,37 @@ export default { console.log(err); } }, - async removeDestination(context, data){ + async replaceDestinations(context, data){ try{ - let group = context.state.forwardGroups.find((group)=>{ + return group.name === ForwardGroup[data.groupName].name; + }); + await addDestinationToDestinationset({ + id: group.id, + data: data.destinations + }); + } + catch(err){ + console.log(err); + } + }, + async removeDestination(context, data){ + try{ + let destinations, group = context.state.forwardGroups.find((group)=>{ return group.id === data.forwardGroupId; }); - - group.destinations = group.destinations.filter(($destination) => { + destinations = group.destinations.filter(($destination) => { return $destination.destination !== data.destination.destination; }); await addDestinationToDestinationset({ id: group.id, - data: group.destinations + data: destinations }); + if(destinations.length < 1){ + context.dispatch('deleteForwardGroup', group); + context.dispatch('loadForwardGroups', group); + context.dispatch('loadMappings', group); + } } catch(err){ console.log(err); @@ -175,22 +261,91 @@ export default { destination.simple_destination = data.destination; destination.destination = data.destination; context.commit('editDestination', data); - await addDestinationToDestinationset({ - id: data.forwardGroupId, - data: group.destinations - }); + try{ + await addDestinationToDestinationset({ + id: data.forwardGroupId, + data: group.destinations + }); + } + catch(err){ + console.log(err) + } + }, + async editRingTimeout(context, timeout){ + try{ + await updateOwnPhoneTimeout({ + subscriberId: localStorage.getItem('subscriberId'), + timeout: timeout + }); + await context.dispatch('loadMappings'); + } + catch(err){ + console.log(err) + } }, async editTimeout(context, data){ - let group = context.state.forwardGroups.find((group)=>{ - return group.id === data.forwardGroupId; - }); - let destination = group.destinations.slice(data.index, data.index+1)[0]; - destination.timeout = data.timeout; - context.commit('editTimeout', data); - await addDestinationToDestinationset({ - id: group.id, - data: group.destinations - }); + if(data.index === 0){ // first row -> change cft_ringtimeout + context.dispatch('editRingTimeout', data.timeout); + } + else{ + const group = context.state.forwardGroups.find((group)=>{ + return group.id === data.forwardGroupId; + }); + let destination = group.destinations.slice(data.index-1, data.index)[0]; + destination.timeout = data.timeout; + context.commit('editTimeout', data); + try{ + await addDestinationToDestinationset({ + id: group.id, + data: group.destinations + }); + } + catch(err){ + console.log(err) + } + } + + }, + async forwardAllCalls(context, noSelfNumber){ + try{ + let unconditionalGroup = await context.dispatch('getForwardGroupByName', 'unconditional') + let timeoutGroup = await context.dispatch('getForwardGroupByName', 'timeout'); + + if(!unconditionalGroup){ + await context.dispatch('addForwardGroup', 'unconditional'); + await context.dispatch('loadMappings'); + await context.dispatch('loadForwardGroups'); + unconditionalGroup = await context.dispatch('getForwardGroupByName', 'unconditional'); + } + if(!timeoutGroup){ + await context.dispatch('addForwardGroup', 'timeout'); + await context.dispatch('loadMappings'); + await context.dispatch('loadForwardGroups'); + timeoutGroup = await context.dispatch('getForwardGroupByName', 'timeout'); + } + + if(noSelfNumber){ + await context.dispatch('replaceDestinations', { + groupName: 'unconditional', + destinations: timeoutGroup.destinations + }); + await context.dispatch('deleteForwardGroup', timeoutGroup); + } + else{ + await context.dispatch('replaceDestinations', { + groupName: 'timeout', + destinations: unconditionalGroup.destinations + }); + await context.dispatch('deleteForwardGroup', unconditionalGroup); + + + } + await context.dispatch('loadMappings'); + await context.dispatch('loadForwardGroups'); + } + catch(err){ + console.log(err) + } } } };