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

91 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-01-29 00:20:46 +05:30
import Vue from 'vue';
2021-03-08 18:12:59 +05:30
import Vuex from 'vuex';
2021-02-22 17:27:13 +05:30
import $ from 'jquery';
2021-03-08 18:12:59 +05:30
import setConfigs from '@gitlab/ui/dist/config';
import Translate from '~/vue_shared/translate';
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
import JiraConnectApp from './components/app.vue';
import { addSubscription, removeSubscription } from '~/jira_connect/api';
import createStore from './store';
import { SET_ERROR_MESSAGE } from './store/mutation_types';
Vue.use(Vuex);
const store = createStore();
2021-02-22 17:27:13 +05:30
/**
2021-03-08 18:12:59 +05:30
* Initialize form handlers for the Jira Connect app
2021-02-22 17:27:13 +05:30
*/
const initJiraFormHandlers = () => {
const reqComplete = () => {
AP.navigator.reload();
};
const reqFailed = (res, fallbackErrorMessage) => {
2021-03-08 18:12:59 +05:30
const { error = fallbackErrorMessage } = res || {};
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
store.commit(SET_ERROR_MESSAGE, error);
2021-02-22 17:27:13 +05:30
};
2021-03-08 18:12:59 +05:30
if (typeof AP.getLocation === 'function') {
AP.getLocation((location) => {
$('.js-jira-connect-sign-in').each(function updateSignInLink() {
const updatedLink = `${$(this).attr('href')}?return_to=${location}`;
$(this).attr('href', updatedLink);
});
2021-02-22 17:27:13 +05:30
});
2021-03-08 18:12:59 +05:30
}
2021-02-22 17:27:13 +05:30
$('#add-subscription-form').on('submit', function onAddSubscriptionForm(e) {
2021-03-08 18:12:59 +05:30
const addPath = $(this).attr('action');
const namespace = $('#namespace-input').val();
2021-02-22 17:27:13 +05:30
e.preventDefault();
2021-03-08 18:12:59 +05:30
addSubscription(addPath, namespace)
.then(reqComplete)
.catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.'));
2021-02-22 17:27:13 +05:30
});
$('.remove-subscription').on('click', function onRemoveSubscriptionClick(e) {
2021-03-08 18:12:59 +05:30
const removePath = $(this).attr('href');
2021-02-22 17:27:13 +05:30
e.preventDefault();
2021-03-08 18:12:59 +05:30
removeSubscription(removePath)
.then(reqComplete)
.catch((err) =>
reqFailed(err.response.data, 'Failed to remove namespace. Please try again.'),
);
2021-02-22 17:27:13 +05:30
});
};
2021-01-29 00:20:46 +05:30
function initJiraConnect() {
const el = document.querySelector('.js-jira-connect-app');
2021-02-22 17:27:13 +05:30
initJiraFormHandlers();
2021-03-08 18:12:59 +05:30
if (!el) {
return null;
}
setConfigs();
Vue.use(Translate);
Vue.use(GlFeatureFlagsPlugin);
const { groupsPath } = el.dataset;
2021-01-29 00:20:46 +05:30
return new Vue({
el,
2021-03-08 18:12:59 +05:30
store,
provide: {
groupsPath,
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);