diff --git a/src/api/call-forward.js b/src/api/call-forward.js index 53b19dfa..16d449d5 100644 --- a/src/api/call-forward.js +++ b/src/api/call-forward.js @@ -816,3 +816,30 @@ export function deleteSourcesetById(id) { }); }); } + +export function deleteItemFromArrayByIndex(options) { + return options.array.filter((item, index) => { + return options.index !== index; + }) +} + +export function deleteSourceFromSourcesetByIndex(options) { + return new Promise((resolve, reject) => { + let sources = deleteItemFromArrayByIndex({ + array: options.sources, + index: options.sourceIndex + }); + let headers = { + 'Content-Type': 'application/json-patch+json' + }; + Vue.http.patch('/api/cfsourcesets/' + options.sourceset.sourcesetId, [{ + op: 'replace', + path: '/sources', + value: sources + }], { headers: headers }).then(() => { + resolve(); + }).catch(err => { + reject(err); + }); + }); +} diff --git a/src/components/pages/CallForward/CscSourcesets.vue b/src/components/pages/CallForward/CscSourcesets.vue index 3bfdeefa..61a61097 100644 --- a/src/components/pages/CallForward/CscSourcesets.vue +++ b/src/components/pages/CallForward/CscSourcesets.vue @@ -49,17 +49,25 @@ name="contact_phone" class="sources-icon" /> - {{ $t('pages.callForward.sources.sourcesTitleMode', - { mode: capitalizedMode(sourceset.sourcesetMode) }) }} + {{ $t('pages.callForward.sources.sourcesTitleMode', + { mode: capitalizedMode(sourceset.sourcesetMode) }) }} + {{ source.source }} + + 0 && this.sourcesetName.length > 0; @@ -214,6 +231,46 @@ } }, methods: { + removeSource(sourceset, source, index) { + let self = this; + let sources = this.sourcesetSources(sourceset.sourcesetId); + let isLastSource = sources.length === 1; + if (isLastSource) { + this.alertDeleteLastSource(); + } + else { + Dialog.create({ + title: self.$t('pages.callForward.sources.removeSourceDialogTitle'), + message: self.$t('pages.callForward.sources.removeSourceDialogText', { + source: source + }), + buttons: [ + self.$t('buttons.cancel'), + { + label: self.$t('buttons.remove'), + color: 'negative', + handler () { + self.$store.dispatch('callForward/deleteSourceFromSourcesetByIndex', { + sourceset: sourceset, + sources: sources, + sourceIndex: index + }); + } + } + ] + }); + } + }, + alertDeleteLastSource() { + Alert.create({ + enter: 'bounceInRight', + leave: 'bounceOutRight', + position: 'top-center', + html: this.$t('pages.callForward.sources.removeLastSourceDialogText'), + icon: 'warning', + dismissible: true + }); + }, capitalizedMode(mode) { return `${mode.charAt(0).toUpperCase()}${mode.slice(1)}`; }, @@ -297,10 +354,13 @@ ] }); }, - loadAll() { + loadDestinations() { this.$store.dispatch('callForward/loadDestinations', { timeset: this.timesetName }); + }, + loadAll() { + this.loadDestinations(); this.$store.dispatch('callForward/loadSourcesets'); } }, @@ -319,9 +379,7 @@ source: this.lastAddedSource })); this.$store.dispatch('callForward/loadSourcesets'); - this.$store.dispatch('callForward/loadDestinations', { - timeset: this.timesetName - }); + this.loadDestinations(); this.closeForm(); } @@ -342,6 +400,22 @@ this.loadAll(); this.resetForm(); } + }, + removeSourceState(state) { + if (state === 'requesting') { + startLoading(); + } + else if (state === 'failed') { + stopLoading(); + showGlobalError(this.removeSourceError); + } + else if (state === 'succeeded') { + stopLoading(); + showToast(this.$t('pages.callForward.sources.removeSourceSuccessMessage', { + source: this.lastRemovedSource + })); + this.loadAll(); + } } } } diff --git a/src/locales/en.json b/src/locales/en.json index fe847677..bdc624fb 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -12,7 +12,8 @@ "edit": "Edit", "add": "Add", "moveUp": "Move up", - "moveDown": "Move down" + "moveDown": "Move down", + "dismiss": "Dismiss" }, "toasts": { "callAvailable": "You are now able to start and receive calls", @@ -206,7 +207,12 @@ "removeSourcesetErrorMessage": "An error occured while trying to delete the sourceset. Please try again.", "removeSourcesetSuccessMessage": "Removed sourceset {sourceset}", "removeSourcesetDialogTitle": "Remove call forward sourceset", - "removeSourcesetDialogText": "You are about to remove the sourceset {sourceset}" + "removeSourcesetDialogText": "You are about to remove the sourceset {sourceset}", + "removeSourceSuccessMessage": "Removed source {source}", + "removeSourceErrorMessage": "An error occured while trying to remove the source. Please try again.", + "removeSourceDialogTitle": "Remove call forward source", + "removeSourceDialogText": "You are about to remove the source entry for {source}", + "removeLastSourceDialogText": "Removing the last source entry is not allowed." } }, "home": { diff --git a/src/store/call-forward.js b/src/store/call-forward.js index d52766e7..142ce672 100644 --- a/src/store/call-forward.js +++ b/src/store/call-forward.js @@ -24,7 +24,8 @@ import { loadDestinations, createSourcesetWithSource, appendSourceToSourceset, - deleteSourcesetById + deleteSourcesetById, + deleteSourceFromSourcesetByIndex } from '../api/call-forward'; const RequestState = { @@ -69,6 +70,9 @@ export default { addSourceState: RequestState.button, addSourceError: null, lastAddedSource: null, + removeSourceState: RequestState.button, + removeSourceError: null, + lastRemovedSource: null, activeForm: '', formType: '', destinationsetId: '', @@ -166,6 +170,16 @@ export default { }, lastRemovedSourceset(state) { return state.lastRemovedSourceset; + }, + removeSourceState(state) { + return state.removeSourceState; + }, + removeSourceError(state) { + return state.removeSourceError || + i18n.t('pages.callForward.sources.removeSourceErrorMessage'); + }, + lastRemovedSource(state) { + return state.lastRemovedSource; } }, mutations: { @@ -378,6 +392,21 @@ export default { }, setLastRemovedSourceset(state, value) { state.lastRemovedSourceset = value; + }, + removeSourceRequesting(state) { + state.removeSourceState = RequestState.requesting; + state.removeSourceError = null; + }, + removeSourceSucceeded(state) { + state.removeSourceState = RequestState.succeeded; + state.removeSourceError = null; + }, + removeSourceFailed(state, error) { + state.removeSourceState = RequestState.failed; + state.removeSourceError = error; + }, + setLastRemovedSource(state, value) { + state.lastRemovedSource = value; } }, actions: { @@ -667,6 +696,15 @@ export default { }).catch((err) => { context.commit('removeSourcesetFailed', err.message); }); + }, + deleteSourceFromSourcesetByIndex(context, options) { + context.commit('removeSourceRequesting'); + deleteSourceFromSourcesetByIndex(options).then(() => { + context.commit('setLastRemovedSource', options.sources[options.sourceIndex].source); + context.commit('removeSourceSucceeded'); + }).catch((err) => { + context.commit('removeSourceFailed', err.message); + }); } } }; diff --git a/t/api/call-forward.js b/t/api/call-forward.js index d910ca22..eb724764 100644 --- a/t/api/call-forward.js +++ b/t/api/call-forward.js @@ -15,7 +15,8 @@ import { deleteTimeFromTimeset, convertAddTime, addNameIdAndTerminating, - createSourcesetWithSource + createSourcesetWithSource, + deleteItemFromArrayByIndex } from '../../src/api/call-forward'; import { assert } from 'chai'; @@ -967,4 +968,25 @@ describe('CallForward', function() { }); + it('should attempt to remove source from sources array', function(){ + + let options = { + array: [ + { source: 1111 }, + { source: 2222 }, + { source: 3333 }, + { source: 4444 } + ], + index: 1 + }; + let result = [ + { source: 1111 }, + { source: 3333 }, + { source: 4444 } + ]; + + assert.deepEqual(deleteItemFromArrayByIndex(options), result); + + }); + });