Skip to content

Commit

Permalink
feat: sort subscription programs in program dashboard (openedx#32651)
Browse files Browse the repository at this point in the history
  • Loading branch information
NawfalAhmed committed Jul 6, 2023
1 parent b477a20 commit 139d0b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
31 changes: 31 additions & 0 deletions lms/static/js/learner_dashboard/program_list_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,37 @@ function ProgramListFactory(options) {
new HeaderView({
context: options,
}).render();

const activeSubscriptions = options.programsSubscriptionData
.filter(({ subscription_state }) => subscription_state === 'active')
.sort((a, b) => new Date(b.created) - new Date(a.created));

// Sort programs so programs with active subscriptions are at the top
if (activeSubscriptions.length) {
options.programsData = options.programsData
.map((programsData) => ({
...programsData,
subscriptionIndex: activeSubscriptions.findIndex(
({ resource_id }) => resource_id === programsData.uuid,
),
}))
.sort(({ subscriptionIndex: indexA }, { subscriptionIndex: indexB }) => {
switch (true) {
case indexA === -1 && indexB === -1:
// Maintain the original order for non-subscription programs
return 0;
case indexA === -1:
// Move non-subscription program to the end
return 1;
case indexB === -1:
// Keep non-subscription program to the end
return -1;
default:
// Sort by subscriptionIndex in ascending order
return indexA - indexB;
}
});
}
}

new CollectionListView({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Program card View', () => {
name: 'Wageningen University & Research',
},
],
subscriptionIndex: 1,
};
const userProgress = [
{
Expand Down Expand Up @@ -173,6 +174,6 @@ describe('Program card View', () => {
});

it('should render the subscription badge if subscription is active', () => {
expect(view.$('.subscription-badge .badge').html().trim()).toEqual('Subscribed');
expect(view.$('.subscription-badge .badge').html()?.trim()).toEqual('Subscribed');
});
});
5 changes: 1 addition & 4 deletions lms/static/js/learner_dashboard/views/program_card_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ class ProgramCardView extends Backbone.View {
}
this.isSubscribed = (
context.isUserB2CSubscriptionsEnabled &&
context.subscriptionCollection?.some({
resource_id: this.model.get('uuid'),
subscription_state: 'active',
})
this.model.get('subscriptionIndex') > -1
) ?? false;
this.render();
}
Expand Down

0 comments on commit 139d0b2

Please sign in to comment.