2019-12-10 22:03:39 +05:00
|
|
|
import React, {useState} from 'react';
|
2020-01-18 01:04:33 +05:00
|
|
|
import {Text, TouchableOpacity, View} from 'react-native';
|
2019-12-03 22:05:47 +05:00
|
|
|
import Icon from 'react-native-vector-icons/Feather';
|
2020-01-18 01:04:33 +05:00
|
|
|
import {db} from '../../../App';
|
|
|
|
|
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import NavigationService from '../../services/NavigationService';
|
|
|
|
|
import {ToastEvent, w} from '../../utils/utils';
|
2020-01-10 18:46:52 +05:00
|
|
|
import ActionSheet from '../ActionSheet';
|
|
|
|
|
import {ActionSheetComponent} from '../ActionSheetComponent';
|
2020-01-18 01:04:33 +05:00
|
|
|
import {AddNotebookDialog} from '../AddNotebookDialog';
|
|
|
|
|
import {AddTopicDialog} from '../AddTopicDialog';
|
|
|
|
|
import {Dialog} from '../Dialog';
|
2020-01-07 16:26:30 +05:00
|
|
|
|
2019-12-05 17:40:09 +05:00
|
|
|
export const NotebookItem = ({
|
|
|
|
|
item,
|
|
|
|
|
index,
|
2019-12-09 13:17:40 +05:00
|
|
|
hideMore = false,
|
2019-12-06 11:09:45 +05:00
|
|
|
topic,
|
2019-12-05 17:40:09 +05:00
|
|
|
isTopic = false,
|
2019-12-09 13:17:40 +05:00
|
|
|
isMove = false,
|
|
|
|
|
noteToMove = null,
|
|
|
|
|
notebookID,
|
2019-12-14 16:00:16 +05:00
|
|
|
numColumns,
|
2019-12-21 09:46:08 +05:00
|
|
|
isTrash,
|
2020-01-13 17:33:41 +05:00
|
|
|
customStyle,
|
|
|
|
|
onLongPress,
|
2019-12-05 17:40:09 +05:00
|
|
|
}) => {
|
2020-01-17 21:26:01 +05:00
|
|
|
const [state, dispatch] = useTracked();
|
|
|
|
|
const {colors} = state;
|
2020-01-17 00:23:16 +05:00
|
|
|
|
|
|
|
|
///
|
2020-01-17 21:26:01 +05:00
|
|
|
const updateDB = () => {};
|
2020-01-17 00:23:16 +05:00
|
|
|
|
2019-12-09 13:17:40 +05:00
|
|
|
const [isVisible, setVisible] = useState(false);
|
|
|
|
|
const [addTopic, setAddTopic] = useState(false);
|
2019-12-21 09:46:08 +05:00
|
|
|
const [addNotebook, setAddNotebook] = useState(false);
|
2020-01-13 17:33:41 +05:00
|
|
|
let actionSheet;
|
2019-12-06 11:09:45 +05:00
|
|
|
let setMenuRef = {};
|
2019-12-09 13:17:40 +05:00
|
|
|
let show = null;
|
2020-01-10 18:46:52 +05:00
|
|
|
let willRefresh;
|
2019-12-09 13:17:40 +05:00
|
|
|
|
|
|
|
|
const deleteItem = async () => {
|
|
|
|
|
if (isTopic) {
|
2019-12-11 01:10:04 +05:00
|
|
|
await db.deleteTopicFromNotebook(notebookID, item.title);
|
2019-12-09 13:17:40 +05:00
|
|
|
ToastEvent.show('Topic moved to trash', 'success', 3000);
|
|
|
|
|
} else {
|
2019-12-11 01:10:04 +05:00
|
|
|
await db.deleteNotebooks([item]);
|
2019-12-09 13:17:40 +05:00
|
|
|
ToastEvent.show('Notebook moved to trash', 'success', 3000);
|
|
|
|
|
}
|
|
|
|
|
setVisible(false);
|
2020-01-13 17:33:41 +05:00
|
|
|
updateDB();
|
2019-12-09 13:17:40 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const navigate = () => {
|
|
|
|
|
isTopic
|
2019-12-10 22:03:39 +05:00
|
|
|
? NavigationService.navigate('Notes', {
|
|
|
|
|
...item,
|
|
|
|
|
notebookID,
|
|
|
|
|
})
|
2019-12-09 13:17:40 +05:00
|
|
|
: NavigationService.navigate('Notebook', {
|
2019-12-03 22:05:47 +05:00
|
|
|
notebook: item,
|
2019-12-09 13:17:40 +05:00
|
|
|
note: noteToMove,
|
2020-01-07 16:26:30 +05:00
|
|
|
title: hideMore ? 'Choose a topic' : item.title,
|
2019-12-09 13:17:40 +05:00
|
|
|
isMove: isMove ? true : false,
|
2019-12-05 17:40:09 +05:00
|
|
|
hideMore: hideMore ? true : false,
|
2019-12-03 22:05:47 +05:00
|
|
|
});
|
2019-12-09 13:17:40 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMenuHide = () => {
|
|
|
|
|
if (show) {
|
|
|
|
|
if (show === 'delete') {
|
|
|
|
|
setVisible(true);
|
|
|
|
|
show = null;
|
|
|
|
|
} else if (show === 'topic') {
|
2019-12-21 09:46:08 +05:00
|
|
|
isTopic ? setAddTopic(true) : setAddNotebook(true);
|
2019-12-09 13:17:40 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hideMenu = () => {
|
|
|
|
|
setMenuRef[index].hide();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const showMenu = () => {
|
|
|
|
|
setMenuRef[index].show();
|
|
|
|
|
};
|
2020-01-13 17:33:41 +05:00
|
|
|
|
2019-12-09 13:17:40 +05:00
|
|
|
return (
|
|
|
|
|
<View
|
2020-01-13 17:33:41 +05:00
|
|
|
style={[
|
|
|
|
|
{
|
|
|
|
|
paddingVertical: isTopic ? pv / 2 : pv,
|
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
paddingRight: 6,
|
|
|
|
|
marginHorizontal: 12,
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
|
borderBottomColor: colors.nav,
|
|
|
|
|
},
|
|
|
|
|
customStyle,
|
|
|
|
|
]}>
|
2019-12-09 13:17:40 +05:00
|
|
|
<Dialog
|
|
|
|
|
visible={isVisible}
|
|
|
|
|
title={`Delete ${isTopic ? 'topic' : 'notebook'}`}
|
|
|
|
|
icon="trash"
|
|
|
|
|
paragraph={`Do you want to delete this ${
|
|
|
|
|
isTopic ? 'topic' : 'notebook'
|
|
|
|
|
}?`}
|
|
|
|
|
positiveText="Delete"
|
|
|
|
|
positivePress={deleteItem}
|
|
|
|
|
close={() => setVisible(false)}
|
|
|
|
|
/>
|
|
|
|
|
<AddTopicDialog
|
|
|
|
|
visible={addTopic}
|
|
|
|
|
toEdit={item}
|
|
|
|
|
close={() => setAddTopic(false)}
|
|
|
|
|
/>
|
2019-12-21 09:46:08 +05:00
|
|
|
<AddNotebookDialog
|
|
|
|
|
visible={addNotebook}
|
|
|
|
|
toEdit={item}
|
|
|
|
|
close={() => {
|
|
|
|
|
setAddNotebook(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2019-12-09 13:17:40 +05:00
|
|
|
|
2019-12-03 22:05:47 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
2019-12-14 16:00:16 +05:00
|
|
|
width: '100%',
|
2019-12-03 22:05:47 +05:00
|
|
|
}}>
|
2020-01-07 16:26:30 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
style={{
|
|
|
|
|
width: '75%',
|
|
|
|
|
}}
|
2020-01-13 17:33:41 +05:00
|
|
|
onLongPress={onLongPress}
|
2020-01-07 16:26:30 +05:00
|
|
|
onPress={navigate}>
|
2019-12-03 22:05:47 +05:00
|
|
|
<Text
|
2019-12-14 16:00:16 +05:00
|
|
|
numberOfLines={1}
|
2019-12-03 22:05:47 +05:00
|
|
|
style={{
|
|
|
|
|
fontFamily: WEIGHT.bold,
|
2019-12-14 21:52:14 +05:00
|
|
|
fontSize: SIZE.md,
|
2019-12-03 22:05:47 +05:00
|
|
|
color: colors.pri,
|
|
|
|
|
maxWidth: '100%',
|
|
|
|
|
}}>
|
2019-12-06 11:09:45 +05:00
|
|
|
{item.title}
|
2019-12-03 22:05:47 +05:00
|
|
|
</Text>
|
2020-01-10 18:46:52 +05:00
|
|
|
{isTopic || !item.description ? null : (
|
2019-12-06 11:09:45 +05:00
|
|
|
<Text
|
2019-12-14 16:00:16 +05:00
|
|
|
numberOfLines={numColumns === 2 ? 3 : 2}
|
2019-12-06 11:09:45 +05:00
|
|
|
style={{
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
2019-12-14 21:52:14 +05:00
|
|
|
fontSize: SIZE.xs + 1,
|
2019-12-14 16:00:16 +05:00
|
|
|
lineHeight: SIZE.sm,
|
2019-12-14 21:52:14 +05:00
|
|
|
color: colors.pri,
|
2019-12-06 11:09:45 +05:00
|
|
|
maxWidth: '100%',
|
2019-12-14 16:00:16 +05:00
|
|
|
paddingVertical: 5,
|
|
|
|
|
height: numColumns === 2 ? SIZE.sm * 3.5 : null,
|
2019-12-06 11:09:45 +05:00
|
|
|
}}>
|
|
|
|
|
{item.description}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{isTopic ? null : (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginTop: 5,
|
2019-12-11 15:20:18 +05:00
|
|
|
width: '80%',
|
|
|
|
|
maxWidth: '80%',
|
2019-12-06 11:09:45 +05:00
|
|
|
}}>
|
2020-01-18 00:46:29 +05:00
|
|
|
{item && item.topics
|
|
|
|
|
? item.topics.slice(1, 4).map(topic => (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
backgroundColor: colors.accent,
|
|
|
|
|
paddingHorizontal: ph / 1.5,
|
|
|
|
|
paddingVertical: pv / 4,
|
|
|
|
|
marginRight: 10,
|
|
|
|
|
}}>
|
|
|
|
|
<Text
|
|
|
|
|
numberOfLines={1}
|
|
|
|
|
style={{
|
|
|
|
|
color: 'white',
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
|
|
|
|
fontSize: SIZE.xxs,
|
|
|
|
|
maxWidth: '100%',
|
|
|
|
|
}}>
|
|
|
|
|
{topic.title}
|
|
|
|
|
</Text>
|
|
|
|
|
</View>
|
|
|
|
|
))
|
|
|
|
|
: null}
|
2019-12-06 11:09:45 +05:00
|
|
|
</View>
|
|
|
|
|
)}
|
2019-12-03 22:05:47 +05:00
|
|
|
|
2019-12-21 09:46:08 +05:00
|
|
|
{isTrash ? (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginTop: 5,
|
|
|
|
|
}}>
|
|
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
color: colors.icon,
|
|
|
|
|
fontSize: SIZE.xs - 1,
|
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
|
|
|
|
}}>
|
|
|
|
|
{'Deleted on: ' +
|
|
|
|
|
new Date(item.dateDeleted).toISOString().slice(0, 10) +
|
|
|
|
|
' '}
|
|
|
|
|
</Text>
|
|
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
color: colors.accent,
|
|
|
|
|
fontSize: SIZE.xs - 1,
|
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
|
|
|
|
}}>
|
|
|
|
|
{item.type[0].toUpperCase() + item.type.slice(1) + ' '}
|
|
|
|
|
</Text>
|
|
|
|
|
</View>
|
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
{isTopic || isTrash ? null : (
|
2019-12-06 12:06:03 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginTop: 5,
|
|
|
|
|
}}>
|
|
|
|
|
<Icon
|
|
|
|
|
style={{width: 30}}
|
|
|
|
|
name="lock"
|
2019-12-14 16:00:16 +05:00
|
|
|
size={SIZE.xs}
|
2019-12-06 12:06:03 +05:00
|
|
|
color={colors.icon}
|
|
|
|
|
/>
|
|
|
|
|
<Icon
|
|
|
|
|
style={{width: 30}}
|
|
|
|
|
name="star"
|
2019-12-14 16:00:16 +05:00
|
|
|
size={SIZE.xs}
|
2019-12-06 12:06:03 +05:00
|
|
|
color={colors.icon}
|
|
|
|
|
/>
|
|
|
|
|
|
2019-12-06 11:09:45 +05:00
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
color: colors.accent,
|
2019-12-14 16:00:16 +05:00
|
|
|
fontSize: SIZE.xxs,
|
2019-12-06 12:06:03 +05:00
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
2019-12-06 11:09:45 +05:00
|
|
|
}}>
|
2019-12-06 12:06:03 +05:00
|
|
|
{new Date(item.dateCreated).toDateString().substring(4)}
|
2019-12-06 11:09:45 +05:00
|
|
|
</Text>
|
2019-12-06 12:06:03 +05:00
|
|
|
</View>
|
|
|
|
|
)}
|
2020-01-07 16:26:30 +05:00
|
|
|
{isTopic ? (
|
|
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
color: colors.icon,
|
|
|
|
|
fontSize: SIZE.xxs,
|
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
|
|
|
|
marginTop: 5,
|
|
|
|
|
}}>
|
|
|
|
|
{item.totalNotes.length == 1
|
|
|
|
|
? item.totalNotes + ' notes'
|
|
|
|
|
: item.totalNotes + ' note'}
|
|
|
|
|
</Text>
|
|
|
|
|
) : null}
|
2019-12-09 13:17:40 +05:00
|
|
|
</TouchableOpacity>
|
2019-12-05 17:40:09 +05:00
|
|
|
{hideMore ? null : (
|
2020-01-10 18:46:52 +05:00
|
|
|
<TouchableOpacity
|
2019-12-06 11:09:45 +05:00
|
|
|
style={{
|
2020-01-10 18:46:52 +05:00
|
|
|
width: w * 0.05,
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
minHeight: 70,
|
|
|
|
|
alignItems: 'center',
|
2019-12-06 11:09:45 +05:00
|
|
|
}}
|
2020-01-10 18:46:52 +05:00
|
|
|
onPress={() => {
|
2020-01-13 17:33:41 +05:00
|
|
|
actionSheet._setModalVisible();
|
2020-01-10 18:46:52 +05:00
|
|
|
}}>
|
|
|
|
|
<Icon name="more-horizontal" size={SIZE.lg} color={colors.icon} />
|
|
|
|
|
</TouchableOpacity>
|
2019-12-05 17:40:09 +05:00
|
|
|
)}
|
2020-01-10 18:46:52 +05:00
|
|
|
|
2019-12-05 17:40:09 +05:00
|
|
|
{hideMore && isTopic ? (
|
|
|
|
|
<TouchableOpacity
|
|
|
|
|
activeOpacity={opacity}
|
2019-12-10 22:03:39 +05:00
|
|
|
onPress={async () => {
|
2019-12-11 15:20:18 +05:00
|
|
|
if (!noteToMove.notebook.notebook) {
|
|
|
|
|
await db.addNoteToTopic(
|
|
|
|
|
notebookID,
|
|
|
|
|
item.title,
|
|
|
|
|
noteToMove.dateCreated,
|
|
|
|
|
);
|
|
|
|
|
} else if (noteToMove.notebook.notebook) {
|
|
|
|
|
await db.moveNote(noteToMove.dateCreated, noteToMove.notebook, {
|
|
|
|
|
notebook: notebookID,
|
|
|
|
|
topic: item.title,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 17:40:09 +05:00
|
|
|
NavigationService.navigate('Home');
|
2020-01-07 16:26:30 +05:00
|
|
|
ToastEvent.show(
|
|
|
|
|
`Note moved to ${item.title}`,
|
|
|
|
|
'success',
|
|
|
|
|
3000,
|
|
|
|
|
() => {},
|
|
|
|
|
'',
|
|
|
|
|
);
|
2019-12-05 17:40:09 +05:00
|
|
|
}}
|
|
|
|
|
style={{
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
width: '20%',
|
|
|
|
|
paddingHorizontal: ph - 5,
|
2019-12-07 12:05:15 +05:00
|
|
|
borderColor: colors.nav,
|
2020-01-07 16:26:30 +05:00
|
|
|
paddingVertical: pv - 5,
|
2019-12-05 17:40:09 +05:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
backgroundColor: colors.accent,
|
|
|
|
|
}}>
|
|
|
|
|
<Text
|
|
|
|
|
style={{
|
2020-01-07 16:26:30 +05:00
|
|
|
fontSize: SIZE.sm,
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
2019-12-05 17:40:09 +05:00
|
|
|
color: 'white',
|
|
|
|
|
}}>
|
|
|
|
|
Move
|
|
|
|
|
</Text>
|
|
|
|
|
</TouchableOpacity>
|
|
|
|
|
) : null}
|
2019-12-03 22:05:47 +05:00
|
|
|
</View>
|
2020-01-10 18:46:52 +05:00
|
|
|
|
|
|
|
|
<ActionSheet
|
2020-01-13 17:33:41 +05:00
|
|
|
ref={ref => (actionSheet = ref)}
|
2020-01-10 18:46:52 +05:00
|
|
|
customStyles={{
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
}}
|
|
|
|
|
indicatorColor={colors.shade}
|
|
|
|
|
initialOffsetFromBottom={1}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
onMenuHide();
|
|
|
|
|
if (willRefresh) {
|
2020-01-13 17:33:41 +05:00
|
|
|
updateDB();
|
2020-01-10 18:46:52 +05:00
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
children={
|
|
|
|
|
<ActionSheetComponent
|
|
|
|
|
item={item}
|
|
|
|
|
setWillRefresh={value => {
|
|
|
|
|
willRefresh = true;
|
|
|
|
|
}}
|
|
|
|
|
rowItems={
|
|
|
|
|
isTrash
|
|
|
|
|
? ['Restore', 'Remove']
|
|
|
|
|
: [
|
|
|
|
|
item.type == 'topic' ? 'Edit Topic' : 'Edit Notebook',
|
|
|
|
|
'Delete',
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
columnItems={['Pin', 'Favorite']}
|
|
|
|
|
close={value => {
|
|
|
|
|
if (value) {
|
|
|
|
|
show = value;
|
|
|
|
|
}
|
2020-01-13 17:33:41 +05:00
|
|
|
actionSheet._setModalVisible();
|
2020-01-10 18:46:52 +05:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2019-12-09 13:17:40 +05:00
|
|
|
</View>
|
2019-12-03 22:05:47 +05:00
|
|
|
);
|
|
|
|
|
};
|