1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
import Vue from 'vue';
import setConfigs from '@gitlab/ui/dist/config';
import Translate from '~/vue_shared/translate';
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
import { addSubscription, removeSubscription, getLocation } from '~/jira_connect/api';
import JiraConnectApp from './components/app.vue';
import createStore from './store';
import { SET_ERROR_MESSAGE } from './store/mutation_types';
const store = createStore();
const reqComplete = () => {
AP.navigator.reload();
};
const reqFailed = (res, fallbackErrorMessage) => {
const { error = fallbackErrorMessage } = res || {};
store.commit(SET_ERROR_MESSAGE, error);
};
const updateSignInLinks = async () => {
const location = await getLocation();
Array.from(document.querySelectorAll('.js-jira-connect-sign-in')).forEach((el) => {
const updatedLink = `${el.getAttribute('href')}?return_to=${location}`;
el.setAttribute('href', updatedLink);
});
};
const initRemoveSubscriptionButtonHandlers = () => {
Array.from(document.querySelectorAll('.js-jira-connect-remove-subscription')).forEach((el) => {
el.addEventListener('click', function onRemoveSubscriptionClick(e) {
e.preventDefault();
const removePath = e.target.getAttribute('href');
removeSubscription(removePath)
.then(reqComplete)
.catch((err) =>
reqFailed(err.response.data, 'Failed to remove namespace. Please try again.'),
);
});
});
};
const initAddSubscriptionFormHandler = () => {
const formEl = document.querySelector('#add-subscription-form');
if (!formEl) {
return;
}
formEl.addEventListener('submit', function onAddSubscriptionForm(e) {
e.preventDefault();
const addPath = e.target.getAttribute('action');
const namespace = (e.target.querySelector('#namespace-input') || {}).value;
addSubscription(addPath, namespace)
.then(reqComplete)
.catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.'));
});
};
export async function initJiraConnect() {
initAddSubscriptionFormHandler();
initRemoveSubscriptionButtonHandlers();
await updateSignInLinks();
const el = document.querySelector('.js-jira-connect-app');
if (!el) {
return null;
}
setConfigs();
Vue.use(Translate);
Vue.use(GlFeatureFlagsPlugin);
const { groupsPath, subscriptionsPath, usersPath } = el.dataset;
return new Vue({
el,
store,
provide: {
groupsPath,
subscriptionsPath,
usersPath,
},
render(createElement) {
return createElement(JiraConnectApp);
},
});
}
document.addEventListener('DOMContentLoaded', initJiraConnect);
|