Commit 5e545e73 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch...

Merge branch '289973-refactor-jquery-to-vanilla-js-app-assets-javascripts-jira_connect-index-js' into 'master'

Refactor jQuery to Vanilla JS: app/assets/javascripts/jira_connect/index.js

See merge request gitlab-org/gitlab!52286
parents a9f0580a 8a2b745d
...@@ -10,6 +10,10 @@ export const getJwt = () => { ...@@ -10,6 +10,10 @@ export const getJwt = () => {
export const getLocation = () => { export const getLocation = () => {
return new Promise((resolve) => { return new Promise((resolve) => {
if (typeof AP.getLocation !== 'function') {
resolve();
}
AP.getLocation((location) => { AP.getLocation((location) => {
resolve(location); resolve(location);
}); });
......
import Vue from 'vue'; import Vue from 'vue';
import $ from 'jquery';
import setConfigs from '@gitlab/ui/dist/config'; import setConfigs from '@gitlab/ui/dist/config';
import Translate from '~/vue_shared/translate'; import Translate from '~/vue_shared/translate';
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin'; import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
import { addSubscription, removeSubscription } from '~/jira_connect/api'; import { addSubscription, removeSubscription, getLocation } from '~/jira_connect/api';
import JiraConnectApp from './components/app.vue'; import JiraConnectApp from './components/app.vue';
import createStore from './store'; import createStore from './store';
import { SET_ERROR_MESSAGE } from './store/mutation_types'; import { SET_ERROR_MESSAGE } from './store/mutation_types';
const store = createStore(); const store = createStore();
/** const reqComplete = () => {
* Initialize form handlers for the Jira Connect app
*/
const initJiraFormHandlers = () => {
const reqComplete = () => {
AP.navigator.reload(); AP.navigator.reload();
}; };
const reqFailed = (res, fallbackErrorMessage) => { const reqFailed = (res, fallbackErrorMessage) => {
const { error = fallbackErrorMessage } = res || {}; const { error = fallbackErrorMessage } = res || {};
store.commit(SET_ERROR_MESSAGE, error); store.commit(SET_ERROR_MESSAGE, error);
}; };
if (typeof AP.getLocation === 'function') { const updateSignInLinks = async () => {
AP.getLocation((location) => { const location = await getLocation();
$('.js-jira-connect-sign-in').each(function updateSignInLink() { Array.from(document.querySelectorAll('.js-jira-connect-sign-in')).forEach((el) => {
const updatedLink = `${$(this).attr('href')}?return_to=${location}`; const updatedLink = `${el.getAttribute('href')}?return_to=${location}`;
$(this).attr('href', updatedLink); el.setAttribute('href', updatedLink);
}); });
}); };
}
$('#add-subscription-form').on('submit', function onAddSubscriptionForm(e) {
const addPath = $(this).attr('action');
const namespace = $('#namespace-input').val();
const initRemoveSubscriptionButtonHandlers = () => {
Array.from(document.querySelectorAll('.js-jira-connect-remove-subscription')).forEach((el) => {
el.addEventListener('click', function onRemoveSubscriptionClick(e) {
e.preventDefault(); e.preventDefault();
addSubscription(addPath, namespace) const removePath = e.target.getAttribute('href');
removeSubscription(removePath)
.then(reqComplete) .then(reqComplete)
.catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.')); .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;
}
$('.remove-subscription').on('click', function onRemoveSubscriptionClick(e) { formEl.addEventListener('submit', function onAddSubscriptionForm(e) {
const removePath = $(this).attr('href');
e.preventDefault(); e.preventDefault();
removeSubscription(removePath) const addPath = e.target.getAttribute('action');
const namespace = (e.target.querySelector('#namespace-input') || {}).value;
addSubscription(addPath, namespace)
.then(reqComplete) .then(reqComplete)
.catch((err) => .catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.'));
reqFailed(err.response.data, 'Failed to remove namespace. Please try again.'),
);
}); });
}; };
function initJiraConnect() { export async function initJiraConnect() {
const el = document.querySelector('.js-jira-connect-app'); initAddSubscriptionFormHandler();
initRemoveSubscriptionButtonHandlers();
initJiraFormHandlers(); await updateSignInLinks();
const el = document.querySelector('.js-jira-connect-app');
if (!el) { if (!el) {
return null; return null;
} }
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
%tr %tr
%td= subscription.namespace.full_path %td= subscription.namespace.full_path
%td= subscription.created_at %td= subscription.created_at
%td= link_to 'Remove', jira_connect_subscription_path(subscription), class: 'remove-subscription' %td= link_to 'Remove', jira_connect_subscription_path(subscription), class: 'js-jira-connect-remove-subscription'
- else - else
%h4.empty-subscriptions %h4.empty-subscriptions
No linked namespaces No linked namespaces
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
= yield :page_specific_styles = yield :page_specific_styles
= javascript_include_tag 'https://connect-cdn.atl-paas.net/all.js' = javascript_include_tag 'https://connect-cdn.atl-paas.net/all.js'
= javascript_include_tag 'https://unpkg.com/jquery@3.3.1/dist/jquery.min.js'
= Gon::Base.render_data(nonce: content_security_policy_nonce) = Gon::Base.render_data(nonce: content_security_policy_nonce)
= yield :head = yield :head
%body %body
......
import waitForPromises from 'helpers/wait_for_promises';
import { initJiraConnect } from '~/jira_connect';
import { removeSubscription } from '~/jira_connect/api';
jest.mock('~/jira_connect/api', () => ({
removeSubscription: jest.fn().mockResolvedValue(),
getLocation: jest.fn().mockResolvedValue('test/location'),
}));
describe('initJiraConnect', () => {
window.AP = {
navigator: {
reload: jest.fn(),
},
};
beforeEach(async () => {
setFixtures(`
<a class="js-jira-connect-sign-in" href="https://gitlab.com">Sign In</a>
<a class="js-jira-connect-sign-in" href="https://gitlab.com">Another Sign In</a>
<a href="https://gitlab.com/sub1" class="js-jira-connect-remove-subscription">Remove</a>
<a href="https://gitlab.com/sub2" class="js-jira-connect-remove-subscription">Remove</a>
<a href="https://gitlab.com/sub3" class="js-jira-connect-remove-subscription">Remove</a>
`);
await initJiraConnect();
});
describe('Sign in links', () => {
it('have `return_to` query parameter', () => {
Array.from(document.querySelectorAll('.js-jira-connect-sign-in')).forEach((el) => {
expect(el.href).toContain('return_to=test/location');
});
});
});
describe('`remove subscription` buttons', () => {
describe('on click', () => {
it('calls `removeSubscription`', () => {
Array.from(document.querySelectorAll('.js-jira-connect-remove-subscription')).forEach(
(removeSubscriptionButton) => {
removeSubscriptionButton.dispatchEvent(new Event('click'));
waitForPromises();
expect(removeSubscription).toHaveBeenCalledWith(removeSubscriptionButton.href);
expect(removeSubscription).toHaveBeenCalledTimes(1);
removeSubscription.mockClear();
},
);
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment