From 217e983a02c1f43def01065476b7f556b125db10 Mon Sep 17 00:00:00 2001 From: Rishabh Mishra Date: Mon, 26 Feb 2024 11:25:17 +0530 Subject: [PATCH] chore(sdk): sort features in plan list page (#510) * chore: sort features as per metadata weightage * chore: add alphabetical sort for fallback * chore: show feature metadata value --- .../organization/plans/helpers/index.ts | 23 ------------ .../components/organization/plans/index.tsx | 36 ++++++++++--------- 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/sdks/js/packages/core/react/components/organization/plans/helpers/index.ts b/sdks/js/packages/core/react/components/organization/plans/helpers/index.ts index 56c5811b6..b3fe93d1d 100644 --- a/sdks/js/packages/core/react/components/organization/plans/helpers/index.ts +++ b/sdks/js/packages/core/react/components/organization/plans/helpers/index.ts @@ -64,26 +64,3 @@ export function groupPlansPricingByInterval(plans: V1Beta1Plan[]) { return Object.values(plansMap); } - -export function getFeaturesWeightageMap( - features: V1Beta1Feature[], - plans: V1Beta1Plan[] -) { - const featureMap = features.reduce((acc, feature) => { - if (feature.id) { - acc[feature.id] = 0; - } - return acc; - }, {} as Record); - - plans.forEach(plan => { - plan?.products?.forEach(product => { - product?.features?.forEach(feature => { - if (feature.id) { - featureMap[feature.id] = featureMap[feature.id] + 1; - } - }); - }); - }); - return featureMap; -} diff --git a/sdks/js/packages/core/react/components/organization/plans/index.tsx b/sdks/js/packages/core/react/components/organization/plans/index.tsx index 714f21452..daf61f049 100644 --- a/sdks/js/packages/core/react/components/organization/plans/index.tsx +++ b/sdks/js/packages/core/react/components/organization/plans/index.tsx @@ -13,10 +13,7 @@ import { V1Beta1Feature, V1Beta1Plan } from '~/src'; import { toast } from 'sonner'; import Skeleton from 'react-loading-skeleton'; import plansStyles from './plans.module.css'; -import { - getFeaturesWeightageMap, - groupPlansPricingByInterval -} from './helpers'; +import { groupPlansPricingByInterval } from './helpers'; import { IntervalKeys, IntervalLabelMap, @@ -260,22 +257,26 @@ const PlanPricingColumn = ({ {features.map(feature => { + const featureId = feature?.id || ''; + const planFeature = _.get(plan?.features, featureId, { metadata: {} }); + const value = (planFeature?.metadata as Record)?.value; + const isAvailable = value?.toLowerCase() === 'true'; return ( - {plan?.features.hasOwnProperty(feature?.id || '') ? ( + {isAvailable ? ( checked - ) : ( - '' - )} + ) : value ? ( + {value} + ) : null} ); })} @@ -311,13 +312,16 @@ const PlansList = ({ }); }); - const featuresWeightageMap = getFeaturesWeightageMap(features, plans); - - const sortedFeatures = features.sort((f1, f2) => { - const f1Weight = (f1.id && featuresWeightageMap[f1.id]) || 0; - const f2Weight = (f2.id && featuresWeightageMap[f2.id]) || 0; - return f2Weight - f1Weight; - }); + const totalFeatures = features.length; + const sortedFeatures = features + .sort((f1, f2) => ((f1?.name || '') > (f2?.name || '') ? -1 : 1)) + .sort((f1, f2) => { + const f1Weight = + (f1.metadata as Record)?.weightage || totalFeatures; + const f2Weight = + (f2.metadata as Record)?.weightage || totalFeatures; + return f1Weight - f2Weight; + }); return (