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/implement subscription-free countries #1365

4 changes: 4 additions & 0 deletions src/components/App/App.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
SET_UNLOGGED_USER_LOCATION
} from './App.constants';

import { updateIsInFreeCountry } from '../../providers/SubscriptionProvider/SubscriptionProvider.actions';

export function updateDisplaySettings(payload = {}) {
return {
type: UPDATE_DISPLAY_SETTINGS,
Expand Down Expand Up @@ -76,6 +78,7 @@ export function updateLoggedUserLocation() {
const userLocation = await APIGetAndUpdateLocation();
if (userLocation) {
dispatch(updateUserData({ ...userData, location: userLocation }));
dispatch(updateIsInFreeCountry());
return;
}
throw new Error('unable to get location');
Expand All @@ -95,6 +98,7 @@ export function updateUnloggedUserLocation() {
const location = await API.getUserLocation();
if (location) {
dispatch(setUnloggedUserLocation(location));
dispatch(updateIsInFreeCountry());
return;
}
throw new Error('unable to get location');
Expand Down
6 changes: 4 additions & 2 deletions src/components/PremiumFeature/PremiumFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ function PremiumFeature({
children,
isOnTrialPeriod,
isSubscribed,
isInFreeCountry,
showPremiumRequired
}) {
const captured = event => {
if (isSubscribed || isOnTrialPeriod) return;
if (isInFreeCountry || isSubscribed || isOnTrialPeriod) return;
event.stopPropagation();
event.preventDefault();
showPremiumRequired();
Expand All @@ -24,7 +25,8 @@ function PremiumFeature({

const mapStateToProps = state => ({
isOnTrialPeriod: state.subscription.isOnTrialPeriod,
isSubscribed: state.subscription.isSubscribed
isSubscribed: state.subscription.isSubscribed,
isInFreeCountry: state.subscription.isInFreeCountry
});

const mapDispatchToProps = { showPremiumRequired };
Expand Down
27 changes: 24 additions & 3 deletions src/providers/SubscriptionProvider/SubscriptionProvider.actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
UPDATE_IS_IN_FREE_COUNTRY,
UPDATE_IS_ON_TRIAL_PERIOD,
UPDATE_ANDROID_SUBSCRIPTION_STATE,
UPDATE_SUBSCRIBER_ID,
Expand All @@ -10,23 +11,43 @@ import {
IN_GRACE_PERIOD,
NOT_SUBSCRIBED,
CANCELED,
ACTIVE
ACTIVE,
REQUIRING_PREMIUM_COUNTRIES
} from './SubscriptionProvider.constants';

import { isLogged } from '../../components/App/App.selectors';

export function updateIsInFreeCountry() {
return (dispatch, getState) => {
const state = getState();
const locationCode = isLogged(state)
? state.app.userData?.location?.countryCode
: state.app.unloggedUserLocation?.countryCode;
const isInFreeCountry = !REQUIRING_PREMIUM_COUNTRIES.includes(locationCode);
dispatch({
type: UPDATE_IS_IN_FREE_COUNTRY,
isInFreeCountry
});
};
}

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

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

function isUserOnTrialPeriod(createdAt) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const UPDATE_IS_ON_TRIAL_PERIOD = 'UPDATE_IS_ON_TRIAL_PERIOD';
export const UPDATE_IS_IN_FREE_COUNTRY =
'cboard/subscription/UPDATE_IS_IN_FREE_COUNTRY';
export const UPDATE_ANDROID_SUBSCRIPTION_STATE =
'cboard/subscription/UPDATE_ANDROID_SUBSCRIPTION_STATE';
export const UPDATE_SUBSCRIBER_ID = 'cboard/subscription/UPDATE_SUBSCRIBER_ID';
Expand All @@ -19,3 +21,5 @@ export const IN_GRACE_PERIOD = 'in_grace_period';
export const PAUSED = 'paused';
export const EXPIRED = 'expired';
export const ON_HOLD = 'on_hold';

export const REQUIRING_PREMIUM_COUNTRIES = ['US', 'GB']; // ISO-2 country codes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import API from '../../api';
import { isAndroid } from '../../cordova-util';

import {
updateIsInFreeCountry,
updateAndroidSubscriptionState,
updateIsSubscribed,
updateSubscription,
Expand All @@ -25,7 +26,8 @@ export class SubscriptionProvider extends Component {
const {
isSubscribed,
comprobeSubscription,
updateIsOnTrialPeriod
updateIsOnTrialPeriod,
updateIsInFreeCountry
} = this.props;

if (isAndroid()) {
Expand All @@ -34,6 +36,7 @@ export class SubscriptionProvider extends Component {
comprobeSubscription();
}
onAndroidResume(() => comprobeSubscription());
updateIsInFreeCountry();
updateIsOnTrialPeriod();
}
}
Expand All @@ -42,6 +45,7 @@ export class SubscriptionProvider extends Component {
if (isAndroid()) {
const {
isLogged,
updateIsInFreeCountry,
updateIsOnTrialPeriod,
subscriberId,
androidSubscriptionState,
Expand All @@ -57,7 +61,10 @@ export class SubscriptionProvider extends Component {
comprobeSubscription();
}
}
if (prevProps.isLogged !== isLogged) updateIsOnTrialPeriod();
if (prevProps.isLogged !== isLogged) {
updateIsInFreeCountry();
updateIsOnTrialPeriod();
}
}
};

Expand Down Expand Up @@ -165,6 +172,7 @@ const mapDispatchToProps = {
updateIsSubscribed,
updateSubscription,
comprobeSubscription,
updateIsInFreeCountry,
updateIsOnTrialPeriod
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
UPDATE_IS_IN_FREE_COUNTRY,
UPDATE_IS_ON_TRIAL_PERIOD,
UPDATE_ANDROID_SUBSCRIPTION_STATE,
UPDATE_SUBSCRIBER_ID,
Expand All @@ -24,6 +25,7 @@ const initialState = {
code: '',
message: ''
},
isInFreeCountry: false,
isOnTrialPeriod: true,
premiumRequiredModalState: {
open: false,
Expand All @@ -33,6 +35,11 @@ const initialState = {

function subscriptionProviderReducer(state = initialState, action) {
switch (action.type) {
case UPDATE_IS_IN_FREE_COUNTRY:
return {
...state,
isInFreeCountry: action.isInFreeCountry
};
case UPDATE_IS_ON_TRIAL_PERIOD:
return {
...state,
Expand Down