-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: centralize Checkout utils in helpers/
- Loading branch information
1 parent
0393abc
commit ce540f8
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isSafeInteger } from "@nerdware/ts-type-safety-utils"; | ||
import { fmt } from "@/utils/formatters"; | ||
import type { OpenApiSchemas } from "@/types/open-api"; | ||
|
||
/** | ||
* > `The values used here are for DISPLAY PURPOSES ONLY and merely convey | ||
* information to the user. All pricing/product info is stored and calculated | ||
* by the backend API. Sending invalid pricing/product info to the server | ||
* results a 400 response.` | ||
*/ | ||
export const getPrice_FOR_DISPLAY_ONLY = <FormatOpts extends { formatAsCurrency?: boolean }>( | ||
price: number, | ||
discountPercentage?: OpenApiSchemas["PromoCodeInfo"]["discountPercentage"] | null, // 10 = 10% off | ||
{ formatAsCurrency }: FormatOpts = {} as FormatOpts | ||
) => { | ||
const priceWithDiscount = isSafeInteger(discountPercentage) | ||
? price - price * (discountPercentage / 100) | ||
: price; | ||
|
||
const returnValue = | ||
formatAsCurrency === true ? fmt.intToCurrencyStr(priceWithDiscount) : priceWithDiscount; | ||
|
||
return returnValue as FormatOpts["formatAsCurrency"] extends true ? string : number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./getTotalForDisplay"; | ||
export * from "./subPricingDisplayConfigs"; |
55 changes: 55 additions & 0 deletions
55
src/pages/CheckoutPage/helpers/subPricingDisplayConfigs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { SubscriptionPriceLabel } from "@/graphql/types"; | ||
|
||
/** | ||
* > `The values used here are for DISPLAY PURPOSES ONLY and merely convey | ||
* information to the user. All pricing/product info is stored and calculated | ||
* by the backend API. Sending invalid pricing/product info to the server | ||
* results a 400 response.` | ||
*/ | ||
export const SUB_PRICING_DISPLAY_CONFIGS = { | ||
TRIAL: { | ||
label: "Try Fixit", | ||
price: 0, | ||
trialDays: 14, | ||
afterTrial: { | ||
price: 500, | ||
billingPeriod: "month", | ||
}, | ||
}, | ||
MONTHLY: { | ||
label: "Monthly Subscription", | ||
price: 500, | ||
billingPeriod: "month", | ||
}, | ||
ANNUAL: { | ||
label: "Annual Subscription", | ||
price: 5000, | ||
billingPeriod: "year", | ||
}, | ||
} as { | ||
readonly [Sub in SubscriptionPriceLabel]: Sub extends "TRIAL" | ||
? TrialSubBillingDisplayConfigs | ||
: PaidSubBillingDisplayConfigs; | ||
}; | ||
|
||
interface BaseSubPricingDisplayConfigs { | ||
label: string; | ||
} | ||
|
||
type SubBillingInfo = { | ||
price: number; | ||
billingPeriod: "month" | "year"; | ||
}; | ||
|
||
type PaidSubBillingDisplayConfigs = BaseSubPricingDisplayConfigs & | ||
SubBillingInfo & { | ||
trialDays?: never; | ||
afterTrial?: never; | ||
}; | ||
|
||
type TrialSubBillingDisplayConfigs = BaseSubPricingDisplayConfigs & { | ||
price: 0; | ||
trialDays: number; | ||
afterTrial: SubBillingInfo; | ||
billingPeriod?: never; | ||
}; |