diff options
Diffstat (limited to 'app/assets/javascripts/jira_connect/subscriptions/store')
5 files changed, 148 insertions, 8 deletions
diff --git a/app/assets/javascripts/jira_connect/subscriptions/store/actions.js b/app/assets/javascripts/jira_connect/subscriptions/store/actions.js new file mode 100644 index 00000000000..4a83ee8671d --- /dev/null +++ b/app/assets/javascripts/jira_connect/subscriptions/store/actions.js @@ -0,0 +1,73 @@ +import { fetchSubscriptions as fetchSubscriptionsREST } from '~/jira_connect/subscriptions/api'; +import { getCurrentUser } from '~/rest_api'; +import { addJiraConnectSubscription } from '~/api/integrations_api'; +import { + I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE, + I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE, + INTEGRATIONS_DOC_LINK, + I18N_DEFAULT_SUBSCRIPTIONS_ERROR_MESSAGE, +} from '../constants'; +import { getJwt } from '../utils'; +import { + SET_SUBSCRIPTIONS, + SET_SUBSCRIPTIONS_LOADING, + SET_SUBSCRIPTIONS_ERROR, + ADD_SUBSCRIPTION_LOADING, + ADD_SUBSCRIPTION_ERROR, + SET_ALERT, + SET_CURRENT_USER, + SET_CURRENT_USER_ERROR, +} from './mutation_types'; + +export const fetchSubscriptions = async ({ commit }, subscriptionsPath) => { + commit(SET_SUBSCRIPTIONS_LOADING, true); + + try { + const data = await fetchSubscriptionsREST(subscriptionsPath); + commit(SET_SUBSCRIPTIONS, data.data.subscriptions); + } catch { + commit(SET_SUBSCRIPTIONS_ERROR, true); + commit(SET_ALERT, { message: I18N_DEFAULT_SUBSCRIPTIONS_ERROR_MESSAGE, variant: 'danger' }); + } finally { + commit(SET_SUBSCRIPTIONS_LOADING, false); + } +}; + +export const loadCurrentUser = async ({ commit }, accessToken) => { + try { + const { data: user } = await getCurrentUser({ + headers: { Authorization: `Bearer ${accessToken}` }, + }); + + commit(SET_CURRENT_USER, user); + } catch (e) { + commit(SET_CURRENT_USER_ERROR, e); + } +}; + +export const addSubscription = async ( + { commit, state, dispatch }, + { namespacePath, subscriptionsPath }, +) => { + try { + commit(ADD_SUBSCRIPTION_LOADING, true); + + await addJiraConnectSubscription(namespacePath, { + jwt: await getJwt(), + accessToken: state.accessToken, + }); + + commit(SET_ALERT, { + title: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE, + message: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE, + linkUrl: INTEGRATIONS_DOC_LINK, + variant: 'success', + }); + + dispatch('fetchSubscriptions', subscriptionsPath); + } catch (e) { + commit(ADD_SUBSCRIPTION_ERROR, e); + } finally { + commit(ADD_SUBSCRIPTION_LOADING, false); + } +}; diff --git a/app/assets/javascripts/jira_connect/subscriptions/store/index.js b/app/assets/javascripts/jira_connect/subscriptions/store/index.js index de830e3891a..abad1920bcc 100644 --- a/app/assets/javascripts/jira_connect/subscriptions/store/index.js +++ b/app/assets/javascripts/jira_connect/subscriptions/store/index.js @@ -1,12 +1,15 @@ import Vue from 'vue'; import Vuex from 'vuex'; +import * as actions from './actions'; import mutations from './mutations'; -import state from './state'; +import createState from './state'; Vue.use(Vuex); -export default () => - new Vuex.Store({ +export default function createStore(initialState) { + return new Vuex.Store({ mutations, - state, + actions, + state: createState(initialState), }); +} diff --git a/app/assets/javascripts/jira_connect/subscriptions/store/mutation_types.js b/app/assets/javascripts/jira_connect/subscriptions/store/mutation_types.js index 15f36b824d9..d4893fbcaf6 100644 --- a/app/assets/javascripts/jira_connect/subscriptions/store/mutation_types.js +++ b/app/assets/javascripts/jira_connect/subscriptions/store/mutation_types.js @@ -1 +1,13 @@ export const SET_ALERT = 'SET_ALERT'; + +export const SET_SUBSCRIPTIONS = 'SET_SUBSCRIPTIONS'; +export const SET_SUBSCRIPTIONS_LOADING = 'SET_SUBSCRIPTIONS_LOADING'; +export const SET_SUBSCRIPTIONS_ERROR = 'SET_SUBSCRIPTIONS_ERROR'; + +export const ADD_SUBSCRIPTION_LOADING = 'ADD_SUBSCRIPTION_LOADING'; +export const ADD_SUBSCRIPTION_ERROR = 'ADD_SUBSCRIPTION_ERROR'; + +export const SET_CURRENT_USER = 'SET_CURRENT_USER'; +export const SET_CURRENT_USER_ERROR = 'SET_CURRENT_USER_ERROR'; + +export const SET_ACCESS_TOKEN = 'SET_ACCESS_TOKEN'; diff --git a/app/assets/javascripts/jira_connect/subscriptions/store/mutations.js b/app/assets/javascripts/jira_connect/subscriptions/store/mutations.js index 2a25e0fe25f..60076c918fd 100644 --- a/app/assets/javascripts/jira_connect/subscriptions/store/mutations.js +++ b/app/assets/javascripts/jira_connect/subscriptions/store/mutations.js @@ -1,7 +1,45 @@ -import { SET_ALERT } from './mutation_types'; +import { + SET_ALERT, + SET_SUBSCRIPTIONS, + SET_SUBSCRIPTIONS_LOADING, + SET_SUBSCRIPTIONS_ERROR, + ADD_SUBSCRIPTION_LOADING, + ADD_SUBSCRIPTION_ERROR, + SET_CURRENT_USER, + SET_CURRENT_USER_ERROR, + SET_ACCESS_TOKEN, +} from './mutation_types'; export default { [SET_ALERT](state, { title, message, variant, linkUrl } = {}) { state.alert = { title, message, variant, linkUrl }; }, + + [SET_SUBSCRIPTIONS](state, subscriptions = []) { + state.subscriptions = subscriptions; + }, + [SET_SUBSCRIPTIONS_LOADING](state, subscriptionsLoading) { + state.subscriptionsLoading = subscriptionsLoading; + }, + [SET_SUBSCRIPTIONS_ERROR](state, subscriptionsError) { + state.subscriptionsError = subscriptionsError; + }, + + [ADD_SUBSCRIPTION_LOADING](state, loading) { + state.addSubscriptionLoading = loading; + }, + [ADD_SUBSCRIPTION_ERROR](state, error) { + state.addSubscriptionError = error; + }, + + [SET_CURRENT_USER](state, currentUser) { + state.currentUser = currentUser; + }, + [SET_CURRENT_USER_ERROR](state, currentUserError) { + state.currentUserError = currentUserError; + }, + + [SET_ACCESS_TOKEN](state, accessToken) { + state.accessToken = accessToken; + }, }; diff --git a/app/assets/javascripts/jira_connect/subscriptions/store/state.js b/app/assets/javascripts/jira_connect/subscriptions/store/state.js index c807df03f00..03a83f18b4c 100644 --- a/app/assets/javascripts/jira_connect/subscriptions/store/state.js +++ b/app/assets/javascripts/jira_connect/subscriptions/store/state.js @@ -1,3 +1,17 @@ -export default () => ({ - alert: undefined, -}); +export default function createState({ subscriptions = [], subscriptionsLoading = false } = {}) { + return { + alert: undefined, + + subscriptions, + subscriptionsLoading, + subscriptionsError: false, + + addSubscriptionLoading: false, + addSubscriptionError: false, + + currentUser: null, + currentUserError: null, + + accessToken: null, + }; +} |