clear more tickets
This commit is contained in:
@@ -154,6 +154,7 @@ const ManageSubscription = ({ navigation }) => {
|
||||
currentUserData?.stripeSubscription?.id ||
|
||||
currentUserData?.stripeSubscription?.subscriptionId ||
|
||||
null;
|
||||
const localSubscription = currentUserData?.stripeSubscription || null;
|
||||
|
||||
const triggerRefresh = useCallback(() => {
|
||||
setRefreshToken((value) => value + 1);
|
||||
@@ -219,8 +220,9 @@ const ManageSubscription = ({ navigation }) => {
|
||||
}, [stripeCustomerId, localSubscriptionId, refreshToken]);
|
||||
|
||||
const subscriptionInfo = useMemo(() => {
|
||||
const rawSubscription =
|
||||
remoteSubscription || currentUserData?.stripeSubscription || null;
|
||||
const subscriptionSources = [remoteSubscription, localSubscription].filter(
|
||||
Boolean,
|
||||
);
|
||||
|
||||
const pickString = (value) => {
|
||||
if (typeof value !== "string") {
|
||||
@@ -230,40 +232,71 @@ const ManageSubscription = ({ navigation }) => {
|
||||
return trimmed ? trimmed : null;
|
||||
};
|
||||
|
||||
const pickFromSources = (resolver) => {
|
||||
for (const source of subscriptionSources) {
|
||||
if (!source) {
|
||||
continue;
|
||||
}
|
||||
const value = resolver(source);
|
||||
if (value !== undefined && value !== null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const pickDateFromSources = (...resolvers) => {
|
||||
for (const source of subscriptionSources) {
|
||||
if (!source) {
|
||||
continue;
|
||||
}
|
||||
for (const resolveValue of resolvers) {
|
||||
const date = toDate(resolveValue(source));
|
||||
if (date) {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const statusSource =
|
||||
pickString(currentUserData?.stripeSubscriptionStatus) ||
|
||||
pickString(remoteSubscription?.status) ||
|
||||
pickString(currentUserData?.stripeSubscription?.status) ||
|
||||
pickString(
|
||||
currentUserData?.stripeSubscription?.stripeSubscriptionStatus,
|
||||
) ||
|
||||
pickString(rawSubscription?.status) ||
|
||||
pickString(pickFromSources((source) => source?.status)) ||
|
||||
pickString(pickFromSources((source) => source?.stripeSubscriptionStatus)) ||
|
||||
pickString(pickFromSources((source) => source?.metadata?.status)) ||
|
||||
null;
|
||||
|
||||
const status = statusSource ? statusSource.toLowerCase() : null;
|
||||
|
||||
const cancelAtPeriodEnd =
|
||||
remoteSubscription?.cancelAtPeriodEnd === true ||
|
||||
rawSubscription?.cancelAtPeriodEnd === true ||
|
||||
rawSubscription?.cancel_at_period_end === true ||
|
||||
false;
|
||||
const cancelAtPeriodEnd = subscriptionSources.some(
|
||||
(source) =>
|
||||
source?.cancelAtPeriodEnd === true ||
|
||||
source?.cancel_at_period_end === true,
|
||||
);
|
||||
|
||||
const resolveLevelSource = () => {
|
||||
const candidates = [
|
||||
pickString(remoteSubscription?.level),
|
||||
pickString(pickFromSources((source) => source?.level)),
|
||||
pickString(currentUserData?.premiumLevel),
|
||||
pickString(rawSubscription?.metadata?.level),
|
||||
pickString(rawSubscription?.metadata?.subscriptionLevel),
|
||||
pickString(pickFromSources((source) => source?.metadata?.level)),
|
||||
pickString(
|
||||
pickFromSources((source) => source?.metadata?.subscriptionLevel),
|
||||
),
|
||||
];
|
||||
return candidates.find(Boolean) || null;
|
||||
};
|
||||
|
||||
const resolvePeriodSource = () => {
|
||||
const candidates = [
|
||||
pickString(remoteSubscription?.billingPeriod),
|
||||
pickString(pickFromSources((source) => source?.billingPeriod)),
|
||||
pickString(currentUserData?.premiumBillingPeriod),
|
||||
pickString(rawSubscription?.metadata?.billingPeriod),
|
||||
pickString(rawSubscription?.metadata?.subscriptionBillingPeriod),
|
||||
pickString(pickFromSources((source) => source?.metadata?.billingPeriod)),
|
||||
pickString(
|
||||
pickFromSources(
|
||||
(source) => source?.metadata?.subscriptionBillingPeriod,
|
||||
),
|
||||
),
|
||||
];
|
||||
return candidates.find(Boolean) || null;
|
||||
};
|
||||
@@ -292,14 +325,15 @@ const ManageSubscription = ({ navigation }) => {
|
||||
? planLabelParts.join(" · ")
|
||||
: "Abonnement Musicland";
|
||||
|
||||
const currentPeriodEndDate =
|
||||
toDate(remoteSubscription?.currentPeriodEnd) ||
|
||||
toDate(rawSubscription?.currentPeriodEnd) ||
|
||||
toDate(rawSubscription?.current_period_end);
|
||||
const createdAtDate =
|
||||
toDate(remoteSubscription?.created) ||
|
||||
toDate(rawSubscription?.createdAt) ||
|
||||
toDate(rawSubscription?.created_at);
|
||||
const currentPeriodEndDate = pickDateFromSources(
|
||||
(source) => source?.currentPeriodEnd,
|
||||
(source) => source?.current_period_end,
|
||||
);
|
||||
const createdAtDate = pickDateFromSources(
|
||||
(source) => source?.created,
|
||||
(source) => source?.createdAt,
|
||||
(source) => source?.created_at,
|
||||
);
|
||||
|
||||
const statusLabelBase =
|
||||
STATUS_LABELS[status] ||
|
||||
@@ -307,7 +341,13 @@ const ManageSubscription = ({ navigation }) => {
|
||||
const statusLabel = statusLabelBase ? capitalize(statusLabelBase) : null;
|
||||
const statusColors = getStatusColors(status);
|
||||
|
||||
const hasAnySubscription = Boolean(rawSubscription?.id);
|
||||
const subscriptionId =
|
||||
pickString(pickFromSources((source) => source?.id)) ||
|
||||
pickString(pickFromSources((source) => source?.subscriptionId)) ||
|
||||
pickString(currentUserData?.stripeSubscription?.subscriptionId) ||
|
||||
null;
|
||||
|
||||
const hasAnySubscription = Boolean(subscriptionId);
|
||||
const hasActiveSubscription =
|
||||
hasAnySubscription && ACTIVE_SUBSCRIPTION_STATUSES.has(status);
|
||||
const canCancel = hasActiveSubscription && !cancelAtPeriodEnd;
|
||||
@@ -317,10 +357,7 @@ const ManageSubscription = ({ navigation }) => {
|
||||
: "—";
|
||||
const createdAtLabel = createdAtDate ? formatDate(createdAtDate) : null;
|
||||
|
||||
let coinsPerMonth =
|
||||
typeof remoteSubscription?.coinsPerMonth === "number"
|
||||
? remoteSubscription.coinsPerMonth
|
||||
: null;
|
||||
let coinsPerMonth = pickFromSources((source) => source?.coinsPerMonth);
|
||||
|
||||
if (
|
||||
coinsPerMonth === null &&
|
||||
@@ -395,17 +432,13 @@ const ManageSubscription = ({ navigation }) => {
|
||||
createdAtLabel,
|
||||
helperMessage: helperMessage || null,
|
||||
level: level || null,
|
||||
subscriptionId:
|
||||
remoteSubscription?.id ||
|
||||
rawSubscription?.id ||
|
||||
currentUserData?.stripeSubscription?.subscriptionId ||
|
||||
null,
|
||||
subscriptionId,
|
||||
coinsPerMonth: normalizedCoins,
|
||||
isAnnual,
|
||||
nextGrantDate: resolvedNextGrantDate,
|
||||
nextGrantLabel,
|
||||
};
|
||||
}, [currentUserData, remoteSubscription]);
|
||||
}, [currentUserData, localSubscription, remoteSubscription]);
|
||||
|
||||
const handleOpenPlans = useCallback(() => {
|
||||
const params =
|
||||
|
||||
Reference in New Issue
Block a user