Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix / Get subscriber loop #1539

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/components/PremiumFeature/PremiumFeature.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import React from 'react';
import { connect } from 'react-redux';
import { showPremiumRequired } from '../../providers/SubscriptionProvider/SubscriptionProvider.actions';
import {
updateIsInFreeCountry,
updateIsSubscribed,
updateSubscription,
updateIsOnTrialPeriod,
showPremiumRequired
} from '../../providers/SubscriptionProvider/SubscriptionProvider.actions';

function isUpdateSubscriptionNeeded(lastUpdated) {
if (!lastUpdated) return true;
const MAX_HOURS_DIFFERENCE = 12;
const actualTime = new Date().getTime();
const difference = actualTime - lastUpdated;
const hoursDifference = difference / (1000 * 60 * 60);
return hoursDifference > MAX_HOURS_DIFFERENCE;
}

function PremiumFeature({
children,
isOnTrialPeriod,
isSubscribed,
isInFreeCountry,
showPremiumRequired
showPremiumRequired,
lastUpdated
}) {
const captured = event => {
if (isUpdateSubscriptionNeeded(lastUpdated)) {
RodriSanchez1 marked this conversation as resolved.
Show resolved Hide resolved
const requestOrigin = 'Function: captured - Component: PremiumFeature';
updateIsSubscribed(requestOrigin);
updateIsInFreeCountry();
updateIsOnTrialPeriod();
}

if (isInFreeCountry || isSubscribed || isOnTrialPeriod) return;
event.stopPropagation();
event.preventDefault();
Expand All @@ -26,10 +49,16 @@ function PremiumFeature({
const mapStateToProps = state => ({
isOnTrialPeriod: state.subscription.isOnTrialPeriod,
isSubscribed: state.subscription.isSubscribed,
isInFreeCountry: state.subscription.isInFreeCountry
isInFreeCountry: state.subscription.isInFreeCountry,
lastUpdated: state.subscription.lastUpdated
});

const mapDispatchToProps = { showPremiumRequired };
const mapDispatchToProps = {
showPremiumRequired,
updateIsSubscribed,
updateSubscription,
updateIsInFreeCountry
};

export default connect(
mapStateToProps,
Expand Down
6 changes: 3 additions & 3 deletions src/components/Settings/Subscribe/Subscribe.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class SubscribeContainer extends PureComponent {
const { updateIsSubscribed, updatePlans } = this.props;
const requestOrigin =
'Function: componentDidMount - Component: Subscribe Container';
updateIsSubscribed(false, requestOrigin);
updateIsSubscribed(requestOrigin);
updatePlans();
}

Expand All @@ -56,7 +56,7 @@ export class SubscribeContainer extends PureComponent {
const { updateIsSubscribed, updatePlans } = this.props;
const requestOrigin =
'Fuction: handleRefreshSubscription() - Component: subscribeContainer';
updateIsSubscribed(false, requestOrigin);
updateIsSubscribed(requestOrigin);
updatePlans();
};

Expand All @@ -68,7 +68,7 @@ export class SubscribeContainer extends PureComponent {
this.setState({ cancelSubscriptionStatus: 'ok' });
const requestOrigin =
'Function: handleCancelSubscription() - Component:Subscribe Container';
updateIsSubscribed(false, requestOrigin);
updateIsSubscribed(requestOrigin);
updatePlans();
} catch (err) {
console.error(err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ export function updateIsOnTrialPeriod() {
};
}

export function updateIsSubscribed(
isOnResume = false,
requestOrigin = 'unkwnown'
) {
export function updateIsSubscribed(requestOrigin = 'unkwnown') {
return async (dispatch, getState) => {
let isSubscribed = false;
let ownedProduct = '';
let status = NOT_SUBSCRIBED;
let expiryDate = null;
const state = getState();
dispatch(
updateSubscription({
lastUpdated: new Date().getTime()
})
);

try {
if (!isLogged(state)) {
dispatch(
Expand All @@ -81,9 +84,6 @@ export function updateIsSubscribed(
);
} else {
if (isAndroid() && state.subscription.status === PROCCESING) {
//If just close the subscribe google play modal
if (isOnResume) return;

const localReceipts = window.CdvPurchase.store.localReceipts;
if (localReceipts.length) {
//Restore purchases to pass to approved
Expand Down
RodriSanchez1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
updateIsOnTrialPeriod,
showPremiumRequired
} from './SubscriptionProvider.actions';
import { onCvaResume, cleanUpCvaOnResume } from '../../cordova-util';
import {
ACTIVE,
CANCELED,
Expand All @@ -26,20 +25,6 @@ export class SubscriptionProvider extends Component {
children: PropTypes.node.isRequired
};

onResume = async () => {
const {
updateIsSubscribed,
updateIsInFreeCountry,
updateIsOnTrialPeriod
} = this.props;
const isOnResume = true;
const requestOrigin =
'Function: onCvaResume() - Component: SubscriptionProvider';
await updateIsSubscribed(isOnResume, requestOrigin);
updateIsInFreeCountry();
updateIsOnTrialPeriod();
};

async componentDidMount() {
const {
isLogged,
Expand All @@ -50,11 +35,9 @@ export class SubscriptionProvider extends Component {
updatePlans
} = this.props;

if (isCordova()) onCvaResume(this.onResume);

const requestOrigin =
'Function: componentDidMount - Component: SubscriptionProvider';
const isSubscribed = await updateIsSubscribed(false, requestOrigin);
const isSubscribed = await updateIsSubscribed(requestOrigin);
const isInFreeCountry = updateIsInFreeCountry();
const isOnTrialPeriod = updateIsOnTrialPeriod();
await updatePlans();
Expand All @@ -74,7 +57,7 @@ export class SubscriptionProvider extends Component {
if (prevProps.isLogged !== isLogged) {
const requestOrigin =
'Function: componentDidUpdate - Component: SubscriptionProvider';
const isSubscribed = await updateIsSubscribed(false, requestOrigin);
const isSubscribed = await updateIsSubscribed(requestOrigin);
const isInFreeCountry = updateIsInFreeCountry();
const isOnTrialPeriod = updateIsOnTrialPeriod();
if (!isInFreeCountry && !isOnTrialPeriod && !isSubscribed && isLogged) {
Expand All @@ -83,10 +66,6 @@ export class SubscriptionProvider extends Component {
}
};

componentWillUnmount() {
if (isCordova()) cleanUpCvaOnResume(this.onResume);
}

configPurchaseValidator = () => {
window.CdvPurchase.store.validator = async function(receipt, callback) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const initialState = {
price: '',
tag: ''
}
]
],
lastUpdated: null
RodriSanchez1 marked this conversation as resolved.
Show resolved Hide resolved
};

function subscriptionProviderReducer(state = initialState, action) {
Expand Down