A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: improve buy dialog with coupons support
thecodrrf92b8b4e61e3
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 0155d7b88..760a5db0c 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -16,6 +16,7 @@"clipboard": "^2.0.6","cogo-toast": "^4.2.3","compressorjs": "^1.0.7",+ "currency-symbol-map": "^5.0.1","dayjs": "^1.10.4","electron-serve": "^1.1.0","electron-updater": "^4.3.8",diff --git a/apps/web/src/common/checkout.js b/apps/web/src/common/checkout.jsindex fef114f89..045d1c026 100644--- a/apps/web/src/common/checkout.js+++ b/apps/web/src/common/checkout.js@@ -1,18 +1,22 @@import { trackEvent } from "../utils/analytics";-const VENDOR_ID = process.env.NODE_ENV === "development" ? 1506 : 128190;-const PRODUCT_ID = process.env.NODE_ENV === "development" ? 9822 : 648884;+const isDev = process.env.NODE_ENV === "development";-function loadPaddle() {+const VENDOR_ID = isDev ? 1506 : 128190;+const PRODUCT_ID = isDev ? 9822 : 648884;++function loadPaddle(eventCallback) {return new Promise((resolve) => {var script = document.createElement("script");script.src = "https://cdn.paddle.com/paddle/paddle.js";script.async = true;var firstScriptElement = document.getElementsByTagName("script")[0];script.onload = function () {- if (process.env.NODE_ENV === "development")- window.Paddle.Environment.set("sandbox");- window.Paddle.Setup({ vendor: VENDOR_ID });+ if (isDev) window.Paddle.Environment.set("sandbox");+ window.Paddle.Setup({+ vendor: VENDOR_ID,+ eventCallback,+ });resolve();};firstScriptElement.parentNode.insertBefore(script, firstScriptElement);@@ -57,5 +61,25 @@ async function openPaddleDialog(overrideUrl) {});}-export { upgrade, openPaddleDialog };-// (async () => await upgrade())();+async function getCouponData(coupon) {+ let url =+ "https://checkout-service.paddle.com/checkout/97379638-chre9f4b00ed6b3-732b0e853d/coupon";+ const response = await fetch(url, {+ headers: {+ accept: "application/json, text/plain, */*",+ "content-type": "application/json;charset=UTF-8",+ },+ body: coupon+ ? JSON.stringify({ data: { coupon_code: coupon } })+ : undefined,+ method: coupon ? "POST" : "DELETE",+ });+ const json = await response.json();+ if (response.ok) {+ return json.data;+ } else {+ throw new Error(json.errors[0].details);+ }+}++export { upgrade, openPaddleDialog, getCouponData };diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.jsindex ad979fc47..227002786 100644--- a/apps/web/src/common/dialogcontroller.js+++ b/apps/web/src/common/dialogcontroller.js@@ -90,23 +90,10 @@ export function showAddNotebookDialog() {));}-const itemIdToIndex = {- sync: 0,- editor: 3,- backups: 5,- customize: 6,- discord: 7,- [CHECK_IDS.noteExport]: 4,- [CHECK_IDS.noteColor]: 1,- [CHECK_IDS.noteTag]: 1,- [CHECK_IDS.notebookAdd]: 1,- [CHECK_IDS.vaultAdd]: 2,- [CHECK_IDS.backupEncrypt]: 5,-};-export function showBuyDialog(initialItemId) {+export function showBuyDialog(couponCode) {return showDialog((Dialogs, perform) => (<Dialogs.BuyDialog- initialIndex={itemIdToIndex[initialItemId]}+ couponCode={couponCode}onCancel={() => perform(false)}/>));diff --git a/apps/web/src/components/announcement/index.js b/apps/web/src/components/announcement/index.jsindex f4f7ca89a..567495c02 100644--- a/apps/web/src/components/announcement/index.js+++ b/apps/web/src/components/announcement/index.js@@ -1,11 +1,7 @@import React from "react";import { Button, Flex, Text } from "rebass";import * as Icon from "../icons";-import useAnnouncement from "../../utils/useAnnouncement";-import { db } from "../../common/db";-import { upgrade } from "../../common/checkout";-import { showLogInDialog } from "../../common/dialog-controller";-import { showToast } from "../../utils/toast";+import { showBuyDialog } from "../../common/dialog-controller";import { trackEvent } from "../../utils/analytics";function Announcement({ announcement, removeAnnouncement }) {@@ -65,31 +61,11 @@ function Announcement({ announcement, removeAnnouncement }) {trackEvent(announcement.cta.text, "Announcement CTA");switch (announcement.cta.type) {case "link":- window.open(announcement.cta.action, "_blank");+ window.open(announcement.cta.action, "_self");break;case "promo":const coupon = announcement.cta.action;- let user = await db.user.getUser();- if (user) {- await upgrade(user, coupon);- return;- }- await showLogInDialog(- `Login in to ${announcement.cta.text.toLowerCase()}`,- `Use the coupon "${coupon}" at checkout to ${announcement.cta.text.toLowerCase()} your first month.`,- announcement.cta.text,- true- );-- user = await db.user.getUser();- if (!user) {- showToast(- "error",- "You are not logged in. Please try again."- );- return;- }- await upgrade(user, coupon);+ await showBuyDialog(coupon);break;default:return;diff --git a/apps/web/src/components/dialogs/buydialog.js b/apps/web/src/components/dialogs/buydialog.jsindex c208258f8..50d54b08f 100644--- a/apps/web/src/components/dialogs/buydialog.js+++ b/apps/web/src/components/dialogs/buydialog.js@@ -1,10 +1,10 @@-import React from "react";+import React, { useEffect, useState } from "react";import { Text, Flex, Button } from "rebass";import Dialog from "./dialog";import * as Icon from "../icons";import { useStore as useUserStore } from "../../stores/user-store";import { useStore as useThemeStore } from "../../stores/theme-store";-import { upgrade } from "../../common/checkout";+import { getCouponData, upgrade } from "../../common/checkout";import { showSignUpDialog } from "../../common/dialog-controller";import { ReactComponent as Personalization } from "../../assets/accent.svg";import { ReactComponent as Backups } from "../../assets/backup.svg";@@ -15,6 +15,7 @@ import { ReactComponent as Sync } from "../../assets/sync.svg";import { ReactComponent as Vault } from "../../assets/vault.svg";import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loaderimport { changeSvgTheme } from "../../utils/css";+import getSymbolFromCurrency from "currency-symbol-map";const premiumDetails = [{@@ -109,10 +110,33 @@ const premiumDetails = [];function BuyDialog(props) {+ const { couponCode } = props;+ const [coupon, setCoupon] = useState(couponCode);+ const [isLoading, setIsLoading] = useState(false);+ const [error, setError] = useState();+ const [prices, setPrices] = useState();const isLoggedIn = useUserStore((store) => store.isLoggedIn);const user = useUserStore((store) => store.user);const accent = useThemeStore((store) => store.accent);+ useEffect(() => {+ (async function () {+ try {+ setIsLoading(true);+ setError();+ const data = await getCouponData(coupon);+ setPrices({+ ...data.paddlejs.vendor,+ withoutDiscount: data.total[0],+ });+ } catch (e) {+ setError(e.message);+ } finally {+ setIsLoading(false);+ }+ })();+ }, [coupon]);+return (<DialogisOpen={true}@@ -144,7 +168,7 @@ function BuyDialog(props) {opacity={0.2}fontSize={90}>- $4.49+ PRO</Text></Flex><Flex@@ -163,32 +187,180 @@ function BuyDialog(props) {</Flex>))}</Flex>- <Flex flexDirection="column" bg="shade" p={5} pt={2}>- <Text variant="heading" fontSize={32} color="primary">- Only for $4.49/mo- </Text>- <Text variant="subBody" color="text" my={1}>- {isLoggedIn- ? "Cancel anytime. No questions asked."- : "Start your 14 days free trial (no credit card required)"}- </Text>- <Button- fontSize="title"- fontWeight="bold"- onClick={async () => {- if (isLoggedIn) {- await upgrade(user);- } else {- await showSignUpDialog();- }- props.onCancel();- }}- >- Subscribe to Notesnook Pro- </Button>++ <Flex+ flexDirection="column"+ bg={error ? "errorBg" : "shade"}+ p={5}+ pt={2}+ >+ {isLoading ? (+ <Icon.Loading size={32} />+ ) : error ? (+ <>+ <Text variant="title" color="error" fontWeight="normal">+ {error}+ </Text>+ <Button+ variant="primary"+ bg="error"+ color="static"+ mt={2}+ onClick={() => setCoupon()}+ >+ Try again+ </Button>+ </>+ ) : (+ <>+ {coupon ? (+ <Flex mb={1} alignItems="center" justifyContent="space-between">+ <Flex alignItems="center" justifyContent="center">+ <Text variant="subBody" mr={1}>+ Coupon:+ </Text>+ <Text+ variant="subBody"+ fontSize={10}+ bg="primary"+ color="static"+ px={1}+ py="3px"+ sx={{ borderRadius: "default" }}+ >+ {coupon}+ </Text>+ </Flex>++ <Flex>+ <Text+ sx={{ cursor: "pointer", ":hover": { opacity: 0.8 } }}+ variant="subBody"+ color="primary"+ mr={1}+ onClick={() => {+ const code = window.prompt("Enter new coupon code:");+ setCoupon(code);+ }}+ >+ Change+ </Text>+ <Text+ sx={{ cursor: "pointer", ":hover": { opacity: 0.8 } }}+ variant="subBody"+ color="error"+ onClick={() => {+ if (+ window.confirm(+ "Are you sure you want to remove this coupon?"+ )+ ) {+ setCoupon();+ }+ }}+ >+ Remove+ </Text>+ </Flex>+ </Flex>+ ) : (+ <Text+ sx={{ cursor: "pointer", ":hover": { opacity: 0.8 } }}+ variant="subBody"+ color="primary"+ mr={1}+ onClick={() => {+ const code = window.prompt("Enter new coupon code:");+ setCoupon(code);+ }}+ >+ Add coupon code+ </Text>+ )}+ <Text variant="heading" fontSize={24} color="primary">+ Only <MainPricing prices={prices} />+ </Text>+ <RecurringPricing prices={prices} />+ <Text display="flex" variant="subBody" color="text" mt={1} mb={2}>+ {isLoggedIn+ ? "Cancel anytime. No questions asked."+ : "Start your 14 days free trial (no credit card required)"}+ </Text>+ <Button+ fontSize="title"+ fontWeight="bold"+ onClick={async () => {+ if (isLoggedIn) {+ await upgrade(user, coupon);+ } else {+ await showSignUpDialog();+ }+ props.onCancel();+ }}+ >+ Subscribe to Notesnook Pro+ </Button>+ </>+ )}</Flex></Flex></Dialog>);}export default BuyDialog;++function MainPricing(props) {+ const { prices } = props;++ if (prices.withoutDiscount.net !== prices.prices.total)+ return (+ <>+ <del>+ <Price+ currency={prices.withoutDiscount.currency}+ price={prices.withoutDiscount.net}+ />+ </del>{" "}+ <Price currency={prices.currency} price={prices.prices.total} />+ {prices.recurring_prices.total === prices.prices.total+ ? " per month"+ : " your first month"}+ </>+ );+ else+ return (+ <>+ <Price currency={prices.currency} price={prices.prices.total} />+ {" per month"}+ </>+ );+}++function RecurringPricing(props) {+ const { prices } = props;++ if (prices.prices.total === prices.recurring_prices.total) return null;+ return (+ <Text+ display="flex"+ variant="body"+ mt={1}+ fontWeight="bold"+ color="primary"+ >+ And then{" "}+ <Price currency={prices.currency} price={prices.recurring_prices.total} />{" "}+ every month afterwards.+ </Text>+ );+}++function Price(props) {+ const { currency, price } = props;+ return (+ <>+ {getSymbolFromCurrency(currency)}+ {price}+ </>+ );+}diff --git a/apps/web/src/navigation/hash-routes.js b/apps/web/src/navigation/hash-routes.jsindex 12791b8aa..4602b3f7b 100644--- a/apps/web/src/navigation/hash-routes.js+++ b/apps/web/src/navigation/hash-routes.js@@ -1,6 +1,9 @@import React from "react";import Vault from "../common/vault";-import { showEmailVerificationDialog } from "../common/dialog-controller";+import {+ showBuyDialog,+ showEmailVerificationDialog,+} from "../common/dialog-controller";import {showAddNotebookDialog,showEditNotebookDialog,@@ -102,11 +105,11 @@ const hashroutes = {"/login": () => {showLogInDialog();},+ "/buy": () => {+ showBuyDialog();+ },"/buy/:code": ({ code }) => {- hashNavigate("/", { notify: false });- const user = userStore.get().user;- if (!user || isUserPremium()) return;- upgrade(user, code);+ showBuyDialog(code);},};diff --git a/apps/web/yarn.lock b/apps/web/yarn.lockindex 150ee52bc..84f239905 100644--- a/apps/web/yarn.lock+++ b/apps/web/yarn.lock@@ -4653,6 +4653,11 @@ csstype@^3.0.2:resolved "https://registry.npmjs.org/csstype/-/csstype-3.0.6.tgz"integrity sha512-+ZAmfyWMT7TiIlzdqJgjMb7S4f1beorDbWbsocyK4RaiqA5RTX3K14bnBWmmA9QEM0gRdsjyyrEmcyga8Zsxmw==+currency-symbol-map@^5.0.1:+ version "5.0.1"+ resolved "https://registry.yarnpkg.com/currency-symbol-map/-/currency-symbol-map-5.0.1.tgz#447629c7e26730299aa6174c55bde281f682b2e5"+ integrity sha512-2yHvoVBPIS9Dx7fjritu9l7UHWo4Z3D2oQnQj8AYwUee2j/PAcoHB4issCqwabfbipISZ/iG6vVYsfrAoks2eA==+cwd@^0.10.0:version "0.10.0"resolved "https://registry.npmjs.org/cwd/-/cwd-0.10.0.tgz"