Skip to content

Refactored corporate-member-join.js #2043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions djangoproject/static/js/djangoproject.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,32 @@ document.querySelectorAll('.btn-clipboard').forEach(function (el) {
input_el.value = '25';
});
})();

// Manage amount and membership level fields on corporate membership page
(function () {
const form_el = document.querySelector('.corporate-membership-join-form');

if (!form_el) {
return;
}

const amount_el = form_el.querySelector('#id_amount');
const level_el = form_el.querySelector('#id_membership_level');
const levels = [-Infinity, 2000, 5000, 12500, 30000, 100000];

amount_el.addEventListener('change', function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many ways to implement the functionality for this handler, but this seemed to be the simplest to me.

let value;

for (let i = 0; i < levels.length; i++) {
if (this.value >= levels[i]) {
value = i;
}

level_el.value = value || '';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty string sets the select back to the default selection.

}
});

level_el.addEventListener('change', function () {
amount_el.value = this.value ? levels[Number(this.value)] : '';
Copy link
Member Author

@adamzap adamzap Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty string sets the select back to the default selection.

Also, I don't think the Number conversion is strictly necessary here, but I using a string index for an array feels wrong.

});
})();
4 changes: 0 additions & 4 deletions djangoproject/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ define(function () {
mods.push('mod/stripe-change-card');
}

if (hasClass('corporate-membership-join-form')) {
mods.push('mod/corporate-member-join');
}

require(mods);
});
52 changes: 0 additions & 52 deletions djangoproject/static/js/mod/corporate-member-join.js

This file was deleted.