Files
notesnook/apps/mobile/src/components/NoteItem/index.js

273 lines
8.1 KiB
JavaScript
Raw Normal View History

2020-01-16 19:55:52 +05:00
import React from 'react';
import {Dimensions, Text, View, TouchableOpacity} from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-11-01 09:22:28 +05:00
import {ActionIcon} from '../ActionIcon';
import {ActionSheetEvent, updateEvent} from '../DialogManager/recievers';
2020-11-01 09:22:28 +05:00
import {timeSince} from '../../utils/TimeUtils';
import {ph, SIZE, WEIGHT} from '../../utils/SizeUtils';
2020-11-09 17:32:31 +05:00
import Paragraph from '../Typography/Paragraph';
import Heading from '../Typography/Heading';
import {db} from '../../utils/DB';
import {Actions} from '../../provider/Actions';
import NavigationService from '../../services/Navigation';
import {COLORS_NOTE} from '../../utils/Colors';
2019-12-05 17:40:09 +05:00
2020-01-16 19:55:52 +05:00
export default class NoteItem extends React.Component {
constructor(props) {
super(props);
2020-01-17 21:05:53 +05:00
this.cipher = {
value: false,
};
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-16 19:55:52 +05:00
this.actionSheet;
this.show = null;
this.setMenuRef = {};
this.willRefresh = false;
}
shouldComponentUpdate(nextProps, nextState) {
2020-02-07 00:26:44 +05:00
if (
nextProps.item.locked !== this.cipher.value ||
2020-03-03 11:01:50 +05:00
nextProps.item.colors.length !== this.colors.length ||
nextProps.selectionMode !== this.props.selectionMode
2020-02-07 00:26:44 +05:00
) {
2020-01-17 21:05:53 +05:00
return true;
} else {
return (
JSON.stringify(nextProps) !== JSON.stringify(this.props) ||
nextState !== this.state
);
}
}
componentDidUpdate() {
2020-02-07 00:26:44 +05:00
this.colors = [...this.props.item.colors];
2020-01-17 21:05:53 +05:00
this.cipher.value = this.props.item.locked ? true : false;
}
componentWillUnmount() {
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-17 21:05:53 +05:00
this.cipher.value = false;
}
componentDidMount() {
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-17 21:05:53 +05:00
if (this.props.item.locked) {
this.cipher.value = true;
}
2020-01-16 19:55:52 +05:00
}
2019-12-09 13:17:40 +05:00
2020-01-16 19:55:52 +05:00
render() {
2020-09-09 14:55:59 +05:00
let {colors, item, customStyle, isTrash} = this.props;
2020-01-16 19:55:52 +05:00
return (
<View
style={[
{
justifyContent: 'flex-start',
2019-12-21 09:46:08 +05:00
alignItems: 'center',
2020-01-16 19:55:52 +05:00
flexDirection: 'row',
2020-09-21 15:40:19 +05:00
width: '100%',
2020-02-02 23:43:46 +05:00
paddingRight: 6,
2020-01-16 19:55:52 +05:00
alignSelf: 'center',
borderBottomWidth: 1,
2020-09-09 14:55:59 +05:00
height: 100,
borderBottomColor: colors.nav,
2020-01-16 19:55:52 +05:00
},
customStyle ? customStyle : {},
]}>
2020-09-09 14:55:59 +05:00
<View
2020-01-16 19:55:52 +05:00
style={{
2020-09-21 15:40:19 +05:00
width: '92%',
2020-11-09 17:32:31 +05:00
paddingRight: 5,
2020-01-16 19:55:52 +05:00
}}>
{item.notebook && item.notebook.id && (
<View
style={{
flexDirection: 'row',
}}>
<TouchableOpacity
activeOpacity={1}
onPress={() => {
let notebook = db.notebooks.notebook(item.notebook.id).data;
updateEvent({
type: Actions.HEADER_TEXT_STATE,
state: {
heading: notebook.title,
},
});
updateEvent({
type: Actions.HEADER_STATE,
state: false,
});
NavigationService.navigate('Notebook', {
notebook: db.notebooks.notebook(item.notebook.id).data,
title: notebook.title,
root: true,
});
}}
style={{
paddingVertical: 1.5,
marginBottom: 2.5,
}}>
<Paragraph
size={SIZE.xs}
color={
item.colors[0] ? COLORS_NOTE[item.colors[0]] : colors.pri
}>
{db.notebooks.notebook(item.notebook.id).title}
</Paragraph>
</TouchableOpacity>
</View>
)}
<Heading
color={COLORS_NOTE[item.colors[0]]}
numberOfLines={1}
size={SIZE.md}>
2020-09-09 14:55:59 +05:00
{item.title.replace('\n', '')}
2020-11-09 17:32:31 +05:00
</Heading>
<Paragraph numberOfLines={2}>
{item.headline[item.headline.length - 1] === '\n'
? item.headline.slice(0, item.headline.length - 1)
: item.headline}
</Paragraph>
2020-09-09 14:55:59 +05:00
<View
style={{
2020-11-09 17:32:31 +05:00
flexDirection: 'row',
2020-09-09 14:55:59 +05:00
justifyContent: 'flex-start',
2020-11-09 17:32:31 +05:00
alignItems: 'center',
2020-09-09 14:55:59 +05:00
width: '100%',
2020-11-09 17:32:31 +05:00
marginTop: 5,
2020-09-09 14:55:59 +05:00
}}>
2020-11-09 17:32:31 +05:00
{!isTrash ? (
<>
{item.colors.length > 0 ? (
<View
2020-04-06 13:38:03 +05:00
style={{
2020-09-09 14:55:59 +05:00
marginRight: 10,
2020-11-09 17:32:31 +05:00
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
2020-04-20 09:16:01 +05:00
}}>
2020-11-09 17:32:31 +05:00
{item.colors.map((item) => (
<View
key={item}
style={{
width: SIZE.xs,
height: SIZE.xs,
borderRadius: 100,
backgroundColor: COLORS_NOTE[item],
2020-11-09 17:32:31 +05:00
marginRight: -4.5,
}}
/>
))}
</View>
) : null}
2020-09-09 14:55:59 +05:00
2020-11-09 17:32:31 +05:00
{item.locked ? (
<Icon
style={{marginRight: 10}}
name="lock"
size={SIZE.xs}
color={colors.icon}
/>
) : null}
2020-09-09 14:55:59 +05:00
2020-11-09 17:32:31 +05:00
{item.favorite ? (
<Icon
style={{marginRight: 10}}
name="star"
size={SIZE.xs}
2020-11-09 17:32:31 +05:00
color="orange"
/>
) : null}
<Text
2020-09-09 14:55:59 +05:00
style={{
2020-11-09 17:32:31 +05:00
color: colors.icon,
fontSize: SIZE.xs,
2020-11-09 17:32:31 +05:00
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
marginRight: 10,
2020-09-09 14:55:59 +05:00
}}>
2020-11-09 17:32:31 +05:00
{timeSince(item.dateCreated)}
</Text>
</>
) : null}
{isTrash ? (
<>
<Text
style={{
color: colors.accent,
fontSize: SIZE.xs,
2020-11-09 17:32:31 +05:00
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
{item.itemType[0].toUpperCase() +
item.itemType.slice(1) +
' '}
</Text>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xs,
2020-11-09 17:32:31 +05:00
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
Deleted on{' '}
{item && item.dateDeleted
? new Date(item.dateDeleted).toISOString().slice(0, 10)
: null + ' '}
</Text>
</>
) : null}
{item.conflicted ? (
<View
style={{
backgroundColor: colors.errorText,
borderRadius: 2.5,
paddingHorizontal: 4,
position: 'absolute',
right: 20,
}}>
<Text
style={{
fontSize: SIZE.xs,
2020-11-09 17:32:31 +05:00
color: 'white',
}}>
CONFLICTS
</Text>
</View>
) : null}
2020-09-09 14:55:59 +05:00
</View>
</View>
2019-11-28 21:40:37 +05:00
2020-09-21 15:40:19 +05:00
<ActionIcon
color={colors.heading}
name="dots-horizontal"
size={SIZE.xl}
2020-09-07 21:23:38 +05:00
onPress={() => {
ActionSheetEvent(
item,
isTrash ? false : true,
isTrash ? false : true,
isTrash
? ['Remove', 'Restore']
2020-09-14 13:14:07 +05:00
: ['Add to', 'Share', 'Export', 'Delete', 'Copy'],
2020-09-07 21:23:38 +05:00
isTrash ? [] : ['Pin', 'Favorite', 'Add to Vault'],
);
2020-09-21 15:40:19 +05:00
}}
customStyle={{
justifyContent: 'center',
height: 35,
width: 35,
borderRadius: 100,
alignItems: 'center',
}}
/>
2019-12-04 19:38:19 +05:00
</View>
2020-01-16 19:55:52 +05:00
);
}
}