Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: simplify invalid coupon code handling

Abdullah Atta6925f328f3bc
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-discounted-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-discounted-prices-Chromium-linux.txt
index cfd203b6f..9a77974a9 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-discounted-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-discounted-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: ₹370
tax: ₹70
discount: -₹220
-total: ₹220then ₹440/mo
\ No newline at end of file
+total: ₹220
+first month then ₹440/mo
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-prices-Chromium-linux.txt
index a2bd3e155..a9ae67d89 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-IN-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: ₹370
tax: ₹70
discount: ₹0
-total: ₹440/mo
\ No newline at end of file
+total: ₹440/mo
+forever
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-discounted-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-discounted-prices-Chromium-linux.txt
index 3aa718c19..6cd6c887b 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-discounted-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-discounted-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: $10
tax: $0
discount: -$10
-total: $10then $10/mo
\ No newline at end of file
+total: $10
+first month then $10/mo
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-prices-Chromium-linux.txt
index 3c65aafcb..6da5a0a77 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-monthly-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: $10
tax: $0
discount: $0
-total: $10/mo
\ No newline at end of file
+total: $10/mo
+forever
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-discounted-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-discounted-prices-Chromium-linux.txt
index 00a087b2b..7bb226011 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-discounted-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-discounted-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: ₹4070
tax: ₹740
-discount: -₹2400
-total: ₹2400then ₹4800/yr
\ No newline at end of file
+discount: -₹720
+total: ₹4080
+first year then ₹4800/yr
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-prices-Chromium-linux.txt
index 57ef3c1bb..35b33c0ca 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-IN-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: ₹4070
tax: ₹740
discount: ₹0
-total: ₹4800/yr
\ No newline at end of file
+total: ₹4800/yr
+forever
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-discounted-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-discounted-prices-Chromium-linux.txt
index bde75a663..0944b8aa8 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-discounted-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-discounted-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: $50
tax: $0
discount: -$10
-total: $50then $50/yr
\ No newline at end of file
+total: $50
+first year then $50/yr
\ No newline at end of file
diff --git a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-prices-Chromium-linux.txt b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-prices-Chromium-linux.txt
index a9b5bd67e..46d69a7aa 100644
--- a/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-prices-Chromium-linux.txt
+++ b/apps/web/__e2e__/checkout.test.js-snapshots/checkout-yearly-prices-Chromium-linux.txt
@@ -1,4 +1,5 @@
subtotal: $50
tax: $0
discount: $0
-total: $50/yr
\ No newline at end of file
+total: $50/yr
+forever
\ No newline at end of file
diff --git a/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx b/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx
index 299a2a450..e8a71824c 100644
--- a/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx
+++ b/apps/web/src/components/dialogs/buy-dialog/buy-dialog.tsx
@@ -33,7 +33,7 @@ import { Features } from "./features";
import { PaddleCheckout } from "./paddle";
import { Period, Plan, PricingInfo } from "./types";
import { PLAN_METADATA, usePlans } from "./plans";
-import { formatPeriod, PlansList } from "./plan-list";
+import { formatPeriod, getFullPeriod, PlansList } from "./plan-list";
import { showToast } from "../../../utils/toast";
import { TaskManager } from "../../../common/task-manager";
import { db } from "../../../common/db";
@@ -51,12 +51,17 @@ export function BuyDialog(props: BuyDialogProps) {
const { onClose, couponCode, plan } = props;
const theme = useTheme() as Theme;
+ const onApplyCoupon = useCheckoutStore((store) => store.onApplyCoupon);
useEffect(() => {
return () => {
useCheckoutStore.getState().reset();
};
}, []);
+ useEffect(() => {
+ if (couponCode) onApplyCoupon(couponCode);
+ }, [couponCode, onApplyCoupon]);
+
return (
<Modal
isOpen={true}
@@ -136,37 +141,41 @@ export function BuyDialog(props: BuyDialogProps) {
p={4}
py={50}
>
- <SideBar
- onClose={onClose}
- couponCode={couponCode}
- initialPlan={plan}
- />
+ <SideBar onClose={onClose} initialPlan={plan} />
</Flex>
- <Details initialCouponCode={couponCode} />
+ <Details />
</Flex>
</Modal>
);
}
type SideBarProps = {
- couponCode?: string;
initialPlan?: Period;
onClose: () => void;
};
function SideBar(props: SideBarProps) {
- const { couponCode, initialPlan, onClose } = props;
+ const { initialPlan, onClose } = props;
const [showPlans, setShowPlans] = useState(!!initialPlan);
const onPlanSelected = useCheckoutStore((state) => state.onPlanSelected);
const selectedPlan = useCheckoutStore((state) => state.selectedPlan);
const pricingInfo = useCheckoutStore((state) => state.pricingInfo);
const user = useUserStore((store) => store.user);
+ const couponCode = useCheckoutStore((store) => store.couponCode);
+ const onApplyCoupon = useCheckoutStore((store) => store.onApplyCoupon);
if (user && selectedPlan)
return (
<SelectedPlan
plan={selectedPlan}
pricingInfo={pricingInfo}
- onChangePlan={initialPlan ? undefined : () => onPlanSelected(undefined)}
+ onChangePlan={
+ initialPlan
+ ? undefined
+ : () => {
+ onApplyCoupon(undefined);
+ onPlanSelected(undefined);
+ }
+ }
/>
);
@@ -212,16 +221,11 @@ function SideBar(props: SideBarProps) {
);
}
-type DetailsProps = {
- initialCouponCode?: string;
-};
-function Details(props: DetailsProps) {
- const { initialCouponCode } = props;
+function Details() {
const user = useUserStore((store) => store.user);
const selectedPlan = useCheckoutStore((state) => state.selectedPlan);
const onPriceUpdated = useCheckoutStore((state) => state.onPriceUpdated);
const couponCode = useCheckoutStore((store) => store.couponCode);
- const onApplyCoupon = useCheckoutStore((store) => store.onApplyCoupon);
const setIsApplyingCoupon = useCheckoutStore(
(store) => store.setIsApplyingCoupon
);
@@ -237,11 +241,16 @@ function Details(props: DetailsProps) {
onCouponApplied={() => setIsApplyingCoupon(true)}
onPriceUpdated={(pricingInfo) => {
onPriceUpdated(pricingInfo);
- console.log(initialCouponCode, "applying coupon", couponCode);
-
- if (!initialCouponCode || initialCouponCode === couponCode) return;
- console.log(initialCouponCode, "applying coupon");
- onApplyCoupon(initialCouponCode);
+ // console.log(
+ // initialCouponCode,
+ // "applying coupon",
+ // couponCode,
+ // pricingInfo.coupon
+ // );
+
+ // if (!initialCouponCode || initialCouponCode === couponCode) return;
+ // console.log(initialCouponCode, "applying coupon");
+ // onApplyCoupon(initialCouponCode);
}}
/>
);
@@ -343,14 +352,12 @@ type SelectedPlanProps = {
function SelectedPlan(props: SelectedPlanProps) {
const { plan, pricingInfo, onChangePlan } = props;
const metadata = PLAN_METADATA[plan.period];
- const [isInvalidCoupon, setIsInvalidCoupon] = useState(false);
const [isApplyingCoupon, setIsApplyingCoupon] = useCheckoutStore((store) => [
store.isApplyingCoupon,
store.setIsApplyingCoupon
]);
const onApplyCoupon = useCheckoutStore((store) => store.onApplyCoupon);
- const couponCode = useCheckoutStore((store) => store.couponCode);
const couponInputRef = useRef<HTMLInputElement>(null);
const applyCoupon = useCallback(() => {
@@ -371,15 +378,7 @@ function SelectedPlan(props: SelectedPlanProps) {
const couponValue = couponInputRef.current.value;
- const isInvalidCoupon =
- (!!couponValue && couponValue !== pricingInfo?.coupon) ||
- (!!couponCode && couponCode !== pricingInfo?.coupon);
-
- setIsInvalidCoupon(isInvalidCoupon);
- if (isInvalidCoupon) {
- if (couponCode) couponInputRef.current.value = couponCode;
- return;
- }
+ if (pricingInfo?.invalidCoupon) return;
const pricingInfoCoupon = pricingInfo?.coupon || "";
if (couponValue !== pricingInfoCoupon) {
@@ -417,7 +416,7 @@ function SelectedPlan(props: SelectedPlanProps) {
<Field
inputRef={couponInputRef}
- variant={isInvalidCoupon ? "error" : "input"}
+ variant={pricingInfo.invalidCoupon ? "error" : "input"}
sx={{ alignSelf: "stretch", my: 2 }}
styles={{ input: { fontSize: "body" } }}
data-test-id="checkout-coupon-code"
@@ -426,7 +425,7 @@ function SelectedPlan(props: SelectedPlanProps) {
placeholder={
isApplyingCoupon ? "Applying coupon code..." : "Coupon code"
}
- autoFocus={isInvalidCoupon}
+ autoFocus={pricingInfo.invalidCoupon}
disabled={!!pricingInfo?.coupon}
onKeyUp={(e: KeyboardEvent) => {
if (e.code === "Enter") applyCoupon();
@@ -531,14 +530,17 @@ function CheckoutPricing(props: CheckoutPricingProps) {
Total
</Text>
<Text
+ as="div"
data-test-id={`checkout-price-total`}
variant="body"
sx={{ fontSize: "heading", textAlign: "end", color: "text" }}
>
{currentTotal}
- {isDiscounted ? null : (
- <Text sx={{ fontSize: "body" }}>{`then ${recurringTotal}`}</Text>
- )}
+ <Text as="div" sx={{ fontSize: "body" }}>
+ {isDiscounted
+ ? "forever"
+ : `first ${getFullPeriod(period)} then ${recurringTotal}`}
+ </Text>
</Text>
</Flex>
</>
diff --git a/apps/web/src/components/dialogs/buy-dialog/paddle.tsx b/apps/web/src/components/dialogs/buy-dialog/paddle.tsx
index 6ec31d841..68468a54d 100644
--- a/apps/web/src/components/dialogs/buy-dialog/paddle.tsx
+++ b/apps/web/src/components/dialogs/buy-dialog/paddle.tsx
@@ -65,10 +65,11 @@ export function PaddleCheckout(props: PaddleCheckoutProps) {
}, [checkoutId]);
const updatePrice = useCallback(
- async (checkoutId: string) => {
+ async (checkoutId: string, isInvalidCoupon?: boolean) => {
const checkoutData = await getCheckoutData(checkoutId);
if (!checkoutData) return;
const pricingInfo = getPricingInfo(plan, checkoutData);
+ pricingInfo.invalidCoupon = isInvalidCoupon;
if (onPriceUpdated) onPriceUpdated(pricingInfo);
return pricingInfo;
},
@@ -123,7 +124,7 @@ export function PaddleCheckout(props: PaddleCheckoutProps) {
? await applyCoupon(checkoutId, coupon)
: await removeCoupon(checkoutId);
if (!checkoutData) {
- await updatePrice(checkoutId);
+ await updatePrice(checkoutId, true);
return;
}
appliedCouponCode.current = coupon;
@@ -235,6 +236,7 @@ async function applyCoupon(
headers,
method: "POST"
});
+
if (!response.ok) return false;
const json = (await response.json()) as CheckoutDataResponse;
diff --git a/apps/web/src/components/dialogs/buy-dialog/plan-list.tsx b/apps/web/src/components/dialogs/buy-dialog/plan-list.tsx
index 383d3faf5..8441b4e36 100644
--- a/apps/web/src/components/dialogs/buy-dialog/plan-list.tsx
+++ b/apps/web/src/components/dialogs/buy-dialog/plan-list.tsx
@@ -83,6 +83,7 @@ export function PlansList(props: PlansListProps) {
>
<Text variant="subtitle" sx={{ fontWeight: "normal" }}>
{metadata.title}
+ <br />
<Text
variant="body"
sx={{ fontWeight: "normal", color: "fontTertiary" }}
@@ -135,3 +136,7 @@ function RecurringPricing(props: RecurringPricingProps) {
export function formatPeriod(period: Period) {
return period === "monthly" ? "/mo" : period === "yearly" ? "/yr" : "";
}
+
+export function getFullPeriod(period: Period) {
+ return period === "monthly" ? "month" : period === "yearly" ? "year" : "";
+}
diff --git a/apps/web/src/components/dialogs/buy-dialog/plans.ts b/apps/web/src/components/dialogs/buy-dialog/plans.ts
index d49fb0f07..ec7bc9f41 100644
--- a/apps/web/src/components/dialogs/buy-dialog/plans.ts
+++ b/apps/web/src/components/dialogs/buy-dialog/plans.ts
@@ -52,6 +52,7 @@ export const PLAN_METADATA: Record<Period, PlanMetadata> = {
let CACHED_PLANS: Plan[];
export async function getPlans(): Promise<Plan[] | null> {
+ return DEFAULT_PLANS;
if (isTesting()) return DEFAULT_PLANS;
if (CACHED_PLANS) return CACHED_PLANS;
diff --git a/apps/web/src/components/dialogs/buy-dialog/store.ts b/apps/web/src/components/dialogs/buy-dialog/store.ts
index 9436a3d70..b1dd69363 100644
--- a/apps/web/src/components/dialogs/buy-dialog/store.ts
+++ b/apps/web/src/components/dialogs/buy-dialog/store.ts
@@ -53,7 +53,6 @@ export const useCheckoutStore = create<ICheckoutStore>((set) => ({
onApplyCoupon: (couponCode) =>
set(
produce((state: ICheckoutStore) => {
- console.log("SETTING coupon", couponCode);
state.couponCode = couponCode;
})
),
diff --git a/apps/web/src/components/dialogs/buy-dialog/types.ts b/apps/web/src/components/dialogs/buy-dialog/types.ts
index 8c1622569..2585b4160 100644
--- a/apps/web/src/components/dialogs/buy-dialog/types.ts
+++ b/apps/web/src/components/dialogs/buy-dialog/types.ts
@@ -104,6 +104,7 @@ export type PricingInfo = {
recurringPrice: Price;
discount: Discount;
coupon?: string;
+ invalidCoupon?: boolean;
};
export type Discount = {