@greatapps/common 1.1.242 → 1.1.244
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/modals/BuyCreditsModal.mjs +23 -18
- package/dist/components/modals/BuyCreditsModal.mjs.map +1 -1
- package/dist/modules/subscriptions/services/subscriptions.service.mjs +0 -1
- package/dist/modules/subscriptions/services/subscriptions.service.mjs.map +1 -1
- package/dist/modules/subscriptions/utils/build-plan-extras.mjs +15 -10
- package/dist/modules/subscriptions/utils/build-plan-extras.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/modals/BuyCreditsModal.tsx +28 -20
- package/src/modules/subscriptions/services/subscriptions.service.ts +0 -2
- package/src/modules/subscriptions/utils/build-plan-extras.ts +22 -17
|
@@ -26,7 +26,7 @@ import { useUpdateSubscriptionPlan } from "../../modules/subscriptions/hooks/upd
|
|
|
26
26
|
import { buildPlanExtras } from "../../modules/subscriptions/utils/build-plan-extras";
|
|
27
27
|
import { getPriceFromCalculatedData } from "../../modules/subscriptions/utils/periodicity";
|
|
28
28
|
import { IconInfoCircle, IconX, IconLoader2 } from "@tabler/icons-react";
|
|
29
|
-
import { useCallback, useEffect, useMemo } from "react";
|
|
29
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
30
30
|
import { useForm } from "react-hook-form";
|
|
31
31
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
32
32
|
import z from "zod";
|
|
@@ -64,25 +64,30 @@ function BuyCreditsModal() {
|
|
|
64
64
|
selectedCardId: cardsData?.data?.[0]?.id.toString() ?? ""
|
|
65
65
|
}
|
|
66
66
|
});
|
|
67
|
+
const hasInitialized = useRef(false);
|
|
67
68
|
useEffect(() => {
|
|
68
|
-
if (open) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
selectedCardId: form.getValues("selectedCardId") || cardsData?.data?.[0]?.id.toString() || ""
|
|
72
|
-
});
|
|
73
|
-
calculate({
|
|
74
|
-
id_product: 1,
|
|
75
|
-
...planId != null && { id_plan: planId },
|
|
76
|
-
...subscriptionId != null && { id_subscription: subscriptionId },
|
|
77
|
-
plan_extras: buildPlanExtras(subscription?.items ?? [], [
|
|
78
|
-
{
|
|
79
|
-
id_addon: ADDON_IDS.AI,
|
|
80
|
-
quantity: currentCredits,
|
|
81
|
-
currency: "BRL"
|
|
82
|
-
}
|
|
83
|
-
])
|
|
84
|
-
});
|
|
69
|
+
if (!open) {
|
|
70
|
+
hasInitialized.current = false;
|
|
71
|
+
return;
|
|
85
72
|
}
|
|
73
|
+
if (hasInitialized.current || !subscription) return;
|
|
74
|
+
hasInitialized.current = true;
|
|
75
|
+
form.reset({
|
|
76
|
+
creditOption: currentCredits,
|
|
77
|
+
selectedCardId: form.getValues("selectedCardId") || cardsData?.data?.[0]?.id.toString() || ""
|
|
78
|
+
});
|
|
79
|
+
calculate({
|
|
80
|
+
id_product: 1,
|
|
81
|
+
...planId != null && { id_plan: planId },
|
|
82
|
+
...subscriptionId != null && { id_subscription: subscriptionId },
|
|
83
|
+
plan_extras: buildPlanExtras(subscription?.items ?? [], [
|
|
84
|
+
{
|
|
85
|
+
id_addon: ADDON_IDS.AI,
|
|
86
|
+
quantity: currentCredits,
|
|
87
|
+
currency: "BRL"
|
|
88
|
+
}
|
|
89
|
+
])
|
|
90
|
+
});
|
|
86
91
|
}, [open, subscription, currentCredits, cardsData, form, calculate, planId, subscriptionId]);
|
|
87
92
|
const creditOption = form.watch("creditOption");
|
|
88
93
|
const selectedCardId = form.watch("selectedCardId");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/components/modals/BuyCreditsModal.tsx"],"sourcesContent":["'use client';\n\nimport {\n Dialog,\n DialogContent,\n DialogHeader,\n DialogTitle,\n} from '../ui/overlay/Dialog';\nimport { Button } from '../ui/buttons/Button';\nimport { Separator } from '../ui/data-display/Separator';\nimport { Toast } from '../ui/feedback/Toast';\nimport { SelectField } from '../ui/form/SelectField';\nimport { Skeleton } from '../ui/feedback/Skeleton';\nimport { PaymentInfoCard } from '../ui/data-display/PaymentInfoCard';\nimport { ADDON_IDS } from '../../modules/subscriptions/constants/addons.constants';\nimport {\n AI_CREDIT_OPTIONS,\n AI_CREDIT_VALUES,\n} from '../../modules/subscriptions/constants/addons.constants';\nimport { useBuyCreditsModal } from '../../store/useBuyCreditsModal';\nimport { useModalManager } from '../../store/useModalManager';\nimport { useCards } from '../../modules/cards/hooks/cards.hook';\nimport { useActiveSubscription } from '../../modules/subscriptions/hooks/find-active-subscription.hook';\nimport { useCalculateSubscription } from '../../modules/subscriptions/hooks/calculate-subscription.hook';\nimport { useUpdateSubscriptionPlan } from '../../modules/subscriptions/hooks/update-subscription-plan.hook';\nimport { buildPlanExtras } from '../../modules/subscriptions/utils/build-plan-extras';\nimport { getPriceFromCalculatedData } from '../../modules/subscriptions/utils/periodicity';\nimport { IconInfoCircle, IconX, IconLoader2 } from '@tabler/icons-react';\nimport { useCallback, useEffect, useMemo } from 'react';\nimport { useForm } from 'react-hook-form';\nimport { zodResolver } from '@hookform/resolvers/zod';\nimport z from 'zod';\nimport { toast } from 'sonner';\n\nconst buyCreditSchema = z.object({\n creditOption: z.number().refine((val) => AI_CREDIT_VALUES.has(val), {\n message: 'Selecione uma opção de créditos',\n }),\n selectedCardId: z.string().min(1, 'Selecione um cartão'),\n});\n\ntype BuyCreditFormData = z.infer<typeof buyCreditSchema>;\n\nexport default function BuyCreditsModal() {\n const { open, closeModal } = useBuyCreditsModal();\n const { openModal } = useModalManager();\n const { data: cardsData } = useCards();\n const { data: subscriptionData } = useActiveSubscription();\n const subscription = subscriptionData?.data?.[0] ?? null;\n\n const {\n mutate: calculate,\n isPending: isCalculatePending,\n data: calculateData,\n } = useCalculateSubscription();\n const updatePlanMutation = useUpdateSubscriptionPlan();\n\n const aiItem = useMemo(\n () => subscription?.items?.find((item) => item.id_addon === ADDON_IDS.AI),\n [subscription]\n );\n\n const currentCredits = aiItem?.quantity ?? 0;\n const subscriptionId = subscription?.id;\n const planId = subscription?.id_plan;\n\n const form = useForm<BuyCreditFormData>({\n resolver: zodResolver(buyCreditSchema),\n mode: 'onChange',\n defaultValues: {\n creditOption: currentCredits,\n selectedCardId: cardsData?.data?.[0]?.id.toString() ?? '',\n },\n });\n\n useEffect(() => {\n if (open) {\n form.reset({\n creditOption: currentCredits,\n selectedCardId:\n form.getValues('selectedCardId') || cardsData?.data?.[0]?.id.toString() || '',\n });\n\n calculate({\n id_product: 1,\n ...(planId != null && { id_plan: planId }),\n ...(subscriptionId != null && { id_subscription: subscriptionId }),\n plan_extras: buildPlanExtras(subscription?.items ?? [], [\n {\n id_addon: ADDON_IDS.AI,\n quantity: currentCredits,\n currency: 'BRL',\n },\n ]),\n });\n }\n }, [open, subscription, currentCredits, cardsData, form, calculate, planId, subscriptionId]);\n\n const creditOption = form.watch('creditOption');\n const selectedCardId = form.watch('selectedCardId');\n const { isDirty, isSubmitting, isValid } = form.formState;\n\n const subscriptionItems = useMemo(() => subscription?.items ?? [], [subscription]);\n\n const handleCreditOptionChange = useCallback(\n (value: string | number | boolean) => {\n const credits = Number(value);\n\n form.setValue('creditOption', credits, {\n shouldValidate: true,\n shouldDirty: true,\n });\n\n const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === credits);\n if (!option) return;\n\n calculate({\n id_product: 1,\n ...(planId != null && { id_plan: planId }),\n ...(subscriptionId != null && { id_subscription: subscriptionId }),\n plan_extras: buildPlanExtras(subscriptionItems, [\n {\n id_addon: ADDON_IDS.AI,\n quantity: option.credits,\n currency: 'BRL',\n },\n ]),\n });\n },\n [form, subscriptionId, planId, subscriptionItems, calculate]\n );\n\n const calculatedData = calculateData?.data ?? null;\n\n const totalPrice = useMemo(() => {\n if (!calculatedData) return null;\n return getPriceFromCalculatedData(calculatedData, subscription?.periodicity);\n }, [calculatedData, subscription]);\n\n const formatPrice = useCallback((price: number) => {\n return price.toLocaleString('pt-BR', {\n style: 'currency',\n currency: 'BRL',\n });\n }, []);\n\n const isConfirmDisabled = !isDirty || !isValid || isCalculatePending || isSubmitting;\n\n const handleClose = useCallback(() => {\n form.reset();\n closeModal();\n }, [form, closeModal]);\n\n const handleOpenAddCardModal = useCallback(() => {\n openModal('addCardModal', {\n returnToBuyCredits: true,\n buyCreditsState: {\n activeCardId: selectedCardId,\n selectedValue: creditOption,\n },\n });\n }, [openModal, selectedCardId, creditOption]);\n\n const selectOptions = useMemo(() => {\n return AI_CREDIT_OPTIONS.map((opt) => ({\n value: opt.credits,\n label: (\n <div className=\"flex items-center justify-between gap-2 w-full\">\n <span className=\"paragraph-small-medium text-zinc-950 flex-1\">\n {opt.credits.toLocaleString('pt-BR')} créditos de IA\n </span>\n <span className=\"paragraph-small-medium text-zinc-500 opacity-70\">{opt.priceLabel}</span>\n </div>\n ),\n displayValue: `${opt.credits.toLocaleString('pt-BR')} créditos de IA`,\n }));\n }, []);\n\n const onSubmit = useCallback(\n async (data: BuyCreditFormData) => {\n if (!subscriptionId) return;\n\n const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === data.creditOption);\n if (!option) return;\n\n await updatePlanMutation.mutateAsync({\n subscriptionId,\n data: {\n plan_extras: [\n {\n id_addon: ADDON_IDS.AI,\n quantity: option.credits,\n },\n ],\n },\n });\n\n toast.custom((t) => (\n <Toast\n variant=\"success\"\n message={`Créditos de IA alterados para ${data.creditOption.toLocaleString('pt-BR')}`}\n toastId={t}\n />\n ));\n form.reset();\n closeModal();\n },\n [subscriptionId, updatePlanMutation, form, closeModal]\n );\n\n const renderPriceDisplay = useCallback(() => {\n if (isCalculatePending) {\n return <Skeleton className=\"h-7 w-24\" />;\n }\n if (totalPrice !== null) {\n return (\n <span className=\"paragraph-xlarge-semibold text-zinc-950\">\n {totalPrice === 0 ? 'Incluso' : formatPrice(totalPrice)}\n </span>\n );\n }\n return <span className=\"paragraph-xlarge-semibold text-zinc-400\">--</span>;\n }, [isCalculatePending, totalPrice, formatPrice]);\n\n return (\n <Dialog open={open} onOpenChange={handleClose}>\n <DialogContent\n showCloseButton={false}\n className=\"flex flex-col p-0! gap-0! max-w-full! border-0 rounded-t-2xl rounded-b-none h-dvh top-0! bottom-0! left-0! right-0! translate-x-0! translate-y-0! lg:max-w-[433px]! lg:h-auto! lg:max-h-[90vh] lg:rounded-lg lg:border lg:top-[50%]! lg:left-[50%]! lg:right-auto! lg:bottom-auto! lg:translate-x-[-50%]! lg:translate-y-[-50%]! overflow-hidden lg:overflow-y-auto\"\n >\n <DialogHeader className=\"px-4 py-3 border-b border-zinc-200 shrink-0 relative\">\n <DialogTitle className=\"paragraph-medium-semibold text-zinc-950 text-center\">\n Comprar créditos de IA\n </DialogTitle>\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute right-3 top-1/2 -translate-y-1/2 size-8 flex items-center justify-center rounded-lg cursor-pointer hover:bg-zinc-100 transition-colors\"\n >\n <IconX size={18} className=\"text-zinc-400\" />\n </button>\n </DialogHeader>\n\n <form\n onSubmit={form.handleSubmit(onSubmit)}\n className=\"flex flex-col flex-1 lg:flex-none overflow-hidden\"\n >\n <div className=\"flex flex-col gap-6 lg:gap-8 p-4 lg:p-5 flex-1 lg:flex-none overflow-y-auto\">\n <div className=\"flex flex-col gap-4\">\n <SelectField\n options={selectOptions}\n value={creditOption}\n onChange={handleCreditOptionChange}\n className=\"h-12!\"\n />\n\n <PaymentInfoCard\n selectedCardId={selectedCardId}\n onCardSelect={(cardId) =>\n form.setValue('selectedCardId', cardId, { shouldValidate: true })\n }\n onOpenAddCard={handleOpenAddCardModal}\n />\n\n <div className=\"flex items-center gap-3 p-4 bg-cyan-50 rounded-lg\">\n <IconInfoCircle className=\"size-4 text-zinc-950 shrink-0\" />\n <span className=\"paragraph-small-regular text-zinc-950\">\n As alterações entrarão em vigor a partir da sua próxima fatura.\n </span>\n </div>\n </div>\n\n <Separator className=\"bg-zinc-200 hidden lg:block\" />\n\n <div className=\"hidden lg:flex items-center justify-between\">\n <span className=\"paragraph-small-medium text-zinc-500\">Valor total do plano</span>\n {renderPriceDisplay()}\n </div>\n\n <Button\n type=\"submit\"\n disabled={isConfirmDisabled}\n className=\"w-full hidden lg:inline-flex\"\n >\n {isSubmitting && <IconLoader2 className=\"size-4 animate-spin\" />}\n Confirmar e pagar\n </Button>\n </div>\n\n <div className=\"flex flex-col gap-4 p-4 border-t border-zinc-200 shrink-0 lg:hidden\">\n <div className=\"flex items-center justify-between\">\n <span className=\"paragraph-small-medium text-zinc-500\">Valor total do plano</span>\n {renderPriceDisplay()}\n </div>\n <Button type=\"submit\" disabled={isConfirmDisabled} className=\"w-full\">\n {isSubmitting && <IconLoader2 className=\"size-4 animate-spin\" />}\n Confirmar e pagar\n </Button>\n </div>\n </form>\n </DialogContent>\n </Dialog>\n );\n}\n"],"mappings":";AAwKU,SAGA,KAHA;AAtKV;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,cAAc;AACvB,SAAS,iBAAiB;AAC1B,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AACzB,SAAS,6BAA6B;AACtC,SAAS,gCAAgC;AACzC,SAAS,iCAAiC;AAC1C,SAAS,uBAAuB;AAChC,SAAS,kCAAkC;AAC3C,SAAS,gBAAgB,OAAO,mBAAmB;AACnD,SAAS,aAAa,WAAW,eAAe;AAChD,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,OAAO,OAAO;AACd,SAAS,aAAa;AAEtB,MAAM,kBAAkB,EAAE,OAAO;AAAA,EAC/B,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,QAAQ,iBAAiB,IAAI,GAAG,GAAG;AAAA,IAClE,SAAS;AAAA,EACX,CAAC;AAAA,EACD,gBAAgB,EAAE,OAAO,EAAE,IAAI,GAAG,wB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AA,sBAElE,eAAe;AAAA;AAAA,kBACjB;AAAA,kBAEA,qBAAC,SAAI,WAAU,qDACb;AAAA,wCAAC,kBAAe,WAAU,iCAAgC;AAAA,oBAC1D,oBAAC,UAAK,WAAU,yCAAwC,yFAExD;AAAA,qBACF;AAAA,mBACF;AAAA,gBAEA,oBAAC,aAAU,WAAU,+BAA8B;AAAA,gBAEnD,qBAAC,SAAI,WAAU,+CACb;AAAA,sCAAC,UAAK,WAAU,wCAAuC,kCAAoB;AAAA,kBAC1E,mBAAmB;AAAA,mBACtB;AAAA,gBAEA;AAAA,kBAAC;AAAA;AAAA,oBACC,MAAK;AAAA,oBACL,UAAU;AAAA,oBACV,WAAU;AAAA,oBAET;AAAA,sCAAgB,oBAAC,eAAY,WAAU,uBAAsB;AAAA,sBAAG;AAAA;AAAA;AAAA,gBAEnE;AAAA,iBACF;AAAA,cAEA,qBAAC,SAAI,WAAU,uEACb;AAAA,qCAAC,SAAI,WAAU,qCACb;AAAA,sCAAC,UAAK,WAAU,wCAAuC,kCAAoB;AAAA,kBAC1E,mBAAmB;AAAA,mBACtB;AAAA,gBACA,qBAAC,UAAO,MAAK,UAAS,UAAU,mBAAmB,WAAU,UAC1D;AAAA,kCAAgB,oBAAC,eAAY,WAAU,uBAAsB;AAAA,kBAAG;AAAA,mBAEnE;AAAA,iBACF;AAAA;AAAA;AAAA,QACF;AAAA;AAAA;AAAA,EACF,GACF;AAEJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/components/modals/BuyCreditsModal.tsx"],"sourcesContent":["'use client';\n\nimport {\n Dialog,\n DialogContent,\n DialogHeader,\n DialogTitle,\n} from '../ui/overlay/Dialog';\nimport { Button } from '../ui/buttons/Button';\nimport { Separator } from '../ui/data-display/Separator';\nimport { Toast } from '../ui/feedback/Toast';\nimport { SelectField } from '../ui/form/SelectField';\nimport { Skeleton } from '../ui/feedback/Skeleton';\nimport { PaymentInfoCard } from '../ui/data-display/PaymentInfoCard';\nimport { ADDON_IDS } from '../../modules/subscriptions/constants/addons.constants';\nimport {\n AI_CREDIT_OPTIONS,\n AI_CREDIT_VALUES,\n} from '../../modules/subscriptions/constants/addons.constants';\nimport { useBuyCreditsModal } from '../../store/useBuyCreditsModal';\nimport { useModalManager } from '../../store/useModalManager';\nimport { useCards } from '../../modules/cards/hooks/cards.hook';\nimport { useActiveSubscription } from '../../modules/subscriptions/hooks/find-active-subscription.hook';\nimport { useCalculateSubscription } from '../../modules/subscriptions/hooks/calculate-subscription.hook';\nimport { useUpdateSubscriptionPlan } from '../../modules/subscriptions/hooks/update-subscription-plan.hook';\nimport { buildPlanExtras } from '../../modules/subscriptions/utils/build-plan-extras';\nimport { getPriceFromCalculatedData } from '../../modules/subscriptions/utils/periodicity';\nimport { IconInfoCircle, IconX, IconLoader2 } from '@tabler/icons-react';\nimport { useCallback, useEffect, useMemo, useRef } from 'react';\nimport { useForm } from 'react-hook-form';\nimport { zodResolver } from '@hookform/resolvers/zod';\nimport z from 'zod';\nimport { toast } from 'sonner';\n\nconst buyCreditSchema = z.object({\n creditOption: z.number().refine((val) => AI_CREDIT_VALUES.has(val), {\n message: 'Selecione uma opção de créditos',\n }),\n selectedCardId: z.string().min(1, 'Selecione um cartão'),\n});\n\ntype BuyCreditFormData = z.infer<typeof buyCreditSchema>;\n\nexport default function BuyCreditsModal() {\n const { open, closeModal } = useBuyCreditsModal();\n const { openModal } = useModalManager();\n const { data: cardsData } = useCards();\n const { data: subscriptionData } = useActiveSubscription();\n const subscription = subscriptionData?.data?.[0] ?? null;\n\n const {\n mutate: calculate,\n isPending: isCalculatePending,\n data: calculateData,\n } = useCalculateSubscription();\n const updatePlanMutation = useUpdateSubscriptionPlan();\n\n const aiItem = useMemo(\n () => subscription?.items?.find((item) => item.id_addon === ADDON_IDS.AI),\n [subscription]\n );\n\n const currentCredits = aiItem?.quantity ?? 0;\n const subscriptionId = subscription?.id;\n const planId = subscription?.id_plan;\n\n const form = useForm<BuyCreditFormData>({\n resolver: zodResolver(buyCreditSchema),\n mode: 'onChange',\n defaultValues: {\n creditOption: currentCredits,\n selectedCardId: cardsData?.data?.[0]?.id.toString() ?? '',\n },\n });\n\n const hasInitialized = useRef(false);\n\n useEffect(() => {\n if (!open) {\n hasInitialized.current = false;\n return;\n }\n\n if (hasInitialized.current || !subscription) return;\n hasInitialized.current = true;\n\n form.reset({\n creditOption: currentCredits,\n selectedCardId:\n form.getValues('selectedCardId') || cardsData?.data?.[0]?.id.toString() || '',\n });\n\n calculate({\n id_product: 1,\n ...(planId != null && { id_plan: planId }),\n ...(subscriptionId != null && { id_subscription: subscriptionId }),\n plan_extras: buildPlanExtras(subscription?.items ?? [], [\n {\n id_addon: ADDON_IDS.AI,\n quantity: currentCredits,\n currency: 'BRL',\n },\n ]),\n });\n }, [open, subscription, currentCredits, cardsData, form, calculate, planId, subscriptionId]);\n\n const creditOption = form.watch('creditOption');\n const selectedCardId = form.watch('selectedCardId');\n const { isDirty, isSubmitting, isValid } = form.formState;\n\n const subscriptionItems = useMemo(() => subscription?.items ?? [], [subscription]);\n\n const handleCreditOptionChange = useCallback(\n (value: string | number | boolean) => {\n const credits = Number(value);\n\n form.setValue('creditOption', credits, {\n shouldValidate: true,\n shouldDirty: true,\n });\n\n const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === credits);\n if (!option) return;\n\n calculate({\n id_product: 1,\n ...(planId != null && { id_plan: planId }),\n ...(subscriptionId != null && { id_subscription: subscriptionId }),\n plan_extras: buildPlanExtras(subscriptionItems, [\n {\n id_addon: ADDON_IDS.AI,\n quantity: option.credits,\n currency: 'BRL',\n },\n ]),\n });\n },\n [form, subscriptionId, planId, subscriptionItems, calculate]\n );\n\n const calculatedData = calculateData?.data ?? null;\n\n const totalPrice = useMemo(() => {\n if (!calculatedData) return null;\n return getPriceFromCalculatedData(calculatedData, subscription?.periodicity);\n }, [calculatedData, subscription]);\n\n const formatPrice = useCallback((price: number) => {\n return price.toLocaleString('pt-BR', {\n style: 'currency',\n currency: 'BRL',\n });\n }, []);\n\n const isConfirmDisabled = !isDirty || !isValid || isCalculatePending || isSubmitting;\n\n const handleClose = useCallback(() => {\n form.reset();\n closeModal();\n }, [form, closeModal]);\n\n const handleOpenAddCardModal = useCallback(() => {\n openModal('addCardModal', {\n returnToBuyCredits: true,\n buyCreditsState: {\n activeCardId: selectedCardId,\n selectedValue: creditOption,\n },\n });\n }, [openModal, selectedCardId, creditOption]);\n\n const selectOptions = useMemo(() => {\n return AI_CREDIT_OPTIONS.map((opt) => ({\n value: opt.credits,\n label: (\n <div className=\"flex items-center justify-between gap-2 w-full\">\n <span className=\"paragraph-small-medium text-zinc-950 flex-1\">\n {opt.credits.toLocaleString('pt-BR')} créditos de IA\n </span>\n <span className=\"paragraph-small-medium text-zinc-500 opacity-70\">{opt.priceLabel}</span>\n </div>\n ),\n displayValue: `${opt.credits.toLocaleString('pt-BR')} créditos de IA`,\n }));\n }, []);\n\n const onSubmit = useCallback(\n async (data: BuyCreditFormData) => {\n if (!subscriptionId) return;\n\n const option = AI_CREDIT_OPTIONS.find((opt) => opt.credits === data.creditOption);\n if (!option) return;\n\n await updatePlanMutation.mutateAsync({\n subscriptionId,\n data: {\n plan_extras: [\n {\n id_addon: ADDON_IDS.AI,\n quantity: option.credits,\n },\n ],\n },\n });\n\n toast.custom((t) => (\n <Toast\n variant=\"success\"\n message={`Créditos de IA alterados para ${data.creditOption.toLocaleString('pt-BR')}`}\n toastId={t}\n />\n ));\n form.reset();\n closeModal();\n },\n [subscriptionId, updatePlanMutation, form, closeModal]\n );\n\n const renderPriceDisplay = useCallback(() => {\n if (isCalculatePending) {\n return <Skeleton className=\"h-7 w-24\" />;\n }\n if (totalPrice !== null) {\n return (\n <span className=\"paragraph-xlarge-semibold text-zinc-950\">\n {totalPrice === 0 ? 'Incluso' : formatPrice(totalPrice)}\n </span>\n );\n }\n return <span className=\"paragraph-xlarge-semibold text-zinc-400\">--</span>;\n }, [isCalculatePending, totalPrice, formatPrice]);\n\n return (\n <Dialog open={open} onOpenChange={handleClose}>\n <DialogContent\n showCloseButton={false}\n className=\"flex flex-col p-0! gap-0! max-w-full! border-0 rounded-t-2xl rounded-b-none h-dvh top-0! bottom-0! left-0! right-0! translate-x-0! translate-y-0! lg:max-w-[433px]! lg:h-auto! lg:max-h-[90vh] lg:rounded-lg lg:border lg:top-[50%]! lg:left-[50%]! lg:right-auto! lg:bottom-auto! lg:translate-x-[-50%]! lg:translate-y-[-50%]! overflow-hidden lg:overflow-y-auto\"\n >\n <DialogHeader className=\"px-4 py-3 border-b border-zinc-200 shrink-0 relative\">\n <DialogTitle className=\"paragraph-medium-semibold text-zinc-950 text-center\">\n Comprar créditos de IA\n </DialogTitle>\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute right-3 top-1/2 -translate-y-1/2 size-8 flex items-center justify-center rounded-lg cursor-pointer hover:bg-zinc-100 transition-colors\"\n >\n <IconX size={18} className=\"text-zinc-400\" />\n </button>\n </DialogHeader>\n\n <form\n onSubmit={form.handleSubmit(onSubmit)}\n className=\"flex flex-col flex-1 lg:flex-none overflow-hidden\"\n >\n <div className=\"flex flex-col gap-6 lg:gap-8 p-4 lg:p-5 flex-1 lg:flex-none overflow-y-auto\">\n <div className=\"flex flex-col gap-4\">\n <SelectField\n options={selectOptions}\n value={creditOption}\n onChange={handleCreditOptionChange}\n className=\"h-12!\"\n />\n\n <PaymentInfoCard\n selectedCardId={selectedCardId}\n onCardSelect={(cardId) =>\n form.setValue('selectedCardId', cardId, { shouldValidate: true })\n }\n onOpenAddCard={handleOpenAddCardModal}\n />\n\n <div className=\"flex items-center gap-3 p-4 bg-cyan-50 rounded-lg\">\n <IconInfoCircle className=\"size-4 text-zinc-950 shrink-0\" />\n <span className=\"paragraph-small-regular text-zinc-950\">\n As alterações entrarão em vigor a partir da sua próxima fatura.\n </span>\n </div>\n </div>\n\n <Separator className=\"bg-zinc-200 hidden lg:block\" />\n\n <div className=\"hidden lg:flex items-center justify-between\">\n <span className=\"paragraph-small-medium text-zinc-500\">Valor total do plano</span>\n {renderPriceDisplay()}\n </div>\n\n <Button\n type=\"submit\"\n disabled={isConfirmDisabled}\n className=\"w-full hidden lg:inline-flex\"\n >\n {isSubmitting && <IconLoader2 className=\"size-4 animate-spin\" />}\n Confirmar e pagar\n </Button>\n </div>\n\n <div className=\"flex flex-col gap-4 p-4 border-t border-zinc-200 shrink-0 lg:hidden\">\n <div className=\"flex items-center justify-between\">\n <span className=\"paragraph-small-medium text-zinc-500\">Valor total do plano</span>\n {renderPriceDisplay()}\n </div>\n <Button type=\"submit\" disabled={isConfirmDisabled} className=\"w-full\">\n {isSubmitting && <IconLoader2 className=\"size-4 animate-spin\" />}\n Confirmar e pagar\n </Button>\n </div>\n </form>\n </DialogContent>\n </Dialog>\n );\n}\n"],"mappings":";AAgLU,SAGA,KAHA;AA9KV;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,cAAc;AACvB,SAAS,iBAAiB;AAC1B,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AACzB,SAAS,6BAA6B;AACtC,SAAS,gCAAgC;AACzC,SAAS,iCAAiC;AAC1C,SAAS,uBAAuB;AAChC,SAAS,kCAAkC;AAC3C,SAAS,gBAAgB,OAAO,mBAAmB;AACnD,SAAS,aAAa,WAAW,SAAS,cAAc;AACxD,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,OAAO,OAAO;AACd,SAAS,aAAa;AAEtB,MAAM,kBAAkB,EAAE,OAAO;AAAA,EAC/B,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,QAAQ,iBAAiB,IAAI,GAAG,GAAG;AAAA,IAClE,SAAS;AAAA,EACX,CAAC;AAAA,EACD,gBAAgB,EAAE,OA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A;AAAA,kBACZ;AAAA,kBAEA;AAAA,oBAAC;AAAA;AAAA,sBACC;AAAA,sBACA,cAAc,CAAC,WACb,KAAK,SAAS,kBAAkB,QAAQ,EAAE,gBAAgB,KAAK,CAAC;AAAA,sBAElE,eAAe;AAAA;AAAA,kBACjB;AAAA,kBAEA,qBAAC,SAAI,WAAU,qDACb;AAAA,wCAAC,kBAAe,WAAU,iCAAgC;AAAA,oBAC1D,oBAAC,UAAK,WAAU,yCAAwC,yFAExD;AAAA,qBACF;AAAA,mBACF;AAAA,gBAEA,oBAAC,aAAU,WAAU,+BAA8B;AAAA,gBAEnD,qBAAC,SAAI,WAAU,+CACb;AAAA,sCAAC,UAAK,WAAU,wCAAuC,kCAAoB;AAAA,kBAC1E,mBAAmB;AAAA,mBACtB;AAAA,gBAEA;AAAA,kBAAC;AAAA;AAAA,oBACC,MAAK;AAAA,oBACL,UAAU;AAAA,oBACV,WAAU;AAAA,oBAET;AAAA,sCAAgB,oBAAC,eAAY,WAAU,uBAAsB;AAAA,sBAAG;AAAA;AAAA;AAAA,gBAEnE;AAAA,iBACF;AAAA,cAEA,qBAAC,SAAI,WAAU,uEACb;AAAA,qCAAC,SAAI,WAAU,qCACb;AAAA,sCAAC,UAAK,WAAU,wCAAuC,kCAAoB;AAAA,kBAC1E,mBAAmB;AAAA,mBACtB;AAAA,gBACA,qBAAC,UAAO,MAAK,UAAS,UAAU,mBAAmB,WAAU,UAC1D;AAAA,kCAAgB,oBAAC,eAAY,WAAU,uBAAsB;AAAA,kBAAG;AAAA,mBAEnE;AAAA,iBACF;AAAA;AAAA;AAAA,QACF;AAAA;AAAA;AAAA,EACF,GACF;AAEJ;","names":[]}
|
|
@@ -36,7 +36,6 @@ class SubscriptionsService {
|
|
|
36
36
|
async calculateSubscription(data) {
|
|
37
37
|
const { id_account } = await getUserContext();
|
|
38
38
|
const response = await api.apps.post(`/accounts/${id_account}/subscriptions/action/calculate`, data);
|
|
39
|
-
console.log("Calculate subscription response:", { response, data });
|
|
40
39
|
if (response.status === 0) {
|
|
41
40
|
throw new ApiError(
|
|
42
41
|
response.message || "Erro ao calcular assinatura",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/modules/subscriptions/services/subscriptions.service.ts"],"sourcesContent":["import \"server-only\";\r\n\r\nimport { api } from \"../../../infra/api/client\";\r\nimport {\r\n ApiError,\r\n ApiActionResult,\r\n ApiPaginatedActionResult,\r\n PaginatedSuccessResult,\r\n SuccessResult,\r\n} from \"../../../infra/api/types\";\r\nimport { buildQueryParams } from \"../../../infra/utils/params\";\r\nimport { getUserContext } from \"../../auth/utils/get-user-context\";\r\nimport {\r\n Subscription,\r\n SubscriptionSchema,\r\n FindSubscriptionsParams,\r\n} from \"../types\";\r\nimport {\r\n type CalculateSubscriptionRequest,\r\n type CalculateSubscriptionResponse,\r\n type UpdateSubscriptionPlanRequest,\r\n CalculateSubscriptionResponseSchema,\r\n} from \"../types/calculate-subscription.type\";\r\n\r\nclass SubscriptionsService {\r\n async listSubscriptions(\r\n params?: Partial<FindSubscriptionsParams>,\r\n ): Promise<PaginatedSuccessResult<Subscription>> {\r\n const { id_account } = await getUserContext();\r\n\r\n const query = buildQueryParams(\r\n params as Record<string, string | boolean | undefined>,\r\n );\r\n const url = `/accounts/${id_account}/subscriptions${query ? `?${query}` : \"\"}`;\r\n\r\n const response =\r\n await api.apps.get<ApiPaginatedActionResult<Subscription>>(url);\r\n\r\n if (response.status === 0) {\r\n throw new ApiError(\r\n response.message || \"Erro ao listar assinaturas\",\r\n \"LIST_SUBSCRIPTIONS_FAILED\",\r\n 400,\r\n );\r\n }\r\n\r\n const data = response.data.map((item) => SubscriptionSchema.parse(item));\r\n\r\n return {\r\n data,\r\n total: response.total,\r\n success: true,\r\n } satisfies PaginatedSuccessResult<Subscription>;\r\n }\r\n\r\n async calculateSubscription(\r\n data: CalculateSubscriptionRequest,\r\n ): Promise<SuccessResult<CalculateSubscriptionResponse>> {\r\n const { id_account } = await getUserContext();\r\n\r\n const response = await api.apps.post<\r\n ApiActionResult<CalculateSubscriptionResponse>\r\n >(`/accounts/${id_account}/subscriptions/action/calculate`, data);\r\n\r\n
|
|
1
|
+
{"version":3,"sources":["../../../../src/modules/subscriptions/services/subscriptions.service.ts"],"sourcesContent":["import \"server-only\";\r\n\r\nimport { api } from \"../../../infra/api/client\";\r\nimport {\r\n ApiError,\r\n ApiActionResult,\r\n ApiPaginatedActionResult,\r\n PaginatedSuccessResult,\r\n SuccessResult,\r\n} from \"../../../infra/api/types\";\r\nimport { buildQueryParams } from \"../../../infra/utils/params\";\r\nimport { getUserContext } from \"../../auth/utils/get-user-context\";\r\nimport {\r\n Subscription,\r\n SubscriptionSchema,\r\n FindSubscriptionsParams,\r\n} from \"../types\";\r\nimport {\r\n type CalculateSubscriptionRequest,\r\n type CalculateSubscriptionResponse,\r\n type UpdateSubscriptionPlanRequest,\r\n CalculateSubscriptionResponseSchema,\r\n} from \"../types/calculate-subscription.type\";\r\n\r\nclass SubscriptionsService {\r\n async listSubscriptions(\r\n params?: Partial<FindSubscriptionsParams>,\r\n ): Promise<PaginatedSuccessResult<Subscription>> {\r\n const { id_account } = await getUserContext();\r\n\r\n const query = buildQueryParams(\r\n params as Record<string, string | boolean | undefined>,\r\n );\r\n const url = `/accounts/${id_account}/subscriptions${query ? `?${query}` : \"\"}`;\r\n\r\n const response =\r\n await api.apps.get<ApiPaginatedActionResult<Subscription>>(url);\r\n\r\n if (response.status === 0) {\r\n throw new ApiError(\r\n response.message || \"Erro ao listar assinaturas\",\r\n \"LIST_SUBSCRIPTIONS_FAILED\",\r\n 400,\r\n );\r\n }\r\n\r\n const data = response.data.map((item) => SubscriptionSchema.parse(item));\r\n\r\n return {\r\n data,\r\n total: response.total,\r\n success: true,\r\n } satisfies PaginatedSuccessResult<Subscription>;\r\n }\r\n\r\n async calculateSubscription(\r\n data: CalculateSubscriptionRequest,\r\n ): Promise<SuccessResult<CalculateSubscriptionResponse>> {\r\n const { id_account } = await getUserContext();\r\n\r\n const response = await api.apps.post<\r\n ApiActionResult<CalculateSubscriptionResponse>\r\n >(`/accounts/${id_account}/subscriptions/action/calculate`, data);\r\n\r\n if (response.status === 0) {\r\n throw new ApiError(\r\n response.message || \"Erro ao calcular assinatura\",\r\n \"CALCULATE_SUBSCRIPTION_FAILED\",\r\n 400,\r\n );\r\n }\r\n\r\n return {\r\n success: true,\r\n data: CalculateSubscriptionResponseSchema.parse(response.data),\r\n };\r\n }\r\n\r\n async updateSubscriptionPlan(\r\n subscriptionId: number | string,\r\n data: UpdateSubscriptionPlanRequest,\r\n ): Promise<SuccessResult<void>> {\r\n const { id_account } = await getUserContext();\r\n\r\n const response = await api.apps.put<ApiActionResult<void>>(\r\n `/accounts/${id_account}/subscriptions/${subscriptionId}/action/plan`,\r\n data,\r\n );\r\n\r\n if (response.status === 0) {\r\n throw new ApiError(\r\n response.message || \"Erro ao atualizar plano\",\r\n \"UPDATE_SUBSCRIPTION_PLAN_FAILED\",\r\n 400,\r\n );\r\n }\r\n\r\n return { success: true };\r\n }\r\n}\r\n\r\nexport const subscriptionsService = new SubscriptionsService();\r\n"],"mappings":"AAAA,OAAO;AAEP,SAAS,WAAW;AACpB;AAAA,EACE;AAAA,OAKK;AACP,SAAS,wBAAwB;AACjC,SAAS,sBAAsB;AAC/B;AAAA,EAEE;AAAA,OAEK;AACP;AAAA,EAIE;AAAA,OACK;AAEP,MAAM,qBAAqB;AAAA,EACzB,MAAM,kBACJ,QAC+C;AAC/C,UAAM,EAAE,WAAW,IAAI,MAAM,eAAe;AAE5C,UAAM,QAAQ;AAAA,MACZ;AAAA,IACF;AACA,UAAM,MAAM,aAAa,UAAU,iBAAiB,QAAQ,IAAI,KAAK,KAAK,EAAE;AAE5E,UAAM,WACJ,MAAM,IAAI,KAAK,IAA4C,GAAG;AAEhE,QAAI,SAAS,WAAW,GAAG;AACzB,YAAM,IAAI;AAAA,QACR,SAAS,WAAW;AAAA,QACpB;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,UAAM,OAAO,SAAS,KAAK,IAAI,CAAC,SAAS,mBAAmB,MAAM,IAAI,CAAC;AAEvE,WAAO;AAAA,MACL;AAAA,MACA,OAAO,SAAS;AAAA,MAChB,SAAS;AAAA,IACX;AAAA,EACF;AAAA,EAEA,MAAM,sBACJ,MACuD;AACvD,UAAM,EAAE,WAAW,IAAI,MAAM,eAAe;AAE5C,UAAM,WAAW,MAAM,IAAI,KAAK,KAE9B,aAAa,UAAU,mCAAmC,IAAI;AAEhE,QAAI,SAAS,WAAW,GAAG;AACzB,YAAM,IAAI;AAAA,QACR,SAAS,WAAW;AAAA,QACpB;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,MACL,SAAS;AAAA,MACT,MAAM,oCAAoC,MAAM,SAAS,IAAI;AAAA,IAC/D;AAAA,EACF;AAAA,EAEA,MAAM,uBACJ,gBACA,MAC8B;AAC9B,UAAM,EAAE,WAAW,IAAI,MAAM,eAAe;AAE5C,UAAM,WAAW,MAAM,IAAI,KAAK;AAAA,MAC9B,aAAa,UAAU,kBAAkB,cAAc;AAAA,MACvD;AAAA,IACF;AAEA,QAAI,SAAS,WAAW,GAAG;AACzB,YAAM,IAAI;AAAA,QACR,SAAS,WAAW;AAAA,QACpB;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,WAAO,EAAE,SAAS,KAAK;AAAA,EACzB;AACF;AAEO,MAAM,uBAAuB,IAAI,qBAAqB;","names":[]}
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
function buildPlanExtras(items, overrides) {
|
|
2
2
|
const overrideMap = new Map(overrides.map((o) => [o.id_addon, o]));
|
|
3
|
-
const
|
|
3
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4
|
+
const extras = [];
|
|
5
|
+
for (const item of items) {
|
|
6
|
+
if (item.id_addon == null || seen.has(item.id_addon)) continue;
|
|
7
|
+
seen.add(item.id_addon);
|
|
4
8
|
const override = overrideMap.get(item.id_addon);
|
|
5
9
|
if (override) {
|
|
6
10
|
overrideMap.delete(item.id_addon);
|
|
7
|
-
|
|
11
|
+
extras.push({
|
|
8
12
|
id_addon: override.id_addon,
|
|
9
13
|
value: 0,
|
|
10
14
|
quantity: override.quantity,
|
|
11
15
|
currency: override.currency
|
|
12
|
-
};
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
extras.push({
|
|
19
|
+
id_addon: item.id_addon,
|
|
20
|
+
value: item.value,
|
|
21
|
+
quantity: item.quantity,
|
|
22
|
+
currency: item.currency
|
|
23
|
+
});
|
|
13
24
|
}
|
|
14
|
-
|
|
15
|
-
id_addon: item.id_addon,
|
|
16
|
-
value: item.value,
|
|
17
|
-
quantity: item.quantity,
|
|
18
|
-
currency: item.currency
|
|
19
|
-
};
|
|
20
|
-
});
|
|
25
|
+
}
|
|
21
26
|
for (const override of overrideMap.values()) {
|
|
22
27
|
extras.push({
|
|
23
28
|
id_addon: override.id_addon,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/modules/subscriptions/utils/build-plan-extras.ts"],"sourcesContent":["import type { SubscriptionItem } from '../types';\n\nexport interface PlanExtraOverride {\n id_addon: number;\n quantity: number;\n currency: string;\n}\n\nexport interface PlanExtra {\n id_addon: number;\n value: number;\n quantity: number;\n currency: string;\n}\n\nexport function buildPlanExtras(\n items: SubscriptionItem[],\n overrides: PlanExtraOverride[]\n): PlanExtra[] {\n const overrideMap = new Map(overrides.map((o) => [o.id_addon, o]));\n\n const extras: PlanExtra[] =
|
|
1
|
+
{"version":3,"sources":["../../../../src/modules/subscriptions/utils/build-plan-extras.ts"],"sourcesContent":["import type { SubscriptionItem } from '../types';\n\nexport interface PlanExtraOverride {\n id_addon: number;\n quantity: number;\n currency: string;\n}\n\nexport interface PlanExtra {\n id_addon: number;\n value: number;\n quantity: number;\n currency: string;\n}\n\nexport function buildPlanExtras(\n items: SubscriptionItem[],\n overrides: PlanExtraOverride[]\n): PlanExtra[] {\n const overrideMap = new Map(overrides.map((o) => [o.id_addon, o]));\n const seen = new Set<number>();\n\n const extras: PlanExtra[] = [];\n\n for (const item of items) {\n if (item.id_addon == null || seen.has(item.id_addon)) continue;\n seen.add(item.id_addon);\n\n const override = overrideMap.get(item.id_addon);\n if (override) {\n overrideMap.delete(item.id_addon);\n extras.push({\n id_addon: override.id_addon,\n value: 0,\n quantity: override.quantity,\n currency: override.currency,\n });\n } else {\n extras.push({\n id_addon: item.id_addon,\n value: item.value,\n quantity: item.quantity,\n currency: item.currency,\n });\n }\n }\n\n for (const override of overrideMap.values()) {\n extras.push({\n id_addon: override.id_addon,\n value: 0,\n quantity: override.quantity,\n currency: override.currency,\n });\n }\n\n return extras;\n}\n"],"mappings":"AAeO,SAAS,gBACd,OACA,WACa;AACb,QAAM,cAAc,IAAI,IAAI,UAAU,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC;AACjE,QAAM,OAAO,oBAAI,IAAY;AAE7B,QAAM,SAAsB,CAAC;AAE7B,aAAW,QAAQ,OAAO;AACxB,QAAI,KAAK,YAAY,QAAQ,KAAK,IAAI,KAAK,QAAQ,EAAG;AACtD,SAAK,IAAI,KAAK,QAAQ;AAEtB,UAAM,WAAW,YAAY,IAAI,KAAK,QAAQ;AAC9C,QAAI,UAAU;AACZ,kBAAY,OAAO,KAAK,QAAQ;AAChC,aAAO,KAAK;AAAA,QACV,UAAU,SAAS;AAAA,QACnB,OAAO;AAAA,QACP,UAAU,SAAS;AAAA,QACnB,UAAU,SAAS;AAAA,MACrB,CAAC;AAAA,IACH,OAAO;AACL,aAAO,KAAK;AAAA,QACV,UAAU,KAAK;AAAA,QACf,OAAO,KAAK;AAAA,QACZ,UAAU,KAAK;AAAA,QACf,UAAU,KAAK;AAAA,MACjB,CAAC;AAAA,IACH;AAAA,EACF;AAEA,aAAW,YAAY,YAAY,OAAO,GAAG;AAC3C,WAAO,KAAK;AAAA,MACV,UAAU,SAAS;AAAA,MACnB,OAAO;AAAA,MACP,UAAU,SAAS;AAAA,MACnB,UAAU,SAAS;AAAA,IACrB,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@ import { useUpdateSubscriptionPlan } from '../../modules/subscriptions/hooks/upd
|
|
|
26
26
|
import { buildPlanExtras } from '../../modules/subscriptions/utils/build-plan-extras';
|
|
27
27
|
import { getPriceFromCalculatedData } from '../../modules/subscriptions/utils/periodicity';
|
|
28
28
|
import { IconInfoCircle, IconX, IconLoader2 } from '@tabler/icons-react';
|
|
29
|
-
import { useCallback, useEffect, useMemo } from 'react';
|
|
29
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
30
30
|
import { useForm } from 'react-hook-form';
|
|
31
31
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
32
32
|
import z from 'zod';
|
|
@@ -73,27 +73,35 @@ export default function BuyCreditsModal() {
|
|
|
73
73
|
},
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
if (open) {
|
|
78
|
-
form.reset({
|
|
79
|
-
creditOption: currentCredits,
|
|
80
|
-
selectedCardId:
|
|
81
|
-
form.getValues('selectedCardId') || cardsData?.data?.[0]?.id.toString() || '',
|
|
82
|
-
});
|
|
76
|
+
const hasInitialized = useRef(false);
|
|
83
77
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
plan_extras: buildPlanExtras(subscription?.items ?? [], [
|
|
89
|
-
{
|
|
90
|
-
id_addon: ADDON_IDS.AI,
|
|
91
|
-
quantity: currentCredits,
|
|
92
|
-
currency: 'BRL',
|
|
93
|
-
},
|
|
94
|
-
]),
|
|
95
|
-
});
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (!open) {
|
|
80
|
+
hasInitialized.current = false;
|
|
81
|
+
return;
|
|
96
82
|
}
|
|
83
|
+
|
|
84
|
+
if (hasInitialized.current || !subscription) return;
|
|
85
|
+
hasInitialized.current = true;
|
|
86
|
+
|
|
87
|
+
form.reset({
|
|
88
|
+
creditOption: currentCredits,
|
|
89
|
+
selectedCardId:
|
|
90
|
+
form.getValues('selectedCardId') || cardsData?.data?.[0]?.id.toString() || '',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
calculate({
|
|
94
|
+
id_product: 1,
|
|
95
|
+
...(planId != null && { id_plan: planId }),
|
|
96
|
+
...(subscriptionId != null && { id_subscription: subscriptionId }),
|
|
97
|
+
plan_extras: buildPlanExtras(subscription?.items ?? [], [
|
|
98
|
+
{
|
|
99
|
+
id_addon: ADDON_IDS.AI,
|
|
100
|
+
quantity: currentCredits,
|
|
101
|
+
currency: 'BRL',
|
|
102
|
+
},
|
|
103
|
+
]),
|
|
104
|
+
});
|
|
97
105
|
}, [open, subscription, currentCredits, cardsData, form, calculate, planId, subscriptionId]);
|
|
98
106
|
|
|
99
107
|
const creditOption = form.watch('creditOption');
|
|
@@ -62,8 +62,6 @@ class SubscriptionsService {
|
|
|
62
62
|
ApiActionResult<CalculateSubscriptionResponse>
|
|
63
63
|
>(`/accounts/${id_account}/subscriptions/action/calculate`, data);
|
|
64
64
|
|
|
65
|
-
console.log("Calculate subscription response:", { response, data });
|
|
66
|
-
|
|
67
65
|
if (response.status === 0) {
|
|
68
66
|
throw new ApiError(
|
|
69
67
|
response.message || "Erro ao calcular assinatura",
|
|
@@ -18,27 +18,32 @@ export function buildPlanExtras(
|
|
|
18
18
|
overrides: PlanExtraOverride[]
|
|
19
19
|
): PlanExtra[] {
|
|
20
20
|
const overrideMap = new Map(overrides.map((o) => [o.id_addon, o]));
|
|
21
|
+
const seen = new Set<number>();
|
|
21
22
|
|
|
22
|
-
const extras: PlanExtra[] =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
23
|
+
const extras: PlanExtra[] = [];
|
|
24
|
+
|
|
25
|
+
for (const item of items) {
|
|
26
|
+
if (item.id_addon == null || seen.has(item.id_addon)) continue;
|
|
27
|
+
seen.add(item.id_addon);
|
|
28
|
+
|
|
29
|
+
const override = overrideMap.get(item.id_addon);
|
|
30
|
+
if (override) {
|
|
31
|
+
overrideMap.delete(item.id_addon);
|
|
32
|
+
extras.push({
|
|
33
|
+
id_addon: override.id_addon,
|
|
34
|
+
value: 0,
|
|
35
|
+
quantity: override.quantity,
|
|
36
|
+
currency: override.currency,
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
extras.push({
|
|
40
|
+
id_addon: item.id_addon,
|
|
37
41
|
value: item.value,
|
|
38
42
|
quantity: item.quantity,
|
|
39
43
|
currency: item.currency,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
42
47
|
|
|
43
48
|
for (const override of overrideMap.values()) {
|
|
44
49
|
extras.push({
|