Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor voucher view code

dominik-zeglen868c690a1f63
diff --git a/saleor/static/dashboard/js/components/coupons.js b/saleor/static/dashboard/js/components/coupons.js
deleted file mode 100644
index ae54eff6b2..0000000000
--- a/saleor/static/dashboard/js/components/coupons.js
+++ /dev/null
@@ -1,26 +0,0 @@
-export default $(document).ready(() => {
- let $voucherTypeInput = $('.body-vouchers [name="type"]');
- if ($voucherTypeInput.length) {
- let $discountValueType = $('[name="discount_value_type"]');
- let $voucherForms = $('.voucher-form');
- let $applyToProduct = $('[name="product-apply_to"]').parents('.input');
- let $applyToCategory = $('[name="category-apply_to"]').parents('.input');
- let onChange = () => {
- let discountValueType = $discountValueType.val();
- let type = $voucherTypeInput.val();
- let hide = discountValueType === 'percentage';
- $applyToProduct.toggleClass('hide', hide);
- $applyToCategory.toggleClass('hide', hide);
-
- $voucherForms.each((index, form) => {
- let $form = $(form);
- let hideForm = $form.data('type') !== type;
- $form.toggleClass('hide', hideForm);
- });
- };
-
- $discountValueType.on('change', onChange);
- $voucherTypeInput.on('change', onChange);
- $voucherTypeInput.trigger('change');
- }
-});
diff --git a/saleor/static/dashboard/js/components/index.js b/saleor/static/dashboard/js/components/index.js
index c69dfa5ee3..036de55c2d 100644
--- a/saleor/static/dashboard/js/components/index.js
+++ b/saleor/static/dashboard/js/components/index.js
@@ -1,11 +1,11 @@
+import './async-form';
+import './bulk-actions';
import './datepicker';
+import './filters';
+import './image-gallery';
+import './misc';
import './modal';
import './side-menu';
-import './image-gallery';
-import './coupons';
-import './async-form';
import './styleguide';
-import './bulk-actions';
import './tab';
-import './filters';
-import './misc';
+import './vouchers';
diff --git a/saleor/static/dashboard/js/components/vouchers.js b/saleor/static/dashboard/js/components/vouchers.js
new file mode 100644
index 0000000000..771125c385
--- /dev/null
+++ b/saleor/static/dashboard/js/components/vouchers.js
@@ -0,0 +1,35 @@
+/*
+ * Handler for voucher type change event. Manages showing options
+ * that are connected to selected voucher type and hiding others.
+ */
+function onVoucherTypeChange (e) {
+ const $target = $(e.currentTarget);
+ $('.voucher-form').each((index, form) => {
+ const $form = $(form);
+ $form.toggleClass('hide', $form.data('type') !== $target.val());
+ });
+}
+
+/*
+ * Handler for discount type change event. Shows additional options
+ * if 'percentage' option is selected.
+ */
+function onDiscountTypeChange (e) {
+ const $target = $(e.currentTarget);
+ const showOnPercentage = '[name="product-apply_to"], [name="category-apply_to"]';
+ const $showOnPercentage = $(showOnPercentage).parents('.input');
+ $showOnPercentage.toggleClass('hide', $target.val() === 'percentage');
+}
+
+const init = $(() => {
+ $('[name="discount_value_type"]')
+ .on('change', onDiscountTypeChange)
+ .trigger('change');
+ $('[name="type"]')
+ .on('change', onVoucherTypeChange)
+ .trigger('change');
+});
+
+export {
+ init as default,
+};