From 4d422a37f3ca7fdbf8cd4148691ef674cf8aee60 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 27 Feb 2021 13:03:54 +0100 Subject: [PATCH] use async/await in store Signed-off-by: dartcafe --- src/js/store/modules/comments.js | 56 ++++----- src/js/store/modules/options.js | 177 ++++++++++++--------------- src/js/store/modules/poll.js | 120 +++++++++--------- src/js/store/modules/polls.js | 15 ++- src/js/store/modules/pollsAdmin.js | 25 ++-- src/js/store/modules/settings.js | 48 ++++---- src/js/store/modules/share.js | 82 ++++++------- src/js/store/modules/shares.js | 103 +++++++--------- src/js/store/modules/subscription.js | 33 +++-- src/js/store/modules/votes.js | 68 +++++----- 10 files changed, 326 insertions(+), 401 deletions(-) diff --git a/src/js/store/modules/comments.js b/src/js/store/modules/comments.js index 7595ee8e1..f54ab2ce7 100644 --- a/src/js/store/modules/comments.js +++ b/src/js/store/modules/comments.js @@ -62,7 +62,7 @@ const getters = { } const actions = { - list(context) { + async list(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { @@ -76,17 +76,15 @@ const actions = { return } - return axios.get(generateUrl(endPoint + '/comments')) - .then((response) => { - context.commit('set', response.data) - }) - .catch(() => { - context.commit('reset') - }) - + try { + const response = await axios.get(generateUrl(endPoint + '/comments')) + context.commit('set', response.data) + } catch (e) { + context.commit('reset') + } }, - add(context, payload) { + async add(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { @@ -100,35 +98,33 @@ const actions = { return } - return axios.post(generateUrl(endPoint + '/comment'), { - message: payload.message, - }) - .then((response) => { - context.commit('add', { comment: response.data.comment }) - return response.data - }) - .catch((error) => { - console.error('Error writing comment', { error: error.response }, { payload: payload }) - throw error + try { + const response = await axios.post(generateUrl(endPoint + '/comment'), { + message: payload.message, }) + context.commit('add', { comment: response.data.comment }) + return response.data + } catch (e) { + console.error('Error writing comment', { error: e.response }, { payload: payload }) + throw e + } }, - delete(context, payload) { + async delete(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token } - return axios.delete(generateUrl(endPoint + '/comment/' + payload.comment.id)) - .then((response) => { - context.commit('delete', { comment: payload.comment }) - return response.data - }) - .catch((error) => { - console.error('Error deleting comment', { error: error.response }, { payload: payload }) - throw error - }) + try { + const response = await axios.delete(generateUrl(endPoint + '/comment/' + payload.comment.id)) + context.commit('delete', { comment: payload.comment }) + return response.data + } catch (e) { + console.error('Error deleting comment', { error: e.response }, { payload: payload }) + throw e + } }, } diff --git a/src/js/store/modules/options.js b/src/js/store/modules/options.js index 3016a8839..a0327b201 100644 --- a/src/js/store/modules/options.js +++ b/src/js/store/modules/options.js @@ -119,7 +119,7 @@ const getters = { const actions = { - list(context) { + async list(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token @@ -132,127 +132,112 @@ const actions = { return } - return axios.get(generateUrl(endPoint + '/options')) - .then((response) => { - context.commit('set', { options: response.data.options }) - }) - .catch((error) => { - console.error('Error loding options', { error: error.response }, { pollId: context.rootState.route.params.id }) - throw error - }) + try { + const response = await axios.get(generateUrl(endPoint + '/options')) + context.commit('set', { options: response.data.options }) + } catch (e) { + console.error('Error loding options', { error: e.response }, { pollId: context.rootState.route.params.id }) + throw e + } }, - add(context, payload) { + async add(context, payload) { const endPoint = 'apps/polls/option' - return axios.post(generateUrl(endPoint), { - pollId: context.rootState.route.params.id, - timestamp: payload.timestamp, - pollOptionText: payload.pollOptionText, - duration: payload.duration, - }) - .then((response) => { - context.commit('setItem', { option: response.data.option }) - }) - .catch((error) => { - console.error('Error adding option: ' + error.response.data, { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error + try { + const response = await axios.post(generateUrl(endPoint), { + pollId: context.rootState.route.params.id, + timestamp: payload.timestamp, + pollOptionText: payload.pollOptionText, + duration: payload.duration, }) + context.commit('setItem', { option: response.data.option }) + } catch (e) { + console.error('Error adding option: ' + e.response.data, { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - update(context, payload) { + async update(context, payload) { const endPoint = 'apps/polls/option' - return axios.put(generateUrl(endPoint + '/' + payload.option.id), { - timestamp: payload.option.timestamp, - pollOptionText: payload.option.timeStamp, - duration: payload.option.duration, - }) - .then((response) => { - context.commit('setItem', { option: response.data.option }) - }) - .catch((error) => { - console.error('Error updating option', { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error + try { + const response = await axios.put(generateUrl(endPoint + '/' + payload.option.id), { + timestamp: payload.option.timestamp, + pollOptionText: payload.option.timeStamp, + duration: payload.option.duration, }) + context.commit('setItem', { option: response.data.option }) + } catch (e) { + console.error('Error updating option', { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - delete(context, payload) { + async delete(context, payload) { const endPoint = 'apps/polls/option' - - return axios.delete(generateUrl(endPoint + '/' + payload.option.id)) - .then((response) => { - context.commit('delete', { option: payload.option }) - }) - .catch((error) => { - console.error('Error deleting option', { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error - }) + try { + await axios.delete(generateUrl(endPoint + '/' + payload.option.id)) + context.commit('delete', { option: payload.option }) + } catch (e) { + console.error('Error deleting option', { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - confirm(context, payload) { + async confirm(context, payload) { context.commit('confirm', { option: payload.option }) - const endPoint = 'apps/polls/option' - return axios.put(generateUrl(endPoint + '/' + payload.option.id + '/confirm')) - .then((response) => { - context.commit('setItem', { option: response.data.option }) - }) - .catch((error) => { - console.error('Error confirming option', { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error - }) + try { + const response = await axios.put(generateUrl(endPoint + '/' + payload.option.id + '/confirm')) + context.commit('setItem', { option: response.data.option }) + } catch (e) { + console.error('Error confirming option', { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - reorder(context, payload) { - const endPoint = 'apps/polls/poll' + async reorder(context, payload) { context.commit('reorder', { options: payload }) - return axios.post(generateUrl(endPoint + '/' + context.rootState.route.params.id + '/options/reorder'), { - options: payload, - }) - .then((response) => { - context.commit('set', { options: response.data.options }) - }) - .catch((error) => { - console.error('Error reordering option', { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error + const endPoint = 'apps/polls/poll' + try { + const response = await axios.post(generateUrl(endPoint + '/' + context.rootState.route.params.id + '/options/reorder'), { + options: payload, }) + context.commit('set', { options: response.data.options }) + } catch (e) { + console.error('Error reordering option', { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - sequence(context, payload) { + async sequence(context, payload) { const endPoint = 'apps/polls/option' - return axios.post(generateUrl(endPoint + '/' + payload.option.id + '/sequence'), { - step: payload.sequence.step, - unit: payload.sequence.unit.value, - amount: payload.sequence.amount, - }) - .then((response) => { - context.commit('set', { options: response.data.options }) - }) - .catch((error) => { - console.error('Error creating sequence', { error: error.response }, { payload: payload }) - context.dispatch('list') - throw error + try { + const response = await axios.post(generateUrl(endPoint + '/' + payload.option.id + '/sequence'), { + step: payload.sequence.step, + unit: payload.sequence.unit.value, + amount: payload.sequence.amount, }) + context.commit('set', { options: response.data.options }) + } catch (e) { + console.error('Error creating sequence', { error: e.response }, { payload: payload }) + context.dispatch('list') + throw e + } }, - getEvents(context, payload) { + async getEvents(context, payload) { const endPoint = 'apps/polls/option' - return axios.get(generateUrl(endPoint + '/' + payload.option.id + '/events')) - .then((response) => { - return response.data - }) - .catch((error) => { - if (error.message === 'Network Error') { - console.error('Got an ugly network error while loading calendar events', { error: error }, { payload: payload }) - throw error - } - console.error('Error loading calendar events - start whistling and behave as if nothing happened', { error: error }, { payload: payload }) - return { events: [] } - }) + try { + const response = await axios.get(generateUrl(endPoint + '/' + payload.option.id + '/events')) + return response.data + } catch (e) { + return { events: [] } + } }, } diff --git a/src/js/store/modules/poll.js b/src/js/store/modules/poll.js index 87c8902c6..633131ff8 100644 --- a/src/js/store/modules/poll.js +++ b/src/js/store/modules/poll.js @@ -132,7 +132,7 @@ const actions = { context.commit('reset') }, - get(context) { + async get(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { @@ -144,88 +144,78 @@ const actions = { context.commit('acl/reset') return } - return axios.get(generateUrl(endPoint + '/poll')) - .then((response) => { - context.commit('set', response.data) - context.commit('acl/set', response.data) - return response - }) - .catch((error) => { - console.debug('Error loading poll', { error: error.response }) - throw error - }) + try { + const response = await axios.get(generateUrl(endPoint + '/poll')) + context.commit('set', response.data) + context.commit('acl/set', response.data) + return response + } catch (e) { + console.debug('Error loading poll', { error: e.response }) + throw e + } }, - add(context, payload) { + async add(context, payload) { const endPoint = 'apps/polls/poll/add' - return axios.post(generateUrl(endPoint), { title: payload.title, type: payload.type }) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error adding poll:', { error: error.response }, { state: state }) - throw error - }) - + try { + const response = await axios.post(generateUrl(endPoint), { title: payload.title, type: payload.type }) + return response + } catch (e) { + console.error('Error adding poll:', { error: e.response }, { state: state }) + throw e + } }, - clone(context, payload) { + async clone(context, payload) { const endPoint = 'apps/polls/poll' - return axios.get(generateUrl(endPoint + '/' + payload.pollId + '/clone')) - .then((response) => { - return response.data - }) - .catch((error) => { - console.error('Error cloning poll', { error: error.response }, { payload: payload }) - }) - + try { + const response = await axios.get(generateUrl(endPoint + '/' + payload.pollId + '/clone')) + return response.data + } catch (e) { + console.error('Error cloning poll', { error: e.response }, { payload: payload }) + } }, - update(context) { + async update(context) { const endPoint = 'apps/polls/poll' - return axios.put(generateUrl(endPoint + '/' + state.id), { poll: state }) - .then((response) => { - context.commit('set', { poll: response.data }) - return response - }) - .catch((error) => { - console.error('Error updating poll:', { error: error.response }, { poll: state }) - throw error - }) - + try { + const response = await axios.put(generateUrl(endPoint + '/' + state.id), { poll: state }) + context.commit('set', { poll: response.data }) + return response + } catch (e) { + console.error('Error updating poll:', { error: e.response }, { poll: state }) + throw e + } }, - switchDeleted(context, payload) { + async switchDeleted(context, payload) { const endPoint = 'apps/polls/poll' - return axios.put(generateUrl(endPoint + '/' + payload.pollId + '/switchDeleted')) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error switching deleted status', { error: error.response }, { payload: payload }) - }) + try { + const response = await axios.put(generateUrl(endPoint + '/' + payload.pollId + '/switchDeleted')) + return response + } catch (e) { + console.error('Error switching deleted status', { error: e.response }, { payload: payload }) + } }, - delete(context, payload) { + async delete(context, payload) { const endPoint = 'apps/polls/poll' - return axios.delete(generateUrl(endPoint + '/' + payload.pollId)) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error deleting poll', { error: error.response }, { payload: payload }) - }) + try { + const response = await axios.delete(generateUrl(endPoint + '/' + payload.pollId)) + return response + } catch (e) { + console.error('Error deleting poll', { error: e.response }, { payload: payload }) + } }, - getParticipantsEmailAddresses(context, payload) { + async getParticipantsEmailAddresses(context, payload) { const endPoint = 'apps/polls/poll' - return axios.get(generateUrl(endPoint + '/' + payload.pollId + '/addresses')) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error retrieving email addresses', { error: error.response }, { payload: payload }) - }) + try { + const response = await axios.get(generateUrl(endPoint + '/' + payload.pollId + '/addresses')) + return response + } catch (e) { + console.error('Error retrieving email addresses', { error: e.response }, { payload: payload }) + } }, } diff --git a/src/js/store/modules/polls.js b/src/js/store/modules/polls.js index b38f0c2c5..d59f2f4b6 100644 --- a/src/js/store/modules/polls.js +++ b/src/js/store/modules/polls.js @@ -71,16 +71,15 @@ const getters = { } const actions = { - load(context) { + async load(context) { const endPoint = 'apps/polls/polls' - return axios.get(generateUrl(endPoint)) - .then((response) => { - context.commit('set', { list: response.data }) - }) - .catch((error) => { - console.error('Error loading polls', { error: error.response }) - }) + try { + const response = await axios.get(generateUrl(endPoint)) + context.commit('set', { list: response.data }) + } catch (e) { + console.error('Error loading polls', { error: e.response }) + } }, } diff --git a/src/js/store/modules/pollsAdmin.js b/src/js/store/modules/pollsAdmin.js index b3299acfa..62fd81536 100644 --- a/src/js/store/modules/pollsAdmin.js +++ b/src/js/store/modules/pollsAdmin.js @@ -44,29 +44,22 @@ const getters = { } const actions = { - load(context) { + async load(context) { const endPoint = 'apps/polls/administration' if (getCurrentUser().isAdmin) { - return axios.get(generateUrl(endPoint + '/polls')) - .then((response) => { - context.commit('set', { list: response.data }) - }) - .catch((error) => { - console.error('Error loading polls', { error: error.response }) - }) + try { + const response = await axios.get(generateUrl(endPoint + '/polls')) + context.commit('set', { list: response.data }) + } catch (e) { + console.error('Error loading polls', { error: e.response }) + } } }, - takeOver(context, payload) { + async takeOver(context, payload) { const endPoint = 'apps/polls/administration' if (getCurrentUser().isAdmin) { - return axios.get(generateUrl(endPoint + '/poll/' + payload.pollId + '/takeover')) - .then((response) => { - return response - }) - .catch((error) => { - throw error - }) + return await axios.get(generateUrl(endPoint + '/poll/' + payload.pollId + '/takeover')) } }, } diff --git a/src/js/store/modules/settings.js b/src/js/store/modules/settings.js index 5b4ba9209..dbdc862de 100644 --- a/src/js/store/modules/settings.js +++ b/src/js/store/modules/settings.js @@ -67,39 +67,33 @@ const mutations = { } const actions = { - get(context) { + async get(context) { const endPoint = 'apps/polls/preferences/get' - - return axios.get(generateUrl(endPoint)) - .then((response) => { - console.debug('settings loaded ') - context.commit('setPreference', response.data.preferences) - }) - .catch(() => { - context.commit('reset') - }) + try { + const response = await axios.get(generateUrl(endPoint)) + console.debug('settings loaded ') + context.commit('setPreference', response.data.preferences) + } catch (e) { + context.commit('reset') + } }, - write(context) { + + async write(context) { const endPoint = 'apps/polls/preferences/write' - - return axios.post(generateUrl(endPoint), { settings: context.state.user }) - .then((response) => { - context.commit('setPreference', response.data.preferences) - }) - .catch((error) => { - console.error('Error writing preferences', { error: error.response }, { preferences: state.user }) - throw error - }) + try { + const response = await axios.post(generateUrl(endPoint), { settings: context.state.user }) + context.commit('setPreference', response.data.preferences) + } catch (e) { + console.error('Error writing preferences', { error: e.response }, { preferences: state.user }) + throw e + } }, - getCalendars(context) { + async getCalendars(context) { const endPoint = 'apps/polls/calendars' - - return axios.get(generateUrl(endPoint)) - .then((response) => { - context.commit('setCalendars', { calendars: response.data.calendars }) - return response - }) + const response = await axios.get(generateUrl(endPoint)) + context.commit('setCalendars', { calendars: response.data.calendars }) + return response }, } diff --git a/src/js/store/modules/share.js b/src/js/store/modules/share.js index afca341d2..359a64ab0 100644 --- a/src/js/store/modules/share.js +++ b/src/js/store/modules/share.js @@ -56,7 +56,7 @@ const mutations = { } const actions = { - get(context) { + async get(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token @@ -64,77 +64,67 @@ const actions = { context.commit('reset') return } - return axios.get(generateUrl(endPoint + '/share')) - .then((response) => { - context.commit('set', { share: response.data.share }) - return response.data - }) - .catch((error) => { - console.debug('Error retrieving share', { error: error.response }) - throw error.response - }) + try { + const response = await axios.get(generateUrl(endPoint + '/share')) + context.commit('set', { share: response.data.share }) + return response.data + } catch (e) { + console.debug('Error retrieving share', { error: e.response }) + throw e.response + } }, - register(context, payload) { + async register(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token } else { return } - - return axios.post(generateUrl(endPoint + '/register'), { - userName: payload.userName, - emailAddress: payload.emailAddress, - }) - .then((response) => { - return { token: response.data.share.token } + try { + const response = await axios.post(generateUrl(endPoint + '/register'), { + userName: payload.userName, + emailAddress: payload.emailAddress, }) - .catch((error) => { - console.error('Error writing personal share', { error: error.response }, { payload: payload }) - throw error - }) - + return { token: response.data.share.token } + } catch (e) { + console.error('Error writing personal share', { error: e.response }, { payload: payload }) + throw e + } }, - updateEmailAddress(context, payload) { + async updateEmailAddress(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token } else { return } - - return axios.put(generateUrl(endPoint + '/email'), { - emailAddress: payload.emailAddress, - }) - .then((response) => { - context.commit('set', { share: response.data.share }) - return response.data + try { + const response = await axios.put(generateUrl(endPoint + '/email'), { + emailAddress: payload.emailAddress, }) - .catch((error) => { - console.error('Error writing email address', { error: error.response }, { payload: payload }) - throw error - }) - + context.commit('set', { share: response.data.share }) + return response.data + } catch (e) { + console.error('Error writing email address', { error: e.response }, { payload: payload }) + throw e + } }, - resendInvitation(context, payload) { + async resendInvitation(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token } else { return } - - return axios.get(generateUrl(endPoint + '/resend')) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error sending invitation', { error: error.response }, { payload: payload }) - throw error - }) + try { + return await axios.get(generateUrl(endPoint + '/resend')) + } catch (e) { + console.error('Error sending invitation', { error: e.response }, { payload: payload }) + throw e + } }, } diff --git a/src/js/store/modules/shares.js b/src/js/store/modules/shares.js index c20028270..cf85a9847 100644 --- a/src/js/store/modules/shares.js +++ b/src/js/store/modules/shares.js @@ -87,7 +87,7 @@ const getters = { } const actions = { - list(context) { + async list(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'vote') { @@ -98,77 +98,64 @@ const actions = { context.commit('reset') return } - return axios.get(generateUrl(endPoint + '/shares')) - .then((response) => { - context.commit('set', response.data) - return response.data - }) - .catch((error) => { - console.error('Error loading shares', { error: error.response }, { pollId: context.rootState.route.params.id }) - throw error - }) + try { + const response = await axios.get(generateUrl(endPoint + '/shares')) + context.commit('set', response.data) + return response.data + } catch (e) { + console.error('Error loading shares', { error: e.response }, { pollId: context.rootState.route.params.id }) + throw e + } }, - add(context, payload) { + async add(context, payload) { const endPoint = 'apps/polls/poll/' + context.rootState.route.params.id - - return axios.post(generateUrl(endPoint + '/share'), payload.share) - .then((response) => { - return response.data - }) - .catch((error) => { - console.error('Error writing share', { error: error.response }, { payload: payload }) - throw error - }) - .finally(() => { - context.dispatch('list') - }) + try { + await axios.post(generateUrl(endPoint + '/share'), payload.share) + } catch (e) { + console.error('Error writing share', { error: e.response }, { payload: payload }) + throw e + } finally { + context.dispatch('list') + } }, - delete(context, payload) { + async delete(context, payload) { const endPoint = 'apps/polls/share' context.commit('delete', { share: payload.share }) - return axios.delete(generateUrl(endPoint + '/' + payload.share.token)) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error removing share', { error: error.response }, { payload: payload }) - throw error - }) - .finally(() => { - context.dispatch('list') - }) + try { + return await axios.delete(generateUrl(endPoint + '/' + payload.share.token)) + } catch (e) { + console.error('Error removing share', { error: e.response }, { payload: payload }) + throw e + } finally { + context.dispatch('list') + } }, - sendInvitation(context, payload) { + async sendInvitation(context, payload) { const endPoint = 'apps/polls/share' - - return axios.post(generateUrl(endPoint + '/' + payload.share.token + '/invite')) - .then((response) => { - return response - }) - .catch((error) => { - console.error('Error sending invitation', { error: error.response }, { payload: payload }) - throw error - }) - .finally(() => { - context.dispatch('list') - }) + try { + return await axios.post(generateUrl(endPoint + '/' + payload.share.token + '/invite')) + } catch (e) { + console.error('Error sending invitation', { error: e.response }, { payload: payload }) + throw e + } finally { + context.dispatch('list') + } }, - resolveGroup(context, payload) { + async resolveGroup(context, payload) { const endPoint = 'apps/polls/share' - - return axios.get(generateUrl(endPoint + '/' + payload.share.token + '/resolve')) - .catch((error) => { - console.error('Error exploding group', error.response.data, { error: error.response }, { payload: payload }) - throw error - }) - .finally(() => { - context.dispatch('list') - }) + try { + return await axios.get(generateUrl(endPoint + '/' + payload.share.token + '/resolve')) + } catch (e) { + console.error('Error exploding group', e.response.data, { error: e.response }, { payload: payload }) + throw e + } finally { + context.dispatch('list') + } }, } diff --git a/src/js/store/modules/subscription.js b/src/js/store/modules/subscription.js index d4708db0e..73810e9d2 100644 --- a/src/js/store/modules/subscription.js +++ b/src/js/store/modules/subscription.js @@ -44,7 +44,7 @@ const mutations = { const actions = { - get(context) { + async get(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { @@ -54,34 +54,29 @@ const actions = { } else { return } - - return axios.get(generateUrl(endPoint + '/subscription')) - .then((response) => { - context.commit('set', response.data) - }) - .catch(() => { - context.commit('set', false) - }) + try { + const response = await axios.get(generateUrl(endPoint + '/subscription')) + context.commit('set', response.data) + } catch (e) { + context.commit('set', false) + } }, - update(context, payload) { + async update(context, payload) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.route.params.token } else if (context.rootState.route.name === 'vote') { endPoint = endPoint + '/poll/' + context.rootState.route.params.id - } else { - return } - return axios.put(generateUrl(endPoint + (payload ? '/subscribe' : '/unsubscribe'))) - .then((response) => { - context.commit('set', response.data) - }) - .catch((error) => { - console.error(error.response) - }) + try { + const response = await axios.put(generateUrl(endPoint + (payload ? '/subscribe' : '/unsubscribe'))) + context.commit('set', response.data) + } catch (e) { + console.error(e.response) + } }, } diff --git a/src/js/store/modules/votes.js b/src/js/store/modules/votes.js index 1c2aba9c6..f7b01d9dc 100644 --- a/src/js/store/modules/votes.js +++ b/src/js/store/modules/votes.js @@ -117,7 +117,7 @@ const getters = { } const actions = { - list(context) { + async list(context) { let endPoint = 'apps/polls' if (context.rootState.route.name === 'publicVote') { @@ -128,51 +128,47 @@ const actions = { context.commit('reset') return } - - return axios.get(generateUrl(endPoint + '/votes')) - .then((response) => { - context.commit('set', response.data) - }) - .catch(() => { - context.commit('reset') - }) - + try { + const response = await axios.get(generateUrl(endPoint + '/votes')) + context.commit('set', response.data) + } catch (e) { + context.commit('reset') + } }, - set(context, payload) { + + async set(context, payload) { let endPoint = 'apps/polls' - if (context.rootState.poll.acl.token) { + if (context.rootState.route.name === 'publicVote') { endPoint = endPoint + '/s/' + context.rootState.poll.acl.token } - return axios.put(generateUrl(endPoint + '/vote'), { - optionId: payload.option.id, - setTo: payload.setTo, - }) - .then((response) => { - context.commit('setItem', { option: payload.option, pollId: context.rootState.poll.id, vote: response.data.vote }) - return response.data - }) - .catch((error) => { - if (error.response.status === 409) { - context.dispatch('list') - } else { - console.error('Error setting vote', { error: error.response }, { payload: payload }) - throw error - } + try { + const response = await axios.put(generateUrl(endPoint + '/vote'), { + optionId: payload.option.id, + setTo: payload.setTo, }) + context.commit('setItem', { option: payload.option, pollId: context.rootState.poll.id, vote: response.data.vote }) + return response.data + } catch (e) { + if (e.response.status === 409) { + context.dispatch('list') + } else { + console.error('Error setting vote', { error: e.response }, { payload: payload }) + throw e + } + } }, - deleteUser(context, payload) { + async deleteUser(context, payload) { const endPoint = 'apps/polls/poll/' + context.rootState.route.params.id + '/user/' + payload.userId - return axios.delete(generateUrl(endPoint)) - .then(() => { - context.commit('deleteVotes', payload) - }) - .catch((error) => { - console.error('Error deleting votes', { error: error.response }, { payload: payload }) - throw error - }) + try { + await axios.delete(generateUrl(endPoint)) + context.commit('deleteVotes', payload) + } catch (e) { + console.error('Error deleting votes', { error: e.response }, { payload: payload }) + throw e + } }, }