2023-10-18 09:14:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState, createContext, useEffect } from '@wordpress/element';
|
2023-12-18 12:53:36 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2023-10-18 09:14:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-12-18 12:53:36 +00:00
|
|
|
import { SubscriptionsContextType, NoticeStatus } from './types';
|
2023-10-18 09:14:14 +00:00
|
|
|
import { Subscription } from '../components/my-subscriptions/types';
|
2023-11-16 10:51:30 +00:00
|
|
|
import {
|
2023-12-18 12:53:36 +00:00
|
|
|
addNotice,
|
2023-11-16 10:51:30 +00:00
|
|
|
fetchSubscriptions,
|
|
|
|
refreshSubscriptions as fetchSubscriptionsFromWooCom,
|
|
|
|
} from '../utils/functions';
|
2023-10-18 09:14:14 +00:00
|
|
|
|
|
|
|
export const SubscriptionsContext = createContext< SubscriptionsContextType >( {
|
|
|
|
subscriptions: [],
|
|
|
|
setSubscriptions: () => {},
|
2023-10-27 04:08:27 +00:00
|
|
|
loadSubscriptions: () => new Promise( () => {} ),
|
2023-11-16 10:51:30 +00:00
|
|
|
refreshSubscriptions: () => new Promise( () => {} ),
|
2023-10-18 09:14:14 +00:00
|
|
|
isLoading: true,
|
|
|
|
setIsLoading: () => {},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export function SubscriptionsContextProvider( props: {
|
|
|
|
children: JSX.Element;
|
|
|
|
} ): JSX.Element {
|
|
|
|
const [ subscriptions, setSubscriptions ] = useState<
|
|
|
|
Array< Subscription >
|
|
|
|
>( [] );
|
|
|
|
const [ isLoading, setIsLoading ] = useState( true );
|
|
|
|
|
|
|
|
const loadSubscriptions = ( toggleLoading?: boolean ) => {
|
|
|
|
if ( toggleLoading === true ) {
|
|
|
|
setIsLoading( true );
|
|
|
|
}
|
|
|
|
|
2023-10-27 04:08:27 +00:00
|
|
|
return fetchSubscriptions()
|
2023-10-18 09:14:14 +00:00
|
|
|
.then( ( subscriptionResponse ) => {
|
|
|
|
setSubscriptions( subscriptionResponse );
|
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
if ( toggleLoading ) {
|
|
|
|
setIsLoading( false );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
2023-11-16 10:51:30 +00:00
|
|
|
|
2023-12-18 12:53:36 +00:00
|
|
|
const refreshSubscriptions = ( toggleLoading?: boolean ) => {
|
|
|
|
if ( toggleLoading ) {
|
|
|
|
setIsLoading( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetchSubscriptionsFromWooCom()
|
|
|
|
.then( ( subscriptionResponse ) => {
|
2023-11-16 10:51:30 +00:00
|
|
|
setSubscriptions( subscriptionResponse );
|
2023-12-18 12:53:36 +00:00
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
if ( toggleLoading ) {
|
|
|
|
setIsLoading( false );
|
|
|
|
}
|
|
|
|
} );
|
2023-11-16 10:51:30 +00:00
|
|
|
};
|
|
|
|
|
2023-10-27 04:08:27 +00:00
|
|
|
useEffect( () => {
|
2023-12-18 12:53:36 +00:00
|
|
|
/**
|
|
|
|
* Check if we have &install=PRODUCT_KEY in the URL. This means we have just
|
|
|
|
* installed a new product and nwe need to refresh the list.
|
|
|
|
*/
|
|
|
|
const urlParams = new URLSearchParams( window.location.search );
|
|
|
|
const installKey = urlParams.get( 'install' );
|
|
|
|
|
|
|
|
if ( installKey ) {
|
|
|
|
refreshSubscriptions( true ).catch( ( error ) => {
|
|
|
|
addNotice(
|
|
|
|
'woocommerce-marketplace-refresh-subscriptions',
|
|
|
|
sprintf(
|
|
|
|
// translators: %s is the error message.
|
|
|
|
__(
|
|
|
|
'Error refreshing subscriptions: %s',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
error.message
|
|
|
|
),
|
|
|
|
NoticeStatus.Error
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubscriptions( true ).catch( ( error ) => {
|
|
|
|
addNotice(
|
|
|
|
'woocommerce-marketplace-load-subscriptions',
|
|
|
|
sprintf(
|
|
|
|
// translators: %s is the error message.
|
|
|
|
__( 'Error loading subscriptions: %s', 'woocommerce' ),
|
|
|
|
error.message
|
|
|
|
),
|
|
|
|
NoticeStatus.Error
|
|
|
|
);
|
|
|
|
} );
|
2023-10-27 04:08:27 +00:00
|
|
|
}, [] );
|
2023-10-18 09:14:14 +00:00
|
|
|
|
|
|
|
const contextValue = {
|
|
|
|
subscriptions,
|
|
|
|
setSubscriptions,
|
2023-11-06 08:35:43 +00:00
|
|
|
loadSubscriptions,
|
2023-11-16 10:51:30 +00:00
|
|
|
refreshSubscriptions,
|
2023-10-18 09:14:14 +00:00
|
|
|
isLoading,
|
|
|
|
setIsLoading,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SubscriptionsContext.Provider value={ contextValue }>
|
|
|
|
{ props.children }
|
|
|
|
</SubscriptionsContext.Provider>
|
|
|
|
);
|
|
|
|
}
|