feat: sort subscription programs in program dashboard (#32651)

This commit is contained in:
Nawfal Ahmed
2023-07-06 14:01:40 +05:00
committed by GitHub
parent b477a20ad2
commit 139d0b2392
3 changed files with 34 additions and 5 deletions

View File

@@ -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({

View File

@@ -42,6 +42,7 @@ describe('Program card View', () => {
name: 'Wageningen University & Research',
},
],
subscriptionIndex: 1,
};
const userProgress = [
{
@@ -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');
});
});

View File

@@ -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();
}