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

297 lines
8.5 KiB
JavaScript
Raw Normal View History

2020-01-18 18:14:57 +05:00
import React from 'react';
2020-09-09 12:00:20 +05:00
import { Text, TouchableOpacity, View } from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-09-09 12:00:20 +05:00
import { ph, pv, SIZE, WEIGHT } from '../../common/common';
import { useTracked } from '../../provider';
import { ACTIONS } from '../../provider/actions';
2020-03-21 10:03:11 +05:00
import NavigationService from '../../services/NavigationService';
2020-09-09 12:00:20 +05:00
import { db, ToastEvent } from '../../utils/utils';
import { Button } from '../Button';
import { ActionSheetEvent, moveNoteHideEvent } from '../DialogManager/recievers';
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,
customStyle,
onLongPress,
2020-01-18 18:14:57 +05:00
navigation,
2020-03-03 11:25:49 +05:00
selectionMode,
pinned,
2019-12-05 17:40:09 +05:00
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-02-02 16:18:52 +05:00
const {colors, selectedItemsList} = state;
2020-01-17 00:23:16 +05:00
2019-12-09 13:17:40 +05:00
const navigate = () => {
2020-03-03 11:25:49 +05:00
if (selectionMode) {
onLongPress();
return;
}
2020-03-04 11:01:43 +05:00
if (isTopic) {
NavigationService.navigate('Notes', {
...item,
});
} else {
dispatch({
type: ACTIONS.HEADER_TEXT_STATE,
state: {
heading: hideMore ? 'Move to topic' : item.title,
},
});
dispatch({
type: ACTIONS.HEADER_STATE,
state: {
2020-09-09 11:10:35 +05:00
canGoBack: true,
menu: false,
},
});
dispatch({
type: ACTIONS.CONTAINER_BOTTOM_BUTTON,
state: {
bottomButtonText: 'Add new topic',
},
});
2020-03-04 11:01:43 +05:00
hideMore
? navigation.navigate('Notebook', {
notebook: item,
title: hideMore ? 'Move to topic' : item.title,
2020-03-04 11:01:43 +05:00
isMove: isMove,
hideMore: hideMore,
2020-09-09 11:10:35 +05:00
root: false,
2020-03-04 11:01:43 +05:00
})
: NavigationService.navigate('Notebook', {
notebook: item,
title: hideMore ? 'Select a topic' : item.title,
2020-03-04 11:01:43 +05:00
isMove: isMove,
hideMore: hideMore,
2020-09-09 11:10:35 +05:00
root: true,
2020-03-04 11:01:43 +05:00
});
}
2019-12-09 13:17:40 +05:00
};
return (
<View
style={[
{
paddingVertical: isTopic ? pv / 2 : pv,
justifyContent: 'flex-start',
alignItems: 'center',
flexDirection: 'row',
paddingRight: 6,
alignSelf: 'center',
borderBottomWidth: 1,
borderBottomColor: colors.nav,
},
customStyle,
]}>
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
2020-02-02 20:44:17 +05:00
activeOpacity={0.8}
2020-01-07 16:26:30 +05:00
style={{
2020-02-20 12:16:17 +05:00
width: hideMore ? '80%' : '90%',
maxWidth: hideMore ? '80%' : '90%',
2020-02-02 19:09:33 +05:00
minHeight: 50,
justifyContent: 'center',
2020-01-07 16:26:30 +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,
2020-02-02 19:09:33 +05:00
fontSize: SIZE.sm + 1,
2020-09-07 21:23:38 +05:00
color: colors.heading,
2019-12-03 22:05:47 +05:00
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',
2020-02-02 19:09:33 +05:00
width: '80%',
maxWidth: '80%',
2020-02-02 16:18:52 +05:00
flexWrap: 'wrap',
2019-12-06 11:09:45 +05:00
}}>
2020-01-18 00:46:29 +05:00
{item && item.topics
2020-09-09 11:10:35 +05:00
? item.topics.slice(1, 4).map((topic) => (
2020-01-18 00:46:29 +05:00
<View
2020-01-20 16:24:01 +05:00
key={topic.dateCreated.toString() + topic.title}
2020-01-18 00:46:29 +05:00
style={{
borderRadius: 5,
backgroundColor: colors.accent,
paddingHorizontal: ph / 1.5,
paddingVertical: pv / 3,
2020-02-02 23:05:14 +05:00
marginRight: 5,
marginVertical: 2.5,
2020-01-18 00:46:29 +05:00
}}>
<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,
}}>
2020-04-20 09:16:01 +05:00
{'Deleted on: ' + item && item.dateDeleted
? new Date(item.dateDeleted).toISOString().slice(0, 10)
: null + ' '}
2019-12-21 09:46:08 +05:00
</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}
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
marginTop: 5,
}}>
<Text
2019-12-06 12:06:03 +05:00
style={{
color: colors.icon,
fontSize: SIZE.xxs,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
2019-12-06 12:06:03 +05:00
}}>
{item && item.totalNotes && item.totalNotes > 1
? item.totalNotes + ' notes'
: item.totalNotes === 1
? item.totalNotes + ' note'
: '0 notes'}
</Text>
2019-12-06 12:06:03 +05:00
{isTopic || isTrash ? null : (
2019-12-06 11:09:45 +05:00
<Text
style={{
color: colors.accent,
2020-03-03 15:06:23 +05:00
marginLeft: 10,
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>
)}
</View>
2019-12-09 13:17:40 +05:00
</TouchableOpacity>
2020-02-02 19:09:33 +05:00
{hideMore ||
(item.title === 'General' && item.type === 'topic') ? 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
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-18 18:14:57 +05:00
let rowItems = isTrash
? ['Restore', 'Remove']
: [
item.type == 'topic' ? 'Edit Topic' : 'Edit Notebook',
'Delete',
];
2020-03-03 14:55:44 +05:00
let columnItems = item.type === 'topic' ? [] : ['Pin'];
2020-01-18 18:14:57 +05:00
ActionSheetEvent(item, false, false, rowItems, columnItems, {
notebookID: notebookID,
});
2020-01-10 18:46:52 +05:00
}}>
2020-09-09 11:10:35 +05:00
<Icon
name="dots-horizontal"
size={SIZE.lg}
color={colors.heading}
/>
2020-01-10 18:46:52 +05:00
</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 ? (
2020-09-09 11:10:35 +05:00
<Button
width="20%"
title="Move"
2019-12-10 22:03:39 +05:00
onPress={async () => {
2020-02-06 13:08:35 +05:00
let noteIds = [];
2020-09-09 11:10:35 +05:00
selectedItemsList.forEach((item) => noteIds.push(item.id));
2020-03-02 15:16:46 +05:00
2020-03-17 15:24:43 +05:00
await db.notes.move(
2020-02-06 13:08:35 +05:00
{
topic: item.title,
2020-02-20 12:16:17 +05:00
id: item.notebookId,
2020-02-06 13:08:35 +05:00
},
2020-02-20 12:16:17 +05:00
...noteIds,
2020-02-06 13:08:35 +05:00
);
2020-02-20 12:16:17 +05:00
dispatch({type: ACTIONS.CLEAR_SELECTION});
2020-01-18 18:14:57 +05:00
2020-02-02 19:09:33 +05:00
moveNoteHideEvent();
2019-12-11 15:20:18 +05:00
2020-03-17 15:24:43 +05:00
ToastEvent.show(`Note moved to ${item.title}`, 'success');
2019-12-05 17:40:09 +05:00
}}
2020-09-09 11:10:35 +05:00
/>
2019-12-05 17:40:09 +05:00
) : null}
2019-12-03 22:05:47 +05:00
</View>
2019-12-09 13:17:40 +05:00
</View>
2019-12-03 22:05:47 +05:00
);
};