From 77e4edab9b4f71acae471972fcbce000e84d2c44 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Mon, 9 Jan 2023 09:41:58 +0800 Subject: [PATCH] refactor: price --- package.json | 2 +- src/app/config.ts | 20 ++++++++++++------- src/common/component/styled/Textarea.tsx | 2 +- .../setting/License/LicensePriceListModal.tsx | 19 +++++++++--------- src/types/common.ts | 10 ++++++++++ src/types/server.ts | 3 ++- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index d31f3fab..b1823502 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vocechat-web", - "version": "0.3.30", + "version": "0.3.31", "private": true, "homepage": "https://voce.chat", "dependencies": { diff --git a/src/app/config.ts b/src/app/config.ts index 2d4c0627..b3ccd1c3 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -1,29 +1,35 @@ // const BASE_URL = `${location.origin}/api`; + +import { Price } from "../types/common"; + // const BASE_URL = `http://localhost:3333/api`; const BASE_URL = process.env.REACT_APP_RELEASE ? `${location.origin}/api` : `https://dev.voce.chat/api`; -let prices = [ + +let prices: Price[] = [ { - mode: "subscription", - sub_type: "year", //day month year + type: "subscription", + // sub_type: "year", //day month year title: "VoceChat Pro", limit: 100, - pid: "price_1MMMWAGGoUDRyc3jIsMay5Rs", + pid: "price_1MN8C7GGoUDRyc3jos06bCqM", desc: "User Limit: 100" }, { + type: "payment", title: "VoceChat Supreme", limit: 99999, pid: "price_1M5VoGGGoUDRyc3j6xhQou6D", desc: "User Limit: No Limit" } ]; -export const LicensePriceList = +export const LicensePriceList: Price[] = process.env.NODE_ENV === "development" ? [ ...prices, { + type: "payment", title: "VoceChat Enterprise", limit: 99999, pid: "price_1LkQGpGGoUDRyc3jGTh3GYHw", @@ -34,8 +40,8 @@ export const LicensePriceList = limit: 100, pid: "price_1MMNNCGGoUDRyc3jSIGIsb3C", desc: "test subscription price", - mode: "subscription", - sub_type: "year", //day month year + type: "subscription", + sub_dur: "year", //day month year }, ] : prices; diff --git a/src/common/component/styled/Textarea.tsx b/src/common/component/styled/Textarea.tsx index 071b0f37..b3788950 100644 --- a/src/common/component/styled/Textarea.tsx +++ b/src/common/component/styled/Textarea.tsx @@ -2,6 +2,6 @@ import { TextareaHTMLAttributes } from "react"; type Props = TextareaHTMLAttributes const StyledTextarea = ({ className, ...rest }: Props) => { - return ; + return ; }; export default StyledTextarea; diff --git a/src/routes/setting/License/LicensePriceListModal.tsx b/src/routes/setting/License/LicensePriceListModal.tsx index c0e81cc4..c6709cb8 100644 --- a/src/routes/setting/License/LicensePriceListModal.tsx +++ b/src/routes/setting/License/LicensePriceListModal.tsx @@ -9,19 +9,19 @@ import { useGetLicensePaymentUrlMutation } from "../../../app/services/server"; import { LicensePriceList } from "../../../app/config"; import { useTranslation } from "react-i18next"; import dayjs from "dayjs"; +import { PriceSubscriptionDuration, PriceType } from "../../../types/common"; // import { LicenseMetadata, RenewLicense } from "../../types/server"; interface Props { closeModal: () => void; // domain: string; } -type Mode = "subscription" | "payment"; -type SubType = "month" | "quarter" | "year"; -const getExpireDay = (sub_type: SubType) => { + +const getExpireDay = (sub_dur: PriceSubscriptionDuration) => { const currDate = dayjs(); // 默认10年 let res = currDate; - switch (sub_type) { + switch (sub_dur) { case "year": res = currDate.add(1, "year"); break; @@ -31,7 +31,6 @@ const getExpireDay = (sub_type: SubType) => { case "quarter": res = currDate.add(3, "month"); break; - default: break; }; @@ -42,16 +41,16 @@ const LicensePriceListModal: FC = ({ closeModal }) => { const { t: ct } = useTranslation(); const [getUrl, { isLoading, isSuccess }] = useGetLicensePaymentUrlMutation(); const [selectPrice, setSelectPrice] = useState( - `${LicensePriceList[0].pid}|${LicensePriceList[0].limit}|${LicensePriceList[0].mode || "payment"}|${LicensePriceList[0].sub_type || ""}` + `${LicensePriceList[0].pid}|${LicensePriceList[0].limit}|${LicensePriceList[0].type}|${LicensePriceList[0].sub_dur || ""}` ); const handleRenew = async () => { - const [priceId, user_limit, mode, sub_type = "month"] = selectPrice.split("|") as [string, string, Mode, SubType]; + const [priceId, user_limit, type, sub_dur = "month"] = selectPrice.split("|") as [string, string, PriceType, PriceSubscriptionDuration]; const resp = await getUrl({ - mode: mode, + type, priceId, metadata: { user_limit: Number(user_limit), - expire: mode == "subscription" ? getExpireDay(sub_type) : "2035-01-01", + expire: type == "subscription" ? getExpireDay(sub_dur) : getExpireDay("year"), // 本地,则*通配符 domain: location.hostname.startsWith("localhost") ? "*" : location.hostname }, @@ -87,7 +86,7 @@ const LicensePriceListModal: FC = ({ closeModal }) => { > `${title} [${desc}]`)} - values={LicensePriceList.map(({ pid, limit, mode = "payment", sub_type = "month" }) => `${pid}|${limit}|${mode}|${sub_type}`)} + values={LicensePriceList.map(({ pid, limit, type = "payment", sub_dur = "month" }) => `${pid}|${limit}|${type}|${sub_dur}`)} value={selectPrice} onChange={(v) => { console.log("wtff", v); diff --git a/src/types/common.ts b/src/types/common.ts index 5e631bd5..8b133e14 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,3 +1,13 @@ export interface EntityId { id: number; } +export type PriceType = "subscription" | "payment"; +export type PriceSubscriptionDuration = "month" | "quarter" | "year"; +export type Price = { + title: string, + limit: number, + pid: string, + desc: string, + type: PriceType, + sub_dur?: PriceSubscriptionDuration +} diff --git a/src/types/server.ts b/src/types/server.ts index 8bfb72ec..beb0d2d9 100644 --- a/src/types/server.ts +++ b/src/types/server.ts @@ -1,4 +1,5 @@ // call `organization` in backend +import { PriceType } from "./common"; import { User } from "./user"; export interface Server { @@ -74,7 +75,7 @@ export interface LicenseMetadata { domain: string | string[]; } export interface RenewLicense { - mode: "payment" | "subscription", + type: PriceType, priceId: string; metadata: LicenseMetadata; cancel_url: string;