Skip to content

Commit

Permalink
fix: verify cancel subscription after cancel api call (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbh authored Jul 29, 2024
1 parent accfddc commit 132ecc5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function ConfirmPlanChange() {
changePlan,
isLoading: isChangePlanLoading,
verifyPlanChange,
verifySubscriptionCancel,
cancelSubscription,
checkBasePlan
} = usePlans();
Expand Down Expand Up @@ -67,17 +68,10 @@ export default function ConfirmPlanChange() {

const isUpgrade = planAction.btnLabel === 'Upgrade';

// const expiryDate = useMemo(() => {
// if (activePlan?.created_at && activePlan?.interval) {
// return dayjs(activePlan?.created_at)
// .add(1, activePlan?.interval as ManipulateType)
// .format(config.dateFormat || DEFAULT_DATE_FORMAT);
// }
// return '';
// }, [activePlan?.created_at, activePlan?.interval, config.dateFormat]);

const verifyChange = useCallback(async () => {
const planPhase = await verifyPlanChange({ planId });
const planPhase = isNewPlanBasePlan
? await verifySubscriptionCancel({})
: await verifyPlanChange({ planId });
const actionName = planAction?.btnLabel.toLowerCase();
if (planPhase) {
const changeDate = dayjs(planPhase?.effective_at).format(
Expand All @@ -93,7 +87,9 @@ export default function ConfirmPlanChange() {
config?.dateFormat,
planAction?.btnLabel,
planId,
verifyPlanChange
verifyPlanChange,
verifySubscriptionCancel,
isNewPlanBasePlan
]);

const onConfirm = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ interface verifyPlanChangeOptions {
onSuccess?: (planPhase: SubscriptionPhase) => void;
}

interface verifyCancelSubscriptionOptions {
onSuccess?: (planPhase: SubscriptionPhase) => void;
}

export const usePlans = () => {
const [isLoading, setIsLoading] = useState(false);
const [hasAlreadyTrialed, setHasAlreadyTrialed] = useState(false);
Expand Down Expand Up @@ -160,9 +164,23 @@ export const usePlans = () => {
const activeSub = await fetchActiveSubsciption();
if (activeSub) {
const planPhase = activeSub.phases?.find(
phase =>
phase?.plan_id === planId ||
(planId === NIL_UUID && phase?.plan_id === '')
phase => phase?.plan_id === planId && phase.reason === 'change'
);
if (planPhase) {
onSuccess(planPhase);
return planPhase;
}
}
},
[fetchActiveSubsciption]
);

const verifySubscriptionCancel = useCallback(
async ({ onSuccess = () => {} }: verifyCancelSubscriptionOptions) => {
const activeSub = await fetchActiveSubsciption();
if (activeSub) {
const planPhase = activeSub.phases?.find(
phase => phase?.plan_id === '' && phase.reason === 'cancel'
);
if (planPhase) {
onSuccess(planPhase);
Expand Down Expand Up @@ -265,6 +283,7 @@ export const usePlans = () => {
isLoading,
changePlan,
verifyPlanChange,
verifySubscriptionCancel,
isTrialCheckLoading: isAllPlansLoading,
hasAlreadyTrialed,
isCurrentlyTrialing,
Expand Down

0 comments on commit 132ecc5

Please sign in to comment.