gitlab-org--gitlab-foss/app/assets/javascripts/protected_tags/protected_tag_edit.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

import { deprecatedCreateFlash as flash } from '../flash';
import axios from '../lib/utils/axios_utils';
import { FAILED_TO_UPDATE_TAG_MESSAGE } from './constants';
import ProtectedTagAccessDropdown from './protected_tag_access_dropdown';
2017-04-06 08:47:19 +00:00
export default class ProtectedTagEdit {
constructor(options) {
this.$wrap = options.$wrap;
2017-04-06 13:39:48 +00:00
this.$allowedToCreateDropdownButton = this.$wrap.find('.js-allowed-to-create');
this.onSelectCallback = this.onSelect.bind(this);
2017-04-06 08:47:19 +00:00
this.buildDropdowns();
}
buildDropdowns() {
// Allowed to create dropdown
this.protectedTagAccessDropdown = new ProtectedTagAccessDropdown({
2017-04-06 13:39:48 +00:00
$dropdown: this.$allowedToCreateDropdownButton,
2017-04-06 08:47:19 +00:00
data: gon.create_access_levels,
2017-04-06 13:39:48 +00:00
onSelect: this.onSelectCallback,
2017-04-06 08:47:19 +00:00
});
}
onSelect() {
const $allowedToCreateInput = this.$wrap.find(
`input[name="${this.$allowedToCreateDropdownButton.data('fieldName')}"]`,
);
2017-04-06 08:47:19 +00:00
// Do not update if one dropdown has not selected any option
if (!$allowedToCreateInput.length) return;
2017-04-06 13:39:48 +00:00
this.$allowedToCreateDropdownButton.disable();
2017-04-06 08:47:19 +00:00
axios
.patch(this.$wrap.data('url'), {
protected_tag: {
create_access_levels_attributes: [
{
id: this.$allowedToCreateDropdownButton.data('accessLevelId'),
access_level: $allowedToCreateInput.val(),
},
],
},
})
.then(() => {
this.$allowedToCreateDropdownButton.enable();
})
.catch(() => {
this.$allowedToCreateDropdownButton.enable();
window.scrollTo({ top: 0, behavior: 'smooth' });
flash(FAILED_TO_UPDATE_TAG_MESSAGE);
});
2017-04-06 08:47:19 +00:00
}
}