Files
notesnook/apps/mobile/src/components/Menu/UserSection.js

164 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-11-16 09:42:13 +05:00
import React from 'react';
2020-12-01 17:51:39 +05:00
import {ActivityIndicator, TouchableOpacity, View} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-12-01 17:51:39 +05:00
import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import {SUBSCRIPTION_STATUS_STRINGS} from '../../utils';
import {db} from '../../utils/DB';
import {eOpenLoginDialog} from '../../utils/Events';
import {pv, SIZE} from '../../utils/SizeUtils';
import {PressableButton} from '../PressableButton';
2020-11-20 01:23:05 +05:00
import Paragraph from '../Typography/Paragraph';
2020-12-01 17:51:39 +05:00
import {TimeSince} from './TimeSince';
export const UserSection = ({noTextMode}) => {
const [state, dispatch] = useTracked();
2020-09-08 22:22:33 +05:00
const {colors, syncing, user} = state;
return user && user.username ? (
<View
style={{
2020-11-16 09:42:13 +05:00
width: '100%',
borderRadius: 5,
alignSelf: 'center',
backgroundColor: colors.shade,
}}>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: colors.accent,
2020-11-16 09:42:13 +05:00
paddingHorizontal: 6,
paddingVertical: 6,
borderTopRightRadius: 5,
borderTopLeftRadius: 5,
}}>
2020-11-20 01:23:05 +05:00
<Paragraph color="white">
<Icon name="account-outline" /> {user.username}
2020-11-20 01:23:05 +05:00
</Paragraph>
<Paragraph color="white" size={SIZE.xs}>
2020-11-16 09:42:13 +05:00
{SUBSCRIPTION_STATUS_STRINGS[user.subscription.status]}
2020-11-20 01:23:05 +05:00
</Paragraph>
</View>
<TouchableOpacity
activeOpacity={0.8}
onPress={async () => {
dispatch({
2020-10-13 17:02:14 +05:00
type: Actions.SYNCING,
syncing: true,
});
try {
if (!user) {
let u = await db.user.get();
2020-10-13 17:02:14 +05:00
dispatch({type: Actions.USER, user: u});
}
await db.sync();
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
ToastEvent.show(e.message, 'error');
}
let u = await db.user.get();
2020-10-13 17:02:14 +05:00
dispatch({type: Actions.USER, user: u});
dispatch({type: Actions.ALL});
dispatch({
2020-10-13 17:02:14 +05:00
type: Actions.SYNCING,
syncing: false,
});
}}
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingRight: 5,
2020-11-16 09:42:13 +05:00
paddingVertical: 12,
}}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
}}>
2020-11-20 01:23:05 +05:00
<Paragraph
style={{
marginLeft: 5,
}}>
{syncing ? 'Syncing ' : 'Last synced: '}
{!syncing ? (
2020-10-29 16:40:02 +05:00
user?.lastSynced ? (
2020-10-28 15:52:15 +05:00
<TimeSince time={user.lastSynced} />
) : (
'never'
)
) : null}
{'\n'}
2020-11-20 01:23:05 +05:00
<Paragraph
size={SIZE.xs}
color={colors.icon}
style={{
2020-11-16 09:42:13 +05:00
fontSize: SIZE.xs,
color: colors.icon,
}}>
2020-11-16 09:42:13 +05:00
{syncing ? 'Fetching your notes ' : 'Tap to sync '}
2020-11-20 01:23:05 +05:00
</Paragraph>
</Paragraph>
</View>
2020-11-16 09:42:13 +05:00
{syncing ? (
<ActivityIndicator size={SIZE.lg} color={colors.accent} />
) : (
<Icon color={colors.accent} name="sync" size={SIZE.lg} />
)}
</TouchableOpacity>
</View>
) : (
2020-09-08 22:22:33 +05:00
<PressableButton
onPress={() => {
2020-09-19 14:32:05 +05:00
eSendEvent(eOpenLoginDialog);
}}
2020-12-01 17:51:39 +05:00
type="shade"
2020-09-08 22:22:33 +05:00
customStyle={{
paddingVertical: 12,
marginVertical: 5,
marginTop: pv + 5,
borderRadius: 5,
2020-11-20 01:23:05 +05:00
width: '100%',
flexDirection: 'row',
alignItems: 'center',
2020-11-20 01:23:05 +05:00
justifyContent: 'flex-start',
paddingHorizontal: 8,
}}>
<View
style={{
width: 30,
backgroundColor: noTextMode ? 'transparent' : colors.accent,
height: 30,
borderRadius: 100,
alignItems: 'center',
justifyContent: 'center',
}}>
<Icon
style={{
textAlign: 'center',
textAlignVertical: 'center',
}}
name={noTextMode ? 'login-variant' : 'account-outline'}
color={noTextMode ? colors.accent : 'white'}
size={noTextMode ? SIZE.md + 5 : SIZE.md + 1}
/>
</View>
{noTextMode ? null : (
<View
style={{
marginLeft: 10,
}}>
2020-11-20 01:23:05 +05:00
<Paragraph size={SIZE.xs} color={colors.icon}>
You are not logged in
2020-11-20 01:23:05 +05:00
</Paragraph>
<Paragraph color={colors.accent}>Login to sync notes.</Paragraph>
</View>
)}
2020-09-08 22:22:33 +05:00
</PressableButton>
);
};