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';
|
2020-05-10 22:16:31 +05:00
|
|
|
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';
|
2020-12-09 13:11:21 +05:00
|
|
|
import {getElevation, SUBSCRIPTION_STATUS_STRINGS} from '../../utils';
|
2020-12-01 17:51:39 +05:00
|
|
|
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';
|
2020-05-10 22:16:31 +05:00
|
|
|
|
|
|
|
|
export const UserSection = ({noTextMode}) => {
|
|
|
|
|
const [state, dispatch] = useTracked();
|
2020-09-08 22:22:33 +05:00
|
|
|
const {colors, syncing, user} = state;
|
2020-05-10 22:16:31 +05:00
|
|
|
|
|
|
|
|
return user && user.username ? (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
2020-11-16 09:42:13 +05:00
|
|
|
width: '100%',
|
2020-11-26 20:56:23 +05:00
|
|
|
borderRadius: 5,
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
backgroundColor: colors.shade,
|
2020-12-09 13:11:21 +05:00
|
|
|
marginTop:10,
|
2020-05-10 22:16:31 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
backgroundColor: colors.accent,
|
2020-11-16 09:42:13 +05:00
|
|
|
paddingHorizontal: 6,
|
2020-11-26 20:56:23 +05:00
|
|
|
paddingVertical: 6,
|
|
|
|
|
borderTopRightRadius: 5,
|
|
|
|
|
borderTopLeftRadius: 5,
|
2020-05-10 22:16:31 +05:00
|
|
|
}}>
|
2020-11-20 01:23:05 +05:00
|
|
|
<Paragraph color="white">
|
2020-05-10 22:16:31 +05:00
|
|
|
<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>
|
2020-05-10 22:16:31 +05:00
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
<TouchableOpacity
|
2020-11-26 20:56:23 +05:00
|
|
|
activeOpacity={0.8}
|
2020-05-10 22:16:31 +05:00
|
|
|
onPress={async () => {
|
|
|
|
|
dispatch({
|
2020-10-13 17:02:14 +05:00
|
|
|
type: Actions.SYNCING,
|
2020-05-10 22:16:31 +05:00
|
|
|
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});
|
2020-05-10 22:16:31 +05:00
|
|
|
}
|
|
|
|
|
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});
|
2020-05-10 22:16:31 +05:00
|
|
|
dispatch({
|
2020-10-13 17:02:14 +05:00
|
|
|
type: Actions.SYNCING,
|
2020-05-10 22:16:31 +05:00
|
|
|
syncing: false,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
2020-11-26 20:56:23 +05:00
|
|
|
paddingRight: 5,
|
2020-11-16 09:42:13 +05:00
|
|
|
paddingVertical: 12,
|
2020-05-10 22:16:31 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}>
|
2020-11-20 01:23:05 +05:00
|
|
|
<Paragraph
|
2020-05-10 22:16:31 +05:00
|
|
|
style={{
|
|
|
|
|
marginLeft: 5,
|
|
|
|
|
}}>
|
2020-11-26 20:56:23 +05:00
|
|
|
{syncing ? 'Syncing ' : 'Last synced: '}
|
2020-05-10 22:16:31 +05:00
|
|
|
{!syncing ? (
|
2020-10-29 16:40:02 +05:00
|
|
|
user?.lastSynced ? (
|
2020-10-28 15:52:15 +05:00
|
|
|
<TimeSince time={user.lastSynced} />
|
2020-05-10 22:16:31 +05:00
|
|
|
) : (
|
|
|
|
|
'never'
|
|
|
|
|
)
|
|
|
|
|
) : null}
|
|
|
|
|
{'\n'}
|
2020-11-20 01:23:05 +05:00
|
|
|
<Paragraph
|
|
|
|
|
size={SIZE.xs}
|
|
|
|
|
color={colors.icon}
|
2020-05-10 22:16:31 +05:00
|
|
|
style={{
|
2020-11-16 09:42:13 +05:00
|
|
|
fontSize: SIZE.xs,
|
2020-05-10 22:16:31 +05:00
|
|
|
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>
|
2020-05-10 22:16:31 +05:00
|
|
|
</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} />
|
|
|
|
|
)}
|
2020-05-10 22:16:31 +05:00
|
|
|
</TouchableOpacity>
|
|
|
|
|
</View>
|
|
|
|
|
) : (
|
2020-09-08 22:22:33 +05:00
|
|
|
<PressableButton
|
2020-05-10 22:16:31 +05:00
|
|
|
onPress={() => {
|
2020-09-19 14:32:05 +05:00
|
|
|
eSendEvent(eOpenLoginDialog);
|
2020-05-10 22:16:31 +05:00
|
|
|
}}
|
2020-12-01 17:51:39 +05:00
|
|
|
type="shade"
|
2020-09-08 22:22:33 +05:00
|
|
|
customStyle={{
|
2020-05-10 22:16:31 +05:00
|
|
|
paddingVertical: 12,
|
|
|
|
|
marginVertical: 5,
|
|
|
|
|
marginTop: pv + 5,
|
2020-11-26 20:56:23 +05:00
|
|
|
borderRadius: 5,
|
2020-11-20 01:23:05 +05:00
|
|
|
width: '100%',
|
2020-05-10 22:16:31 +05:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
2020-11-20 01:23:05 +05:00
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
paddingHorizontal: 8,
|
2020-05-10 22:16:31 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: 30,
|
2020-10-01 17:23:04 +05:00
|
|
|
backgroundColor: noTextMode ? 'transparent' : colors.accent,
|
2020-05-10 22:16:31 +05:00
|
|
|
height: 30,
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
}}>
|
|
|
|
|
<Icon
|
|
|
|
|
style={{
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
}}
|
2020-10-01 17:23:04 +05:00
|
|
|
name={noTextMode ? 'login-variant' : 'account-outline'}
|
|
|
|
|
color={noTextMode ? colors.accent : 'white'}
|
|
|
|
|
size={noTextMode ? SIZE.md + 5 : SIZE.md + 1}
|
2020-05-10 22:16:31 +05:00
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
{noTextMode ? null : (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
marginLeft: 10,
|
|
|
|
|
}}>
|
2020-11-20 01:23:05 +05:00
|
|
|
<Paragraph size={SIZE.xs} color={colors.icon}>
|
2020-05-10 22:16:31 +05:00
|
|
|
You are not logged in
|
2020-11-20 01:23:05 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
<Paragraph color={colors.accent}>Login to sync notes.</Paragraph>
|
2020-05-10 22:16:31 +05:00
|
|
|
</View>
|
|
|
|
|
)}
|
2020-09-08 22:22:33 +05:00
|
|
|
</PressableButton>
|
2020-05-10 22:16:31 +05:00
|
|
|
);
|
|
|
|
|
};
|