Files
notesnook/apps/mobile/src/screens/settings/usersection.js

343 lines
13 KiB
JavaScript
Raw Normal View History

2021-11-24 09:57:23 +05:00
import dayjs from 'dayjs';
2021-11-24 10:39:09 +05:00
import React from 'react';
2022-01-22 12:57:05 +05:00
import { Linking, Platform, View } from 'react-native';
2021-11-24 09:57:23 +05:00
import * as RNIap from 'react-native-iap';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2022-02-28 13:48:59 +05:00
import { ChangePassword } from '../../components/auth/change-password';
2022-04-01 00:56:10 +05:00
import { Progress } from '../../components/sheets/progress';
2022-02-28 13:48:59 +05:00
import { Button } from '../../components/ui/button';
import Seperator from '../../components/ui/seperator';
import Heading from '../../components/ui/typography/heading';
import Paragraph from '../../components/ui/typography/paragraph';
2022-02-28 23:25:18 +05:00
import { eSendEvent, presentSheet, ToastEvent } from '../../services/event-manager';
import PremiumService from '../../services/premium';
import Sync from '../../services/sync';
2022-03-09 19:04:01 +05:00
import { useUserStore } from '../../stores/stores';
import { useThemeStore } from '../../stores/theme';
2021-11-24 09:57:23 +05:00
import {
SUBSCRIPTION_PROVIDER,
SUBSCRIPTION_STATUS,
SUBSCRIPTION_STATUS_STRINGS
2022-02-28 13:48:59 +05:00
} from '../../utils/constants';
2021-11-24 09:57:23 +05:00
import {
eCloseProgressDialog,
2022-03-07 15:19:07 +05:00
eOpenAttachmentsDialog,
2021-11-24 09:57:23 +05:00
eOpenPremiumDialog,
eOpenRecoveryKeyDialog
2022-02-28 13:48:59 +05:00
} from '../../utils/events';
2022-04-05 00:50:02 +05:00
import { usePricing } from '../../utils/hooks/use-pricing';
2022-02-28 13:48:59 +05:00
import { SIZE } from '../../utils/size';
import { sleep } from '../../utils/time';
2022-03-18 14:40:15 +05:00
import TwoFactorAuth from './2fa';
2022-01-22 12:57:05 +05:00
import { CustomButton } from './button';
import { verifyUser } from './functions';
2021-11-24 09:57:23 +05:00
const getTimeLeft = t2 => {
let daysRemaining = dayjs(t2).diff(dayjs(), 'days');
return {
time: dayjs(t2).diff(dayjs(), daysRemaining === 0 ? 'hours' : 'days'),
isHour: daysRemaining === 0
};
};
const SettingsUserSection = () => {
2022-02-28 23:25:18 +05:00
const colors = useThemeStore(state => state.colors);
2021-11-24 09:57:23 +05:00
const user = useUserStore(state => state.user);
2022-01-22 12:57:05 +05:00
const subscriptionDaysLeft = user && getTimeLeft(parseInt(user.subscription?.expiry));
2021-11-24 09:57:23 +05:00
const isExpired = user && subscriptionDaysLeft.time < 0;
const expiryDate = dayjs(user?.subscription?.expiry).format('MMMM D, YYYY');
const startDate = dayjs(user?.subscription?.start).format('MMMM D, YYYY');
2022-04-05 00:50:02 +05:00
const monthlyPlan = usePricing('monthly');
2021-11-24 09:57:23 +05:00
const manageSubscription = () => {
if (!user.isEmailConfirmed) {
PremiumService.showVerifyEmailDialog();
return;
}
if (
user.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_CANCELLED &&
Platform.OS === 'android'
) {
if (user.subscription?.provider === 3) {
ToastEvent.show({
heading: 'Subscribed on web',
message: 'Open your web browser to manage your subscription.',
type: 'success'
});
return;
}
Linking.openURL(
Platform.OS === 'ios'
? 'https://apps.apple.com/account/subscriptions'
: 'https://play.google.com/store/account/subscriptions'
);
} else {
eSendEvent(eOpenPremiumDialog);
}
};
return (
<>
{user ? (
<>
<View
style={{
paddingHorizontal: 12,
marginTop: 15,
marginBottom: 15
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
alignSelf: 'center',
width: '100%',
paddingVertical: 12,
backgroundColor: colors.bg,
borderRadius: 5
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
justifyContent: 'space-between',
alignItems: 'center',
flexDirection: 'row',
paddingBottom: 4,
borderBottomWidth: 1,
borderColor: colors.accent
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<View
style={{
borderWidth: 1,
borderRadius: 100,
borderColor: colors.accent,
width: 20,
height: 20,
alignItems: 'center',
justifyContent: 'center'
2022-01-22 12:57:05 +05:00
}}
>
<Icon size={SIZE.md} color={colors.accent} name="account-outline" />
2021-11-24 09:57:23 +05:00
</View>
<Paragraph
color={colors.heading}
size={SIZE.sm}
style={{
marginLeft: 5
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
{user?.email}
</Paragraph>
</View>
<View
style={{
borderRadius: 5,
padding: 5,
paddingVertical: 2.5
2022-01-22 12:57:05 +05:00
}}
>
2021-11-24 09:57:23 +05:00
<Heading color={colors.accent} size={SIZE.sm}>
{SUBSCRIPTION_STATUS_STRINGS[user.subscription?.type]}
</Heading>
</View>
</View>
<View>
{user.subscription?.type !== SUBSCRIPTION_STATUS.BASIC ? (
<View>
<Seperator />
<Paragraph
size={SIZE.lg}
style={{
textAlign: 'center'
}}
color={
2022-01-22 12:57:05 +05:00
(subscriptionDaysLeft.time > 5 && !subscriptionDaysLeft.isHour) ||
2021-11-24 09:57:23 +05:00
user.subscription?.type !== 6
? colors.accent
: colors.red
2022-01-22 12:57:05 +05:00
}
>
2021-11-24 09:57:23 +05:00
{isExpired
? 'Your subscription has ended.'
: user.subscription?.type === 1
? `Your free trial has started`
: `Subscribed to Notesnook Pro`}
</Paragraph>
<Paragraph
style={{
textAlign: 'center'
}}
2022-01-22 12:57:05 +05:00
color={colors.pri}
>
2021-11-24 09:57:23 +05:00
{user.subscription?.type === 2
? 'You signed up on ' + startDate
: user.subscription?.type === 1
? 'Your free trial will end on ' + expiryDate
: user.subscription?.type === 6
? subscriptionDaysLeft.time < -3
? 'Your subscription has ended'
: 'Your account will be downgraded to Basic in 3 days'
: user.subscription?.type === 7
? `Your subscription will end on ${expiryDate}.`
: user.subscription?.type === 5
? `Your subscription will renew on ${expiryDate}.`
: null}
</Paragraph>
</View>
) : null}
{user.subscription?.type !== SUBSCRIPTION_STATUS.PREMIUM &&
user.subscription?.type !== SUBSCRIPTION_STATUS.BETA && (
<>
<Seperator />
<Button
onPress={manageSubscription}
width="100%"
style={{
paddingHorizontal: 0
}}
fontSize={SIZE.md}
title={
!user.isEmailConfirmed
2022-02-18 23:25:52 +05:00
? 'Confirm your email'
2021-11-24 09:57:23 +05:00
: user.subscription?.provider === 3 &&
2022-01-22 12:57:05 +05:00
user.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_CANCELLED
2021-11-24 09:57:23 +05:00
? 'Manage subscription from desktop app'
2022-01-22 12:57:05 +05:00
: user.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_CANCELLED &&
2021-11-24 09:57:23 +05:00
Platform.OS === 'android'
? `Resubscribe from Google Playstore`
2022-01-22 12:57:05 +05:00
: user.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_EXPIRED
2022-04-05 00:50:02 +05:00
? `Resubscribe to Notesnook Pro (${monthlyPlan?.product?.localizedPrice} / mo)`
: `Subscribe to Notesnook Pro (${monthlyPlan?.product?.localizedPrice} / mo)`
2021-11-24 09:57:23 +05:00
}
height={50}
type="accent"
/>
</>
)}
</View>
{user?.subscription?.provider &&
user.subscription?.type !== SUBSCRIPTION_STATUS.PREMIUM_EXPIRED &&
user.subscription?.type !== SUBSCRIPTION_STATUS.BASIC &&
SUBSCRIPTION_PROVIDER[user?.subscription?.provider] ? (
<Button
2022-01-22 12:57:05 +05:00
title={SUBSCRIPTION_PROVIDER[user?.subscription?.provider]?.title}
2021-11-24 09:57:23 +05:00
onPress={() => {
presentSheet({
2022-01-22 12:57:05 +05:00
title: SUBSCRIPTION_PROVIDER[user?.subscription?.provider].title,
paragraph: SUBSCRIPTION_PROVIDER[user?.subscription?.provider].desc
2021-11-24 09:57:23 +05:00
});
}}
style={{
alignSelf: 'flex-end',
marginTop: 10,
borderRadius: 3,
zIndex: 10
}}
fontSize={11}
textStyle={{
fontWeight: 'normal'
}}
height={20}
type="accent"
/>
) : null}
</View>
</View>
{[
{
name: 'Save data recovery key',
func: async () => {
2021-11-24 10:39:09 +05:00
verifyUser(null, async () => {
await sleep(300);
eSendEvent(eOpenRecoveryKeyDialog);
});
2021-11-24 09:57:23 +05:00
},
desc: 'Recover your data using the recovery key if your password is lost.'
},
2022-03-07 15:19:07 +05:00
{
name: 'Manage attachments',
func: () => {
eSendEvent(eOpenAttachmentsDialog);
},
desc: 'Manage all attachments in one place.'
},
{
name: 'Change password',
func: async () => {
2022-02-07 14:44:48 +05:00
ChangePassword.present();
},
desc: 'Setup a new password for your account.'
},
2021-11-24 09:57:23 +05:00
{
name: 'Having problems with syncing?',
func: async () => {
2022-04-01 00:56:10 +05:00
Progress.present();
2021-11-24 09:57:23 +05:00
await Sync.run('global', true);
eSendEvent(eCloseProgressDialog);
2021-11-24 09:57:23 +05:00
},
desc: 'Try force sync to resolve issues with syncing.'
},
{
name: 'Subscription not activated?',
func: async () => {
if (Platform.OS === 'android') return;
presentSheet({
title: 'Loading subscriptions',
paragraph: `Please wait while we fetch your subscriptions.`
});
let subscriptions = await RNIap.getPurchaseHistory();
2022-01-22 12:57:05 +05:00
subscriptions.sort((a, b) => b.transactionDate - a.transactionDate);
2021-11-24 09:57:23 +05:00
let currentSubscription = subscriptions[0];
presentSheet({
title: 'Notesnook Pro',
paragraph: `You subscribed to Notesnook Pro on ${new Date(
currentSubscription.transactionDate
).toLocaleString()}. Verify this subscription?`,
action: async () => {
presentSheet({
title: 'Verifying subscription',
paragraph: `Please wait while we verify your subscription.`
});
2022-01-22 12:57:05 +05:00
await PremiumService.subscriptions.verify(currentSubscription);
2021-11-24 09:57:23 +05:00
eSendEvent(eCloseProgressDialog);
},
icon: 'information-outline',
2022-01-22 12:57:05 +05:00
actionText: 'Verify'
2021-11-24 09:57:23 +05:00
});
},
desc: 'Verify your subscription to Notesnook Pro'
}
].map(item =>
item.name === 'Subscription not activated?' &&
(Platform.OS !== 'ios' || PremiumService.get()) ? null : (
<CustomButton
key={item.name}
title={item.name}
onPress={item.func}
tagline={item.desc}
color={item.name === 'Logout' ? colors.errorText : colors.pri}
/>
)
)}
2022-03-18 14:40:15 +05:00
<TwoFactorAuth />
2021-11-24 09:57:23 +05:00
</>
) : null}
</>
);
};
export default SettingsUserSection;