From b2a48bb656f0e4dc9bd698ca7c55352833a762ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Fri, 26 Oct 2018 22:54:37 +0200 Subject: [PATCH] Divide store into modules --- .../Private/Assets/js/myep/_store.js | 131 ++---------------- .../Assets/js/myep/components/Address.vue | 4 +- .../Assets/js/myep/components/CrmData.vue | 4 +- .../Assets/js/myep/components/Events.vue | 2 +- .../Assets/js/myep/components/Teamer.vue | 4 +- .../Private/Assets/js/myep/stores/address.js | 49 +++++++ .../Private/Assets/js/myep/stores/crmdata.js | 48 +++++++ .../Private/Assets/js/myep/stores/events.js | 29 ++++ .../Private/Assets/js/myep/stores/teamer.js | 48 +++++++ 9 files changed, 193 insertions(+), 126 deletions(-) create mode 100644 web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/address.js create mode 100644 web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/crmdata.js create mode 100644 web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/events.js create mode 100644 web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/teamer.js diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/_store.js b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/_store.js index bb80dd97..c64484bf 100644 --- a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/_store.js +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/_store.js @@ -1,24 +1,26 @@ import Vue from 'vue' import Vuex from 'vuex' import axios from 'axios' -import EventBus from '../_bus' + +import AddressStore from './stores/address' +import CrmDataStore from './stores/crmdata' +import TeamerStore from './stores/teamer' +import EventsStore from './stores/events' const configElement = document.getElementById('appconfig'); const config = JSON.parse(configElement.innerHTML); axios.defaults.baseURL = config.myEpApiBaseUrl; -const createAuthenticatingClient = () => { - return axios.create({ - headers: { - 'X-Auth-Token': sessionStorage.getItem('token') - }, - }); -}; - Vue.use(Vuex); export default new Vuex.Store({ + modules: { + address: AddressStore, + crmdata: CrmDataStore, + teamer: TeamerStore, + events: EventsStore + }, state: { config: config, loading: false, @@ -38,6 +40,7 @@ export default new Vuex.Store({ }, logout (state) { state.loggedIn = false; + state.teamer = false; }, isTeamer (state) { state.teamer = true; @@ -133,116 +136,6 @@ export default new Vuex.Store({ }).then((response) => { commit('setJobProfiles', response.data['hydra:member']); }); - }, - loadAddress ({ commit }) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.get('/api/addresses') - .then((response) => { - commit('loadingFinish'); - return response.data['hydra:member'][0]; - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - saveAddress ({ commit }, data) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.put('/api/addresses/' + data.id, data) - .then(() => { - commit('loadingFinish'); - }) - .catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - loadEvents ({ commit }) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.get('/api/events') - .then((response) => { - commit('loadingFinish'); - return response.data['hydra:member']; - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - loadCrmData ({ commit }) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.get('/api/crm-selections') - .then((response) => { - commit('loadingFinish'); - return response.data['hydra:member'][0]; - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - saveCrmData ({ commit }, data) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.put('/api/crm-selections/' + data.id, data) - .then(() => { - commit('loadingFinish'); - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - loadTeamerData ({ commit }) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.get('/api/teamers/' + sessionStorage.getItem('teamerId')) - .then((response) => { - commit('loadingFinish'); - return response.data; - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); - }, - saveTeamerData ({ commit }, data) { - commit('loadingBegin'); - - const client = createAuthenticatingClient(); - - return client.put('/api/teamers/' + sessionStorage.getItem('teamerId'), data) - .then(() => { - commit('loadingFinish'); - }).catch((error) => { - commit('loadingFinish'); - commit('logout'); - EventBus.$emit('forcedLogout'); - throw error; - }); } }, getters: { diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Address.vue b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Address.vue index 46c1e14f..7ebf32e2 100644 --- a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Address.vue +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Address.vue @@ -137,7 +137,7 @@ }, methods: { fetchAddress () { - this.$store.dispatch('loadAddress') + this.$store.dispatch('address/load') .then((data) => { this.address = data; this.picker.setDate(new Date(this.address.dateOfBirth), true); @@ -150,7 +150,7 @@ }); }, saveAddress () { - this.$store.dispatch('saveAddress', this.address) + this.$store.dispatch('address/save', this.address) .then(() => { this.message = 'Die Änderungen wurden gespeichert'; this.messageClass = 'alert-success'; diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/CrmData.vue b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/CrmData.vue index 5f2b1e9c..b77d1948 100644 --- a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/CrmData.vue +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/CrmData.vue @@ -58,7 +58,7 @@ }, methods: { fetchSelections () { - this.$store.dispatch('loadCrmData') + this.$store.dispatch('crmdata/load') .then((data) => { this.crmData = data; }).catch(() => { @@ -70,7 +70,7 @@ }); }, saveSelections () { - this.$store.dispatch('saveCrmData', this.crmData) + this.$store.dispatch('crmdata/save', this.crmData) .then(() => { this.message = 'Die Änderungen wurden gespeichert'; this.messageClass = 'alert-success'; diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Events.vue b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Events.vue index 01c59593..c919e0a6 100644 --- a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Events.vue +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Events.vue @@ -65,7 +65,7 @@ }, methods: { fetchEvents () { - this.$store.dispatch('loadEvents') + this.$store.dispatch('events/load') .then((events) => { this.events = events; }).catch(() => { diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Teamer.vue b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Teamer.vue index c17f027e..02e19400 100644 --- a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Teamer.vue +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/components/Teamer.vue @@ -121,14 +121,14 @@ }, methods: { fetchTeamerData () { - this.$store.dispatch('loadTeamerData') + this.$store.dispatch('teamer/load') .then((data) => { this.teamerData = data; this.loaded = true; }) }, saveTeamerData () { - this.$store.dispatch('saveTeamerData', this.teamerData) + this.$store.dispatch('teamer/save', this.teamerData) .then(() => { this.message = 'Die Änderungen wurden gespeichert'; this.messageClass = 'alert-success'; diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/address.js b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/address.js new file mode 100644 index 00000000..9356f457 --- /dev/null +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/address.js @@ -0,0 +1,49 @@ +import EventBus from '../../_bus'; +import axios from 'axios'; + +export default { + namespaced: true, + state: {}, + actions: { + load ({ commit }) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.get('/api/addresses') + .then((response) => { + commit('loadingFinish', null, { root: true }); + return response.data['hydra:member'][0]; + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + }, + save ({ commit }, data) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.put('/api/addresses/' + data.id, data) + .then(() => { + commit('loadingFinish', null, { root: true }); + }) + .catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + } + } +} diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/crmdata.js b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/crmdata.js new file mode 100644 index 00000000..78ecaa50 --- /dev/null +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/crmdata.js @@ -0,0 +1,48 @@ +import EventBus from '../../_bus'; +import axios from 'axios'; + +export default { + namespaced: true, + state: {}, + actions: { + load ({ commit }) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.get('/api/crm-selections') + .then((response) => { + commit('loadingFinish', null, { root: true }); + return response.data['hydra:member'][0]; + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + }, + save ({ commit }, data) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.put('/api/crm-selections/' + data.id, data) + .then(() => { + commit('loadingFinish', null, { root: true }); + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + } + } +} diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/events.js b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/events.js new file mode 100644 index 00000000..eadf7b4b --- /dev/null +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/events.js @@ -0,0 +1,29 @@ +import EventBus from '../../_bus'; +import axios from 'axios'; + +export default { + namespaced: true, + state: {}, + actions: { + load ({ commit }) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.get('/api/events') + .then((response) => { + commit('loadingFinish', null, { root: true }); + return response.data['hydra:member']; + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + } + } +} diff --git a/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/teamer.js b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/teamer.js new file mode 100644 index 00000000..66318ced --- /dev/null +++ b/web/typo3conf/ext/ep_theme/Resources/Private/Assets/js/myep/stores/teamer.js @@ -0,0 +1,48 @@ +import EventBus from '../../_bus'; +import axios from 'axios'; + +export default { + namespaced: true, + state: {}, + actions: { + load ({ commit }) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.get('/api/teamers/' + sessionStorage.getItem('teamerId')) + .then((response) => { + commit('loadingFinish', null, { root: true }); + return response.data; + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + }, + save ({ commit }, data) { + commit('loadingBegin', null, { root: true }); + + const client = axios.create({ + headers: { + 'X-Auth-Token': sessionStorage.getItem('token') + } + }); + + return client.put('/api/teamers/' + sessionStorage.getItem('teamerId'), data) + .then(() => { + commit('loadingFinish', null, { root: true }); + }).catch((error) => { + commit('loadingFinish', null, { root: true }); + commit('logout', null, { root: true }); + EventBus.$emit('forcedLogout'); + throw error; + }); + } + } +}