Add js confirm() to warn admins about promoted group updates (#15990)

This commit is contained in:
William Durand 2020-11-12 13:40:51 +01:00 коммит произвёл GitHub
Родитель 6765fbbe07
Коммит bac109cd9c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 32 добавлений и 0 удалений

Просмотреть файл

@ -132,6 +132,11 @@ class PromotedAddonAdmin(admin.ModelAdmin):
inlines = (PromotedApprovalInline, PrimaryHeroInline,
PromotedSubscriptionInline)
class Media:
js = (
'js/admin/promotedaddon.js',
)
@classmethod
def _transformer(self, objs):
Version.transformer_promoted(

Просмотреть файл

@ -0,0 +1,27 @@
document.addEventListener('DOMContentLoaded', () => {
'use strict';
const promotedAddonForm = document.getElementById('promotedaddon_form');
if (!promotedAddonForm) {
return;
}
const groupSelect = promotedAddonForm.querySelector('#id_group_id');
let initialIndex = groupSelect.selectedIndex;
const initialGroup = groupSelect.options[initialIndex].label || '';
// Enable the listener only when loading a promoted add-on belonging to a
// group that requires a subscription.
if (['sponsored', 'verified'].includes(initialGroup.toLowerCase())) {
groupSelect.addEventListener('change', () => {
if (
!confirm(
"This promoted add-on belongs to a group that requires a Stripe subscription, which should be cancelled in Stripe first. If you confirm this action, you will be able to update the group but Stripe won't be notified and the Stripe subscription will remain active. Do you really want to continue?",
)
) {
groupSelect.selectedIndex = initialIndex;
}
});
}
});