Skip to content

Commit

Permalink
Chore: convert marketplace price display component to use typescript (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuslc committed May 20, 2022
1 parent 45417ea commit 6d95144
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { Box } from '@rocket.chat/fuselage';
import { useTranslation } from '@rocket.chat/ui-contexts';
import React, { useMemo } from 'react';
import { TranslationKey, useTranslation } from '@rocket.chat/ui-contexts';
import React, { FC, useMemo } from 'react';

import { PricingPlan } from './definitions/PricingPlan';
import { formatPricingPlan, formatPrice } from './helpers';

const formatPriceAndPurchaseType = (purchaseType, pricingPlans, price) => {
type PriceDisplayProps = {
purchaseType: string;
pricingPlans: PricingPlan[];
price: number;
showType?: boolean;
marginInline?: string;
};

type PlanType = 'Subscription' | 'Paid' | 'Free';

type FormattedPriceAndPlan = {
type: PlanType;
price: string;
};

const formatPriceAndPurchaseType = (purchaseType: string, pricingPlans: PricingPlan[], price: number): FormattedPriceAndPlan => {
if (purchaseType === 'subscription') {
const type = 'Subscription';
if (!pricingPlans || !Array.isArray(pricingPlans) || pricingPlans.length === 0) {
Expand All @@ -20,25 +36,26 @@ const formatPriceAndPurchaseType = (purchaseType, pricingPlans, price) => {
return { type: 'Free', price: '-' };
};

function PriceDisplay({ purchaseType, pricingPlans, price, showType = true, ...props }) {
const PriceDisplay: FC<PriceDisplayProps> = ({ purchaseType, pricingPlans, price, showType = true, ...props }) => {
const t = useTranslation();

const { type, price: formattedPrice } = useMemo(
() => formatPriceAndPurchaseType(purchaseType, pricingPlans, price),
[purchaseType, pricingPlans, price],
);

return (
<Box display='flex' flexDirection='column' {...props}>
{showType && (
<Box color='default' withTruncatedText>
{t(type)}
{t(type as TranslationKey)}
</Box>
)}
<Box color='hint' withTruncatedText>
{!showType && type === 'Free' ? t(type) : formattedPrice}
</Box>
</Box>
);
}
};

export default PriceDisplay;
16 changes: 16 additions & 0 deletions apps/meteor/client/views/admin/apps/definitions/PricingPlan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type PricingPlan = {
id: string;
enabled: boolean;
price: number;
trialDays: number;
strategy: string;
isPerSeat: boolean;
tiers?: Tiers[];
};

export type Tiers = {
perUnit: boolean;
minimum: number;
maximum: number;
price: number;
};
4 changes: 3 additions & 1 deletion apps/meteor/client/views/admin/apps/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AppStatus } from '@rocket.chat/apps-engine/definition/AppStatus';

import { PricingPlan } from './definitions/PricingPlan';

export type App = {
id: string;
iconFileData: string;
Expand All @@ -14,7 +16,7 @@ export type App = {
version: string;
price: number;
purchaseType: string;
pricingPlans: unknown[];
pricingPlans: PricingPlan[];
iconFileContent: string;
installed?: boolean;
isEnterpriseOnly?: boolean;
Expand Down

0 comments on commit 6d95144

Please sign in to comment.