debian-mirror-gitlab/app/assets/javascripts/jira_connect/index.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import setConfigs from '@gitlab/ui/dist/config';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
import { addSubscription, removeSubscription, getLocation } from '~/jira_connect/api';
2021-03-08 18:12:59 +05:30
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
2021-03-11 19:13:27 +05:30
import Translate from '~/vue_shared/translate';
2021-03-08 18:12:59 +05:30
import JiraConnectApp from './components/app.vue';
import createStore from './store';
2021-04-17 20:07:23 +05:30
import { SET_ALERT } from './store/mutation_types';
2021-03-08 18:12:59 +05:30
const store = createStore();
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
const reqComplete = () => {
AP.navigator.reload();
};
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
const reqFailed = (res, fallbackErrorMessage) => {
const { error = fallbackErrorMessage } = res || {};
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
store.commit(SET_ALERT, { message: error, variant: 'danger' });
2021-03-11 19:13:27 +05:30
};
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
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);
2021-02-22 17:27:13 +05:30
});
2021-03-11 19:13:27 +05:30
};
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;
}
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
formEl.addEventListener('submit', function onAddSubscriptionForm(e) {
2021-02-22 17:27:13 +05:30
e.preventDefault();
2021-03-11 19:13:27 +05:30
const addPath = e.target.getAttribute('action');
const namespace = (e.target.querySelector('#namespace-input') || {}).value;
addSubscription(addPath, namespace)
2021-03-08 18:12:59 +05:30
.then(reqComplete)
2021-03-11 19:13:27 +05:30
.catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.'));
2021-02-22 17:27:13 +05:30
});
};
2021-03-11 19:13:27 +05:30
export async function initJiraConnect() {
initAddSubscriptionFormHandler();
initRemoveSubscriptionButtonHandlers();
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
await updateSignInLinks();
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
const el = document.querySelector('.js-jira-connect-app');
2021-03-08 18:12:59 +05:30
if (!el) {
return null;
}
setConfigs();
Vue.use(Translate);
Vue.use(GlFeatureFlagsPlugin);
2021-03-11 19:13:27 +05:30
const { groupsPath, subscriptionsPath, usersPath } = el.dataset;
2021-04-17 20:07:23 +05:30
AP.sizeToParent();
2021-03-08 18:12:59 +05:30
2021-01-29 00:20:46 +05:30
return new Vue({
el,
2021-03-08 18:12:59 +05:30
store,
provide: {
groupsPath,
2021-03-11 19:13:27 +05:30
subscriptionsPath,
usersPath,
2021-02-22 17:27:13 +05:30
},
2021-01-29 00:20:46 +05:30
render(createElement) {
2021-03-08 18:12:59 +05:30
return createElement(JiraConnectApp);
2021-01-29 00:20:46 +05:30
},
});
}
document.addEventListener('DOMContentLoaded', initJiraConnect);