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

Feature/show trial period ended when open the app #1348

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/providers/SubscriptionProvider/SubscriptionProvider.actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
UPDATE_IS_ON_TRIAL_PERIOD,
UPDATE_ANDROID_SUBSCRIPTION_STATE,
UPDATE_SUBSCRIBER_ID,
UPDATE_IS_SUBSCRIBED,
Expand All @@ -12,6 +13,33 @@ import {
ACTIVE
} from './SubscriptionProvider.constants';

export function updateIsOnTrialPeriod() {
return (dispatch, getState) => {
const userCreatedAt = getState().app.userData.createdAt;
const { isSubscribed } = getState().subscription;
const isOnTrialPeriod = isUserOnTrialPeriod(userCreatedAt);
dispatch({
type: UPDATE_IS_ON_TRIAL_PERIOD,
isOnTrialPeriod
});

if (!isOnTrialPeriod && !isSubscribed)
dispatch(showPremiumRequired({ showTryPeriodFinishedMessages: true }));

function isUserOnTrialPeriod(createdAt) {
if (!createdAt) return false; //this case are already created users
const createdAtDate = new Date(createdAt);
const actualDate = new Date();
const DAYS_TO_TRY = 30;
const tryLimitDate = createdAtDate.setDate(
createdAtDate.getDate() + DAYS_TO_TRY
);
if (actualDate >= tryLimitDate) return false;
return true;
}
};
}

export function updateAndroidSubscriptionState(payload = {}) {
return {
type: UPDATE_ANDROID_SUBSCRIPTION_STATE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const UPDATE_IS_ON_TRIAL_PERIOD = 'UPDATE_IS_ON_TRIAL_PERIOD';
export const UPDATE_ANDROID_SUBSCRIPTION_STATE =
'cboard/subscription/UPDATE_ANDROID_SUBSCRIPTION_STATE';
export const UPDATE_SUBSCRIBER_ID = 'cboard/subscription/UPDATE_SUBSCRIBER_ID';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
updateAndroidSubscriptionState,
updateIsSubscribed,
updateSubscription,
comprobeSubscription
comprobeSubscription,
updateIsOnTrialPeriod
} from './SubscriptionProvider.actions';
import { onAndroidResume } from '../../cordova-util';
import { NOT_SUBSCRIBED, PROCCESING } from './SubscriptionProvider.constants';
import { isLogged } from '../../components/App/App.selectors';

export class SubscriptionProvider extends Component {
static propTypes = {
Expand All @@ -21,15 +23,34 @@ export class SubscriptionProvider extends Component {

componentDidMount() {
const { isSubscribed, comprobeSubscription } = this.props;

const manageTrialPeriod = () => {
const { isLogged, updateIsOnTrialPeriod } = this.props;
if (isLogged) {
updateIsOnTrialPeriod();
}
};

if (isAndroid()) {
this.configInAppPurchasePlugin();
if (isSubscribed) {
comprobeSubscription();
}
onAndroidResume(() => comprobeSubscription());
manageTrialPeriod();
}
}

componentDidUpdate = prevProps => {
if (isAndroid()) {
const { isLogged, isOnTrialPeriod, updateIsOnTrialPeriod } = this.props;
if (!prevProps.isLogged && isLogged)
if (isOnTrialPeriod === undefined || isOnTrialPeriod) {
updateIsOnTrialPeriod();
}
}
};

configPurchaseValidator = () => {
let count = 1;
window.CdvPurchase.store.validator = async function(receipt, callback) {
Expand Down Expand Up @@ -120,14 +141,17 @@ export class SubscriptionProvider extends Component {
const mapStateToProps = state => ({
isSubscribed: state.subscription.isSubscribed,
expiryDate: state.subscription.expiryDate,
androidSubscriptionState: state.subscription.androidSubscriptionState
androidSubscriptionState: state.subscription.androidSubscriptionState,
isOnTrialPeriod: state.subscription.isOnTrialPeriod,
isLogged: isLogged(state)
});

const mapDispatchToProps = {
updateAndroidSubscriptionState,
updateIsSubscribed,
updateSubscription,
comprobeSubscription
comprobeSubscription,
updateIsOnTrialPeriod
};

export default connect(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
UPDATE_IS_ON_TRIAL_PERIOD,
UPDATE_ANDROID_SUBSCRIPTION_STATE,
UPDATE_SUBSCRIBER_ID,
UPDATE_IS_SUBSCRIBED,
Expand Down Expand Up @@ -28,6 +29,11 @@ const initialState = {

function subscriptionProviderReducer(state = initialState, action) {
switch (action.type) {
case UPDATE_IS_ON_TRIAL_PERIOD:
return {
...state,
isOnTrialPeriod: action.isOnTrialPeriod
};
case UPDATE_ANDROID_SUBSCRIPTION_STATE:
return {
...state,
Expand Down Expand Up @@ -80,7 +86,8 @@ function subscriptionProviderReducer(state = initialState, action) {
...state,
premiumRequiredModalState: {
open: false,
showTryPeriodFinishedMessages: false
showTryPeriodFinishedMessages:
state.premiumRequiredModalState.showTryPeriodFinishedMessages
}
};
default:
Expand Down