-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move logic from user and payment to paymentcontainer
- Loading branch information
1 parent
d6b369d
commit 6b7a72a
Showing
3 changed files
with
134 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router'; | ||
import shallow from 'zustand/shallow'; | ||
|
||
import styles from '#src/pages/User/User.module.scss'; | ||
import LoadingOverlay from '#components/LoadingOverlay/LoadingOverlay'; | ||
import Payment from '#components/Payment/Payment'; | ||
import { getReceipt } from '#src/stores/AccountController'; | ||
import { useAccountStore } from '#src/stores/AccountStore'; | ||
import { getSubscriptionSwitches } from '#src/stores/CheckoutController'; | ||
import { useCheckoutStore } from '#src/stores/CheckoutStore'; | ||
import { useConfigStore } from '#src/stores/ConfigStore'; | ||
import { addQueryParam } from '#src/utils/location'; | ||
import useOffers from '#src/hooks/useOffers'; | ||
|
||
const PaymentContainer = () => { | ||
const { accessModel } = useConfigStore( | ||
(s) => ({ | ||
accessModel: s.accessModel, | ||
favoritesList: s.config.features?.favoritesList, | ||
}), | ||
shallow, | ||
); | ||
const navigate = useNavigate(); | ||
const [showAllTransactions, setShowAllTransactions] = useState(false); | ||
const [isLoadingReceipt, setIsLoadingReceipt] = useState(false); | ||
|
||
const { | ||
user: customer, | ||
subscription: activeSubscription, | ||
transactions, | ||
activePayment, | ||
pendingOffer, | ||
loading, | ||
canRenewSubscription, | ||
canUpdatePaymentMethod, | ||
canShowReceipts, | ||
} = useAccountStore(); | ||
const { offerSwitches } = useCheckoutStore(); | ||
const location = useLocation(); | ||
|
||
const handleUpgradeSubscriptionClick = async () => { | ||
navigate(addQueryParam(location, 'u', 'upgrade-subscription')); | ||
}; | ||
|
||
const handleShowReceiptClick = async (transactionId: string) => { | ||
setIsLoadingReceipt(true); | ||
|
||
try { | ||
const receipt = await getReceipt(transactionId); | ||
|
||
if (receipt) { | ||
const newWindow = window.open('', `Receipt ${transactionId}`, ''); | ||
const htmlString = window.atob(receipt); | ||
|
||
if (newWindow) { | ||
newWindow.opener = null; | ||
newWindow.document.write(htmlString); | ||
newWindow.document.close(); | ||
} | ||
} | ||
} catch (error: unknown) { | ||
throw new Error("Couldn't parse receipt. " + (error instanceof Error ? error.message : '')); | ||
} | ||
|
||
setIsLoadingReceipt(false); | ||
}; | ||
|
||
useEffect(() => { | ||
if (accessModel !== 'AVOD') { | ||
getSubscriptionSwitches(); | ||
} | ||
}, [accessModel]); | ||
|
||
useEffect(() => { | ||
if (!loading && !customer) { | ||
navigate('/', { replace: true }); | ||
} | ||
}, [navigate, customer, loading]); | ||
|
||
const { offers } = useOffers(); | ||
|
||
if (!customer) { | ||
return <LoadingOverlay />; | ||
} | ||
|
||
const pendingDowngradeOfferId = (customer.metadata?.[`${activeSubscription?.subscriptionId}_pending_downgrade`] as string) || ''; | ||
|
||
return ( | ||
<Payment | ||
accessModel={accessModel} | ||
activeSubscription={activeSubscription} | ||
activePaymentDetail={activePayment} | ||
transactions={transactions} | ||
customer={customer} | ||
pendingOffer={pendingOffer} | ||
isLoading={loading || isLoadingReceipt} | ||
panelClassName={styles.panel} | ||
panelHeaderClassName={styles.panelHeader} | ||
onShowAllTransactionsClick={() => setShowAllTransactions(true)} | ||
showAllTransactions={showAllTransactions} | ||
canUpdatePaymentMethod={canUpdatePaymentMethod} | ||
canRenewSubscription={canRenewSubscription} | ||
onUpgradeSubscriptionClick={handleUpgradeSubscriptionClick} | ||
offerSwitchesAvailable={!!offerSwitches.length} | ||
canShowReceipts={canShowReceipts} | ||
onShowReceiptClick={handleShowReceiptClick} | ||
offers={offers} | ||
pendingDowngradeOfferId={pendingDowngradeOfferId} | ||
/> | ||
); | ||
}; | ||
|
||
export default PaymentContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters