import React, {useState} from 'react'; import {View, Text, TouchableOpacity} from 'react-native'; import NavigationService from '../../services/NavigationService'; import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu'; import {SIZE, ph, pv, opacity, WEIGHT, br} from '../../common/common'; import Icon from 'react-native-vector-icons/Feather'; import {w, ToastEvent} from '../../utils/utils'; import {db, DDS} from '../../../App'; import {Dialog} from '../Dialog'; import {AddTopicDialog} from '../AddTopicDialog'; import {useAppContext} from '../../provider/useAppContext'; import {AddNotebookDialog} from '../AddNotebookDialog'; import ActionSheet from '../ActionSheet'; import {ActionSheetComponent} from '../ActionSheetComponent'; export const NotebookItem = ({ item, index, hideMore = false, topic, isTopic = false, isMove = false, noteToMove = null, notebookID, numColumns, isTrash, refresh = () => {}, }) => { const {colors} = useAppContext(); const [isVisible, setVisible] = useState(false); const [addTopic, setAddTopic] = useState(false); const [addNotebook, setAddNotebook] = useState(false); let setMenuRef = {}; let show = null; let willRefresh; const deleteItem = async () => { if (isTopic) { await db.deleteTopicFromNotebook(notebookID, item.title); ToastEvent.show('Topic moved to trash', 'success', 3000); } else { await db.deleteNotebooks([item]); ToastEvent.show('Notebook moved to trash', 'success', 3000); } setVisible(false); refresh(); }; const navigate = () => { isTopic ? NavigationService.navigate('Notes', { ...item, notebookID, }) : NavigationService.navigate('Notebook', { notebook: item, note: noteToMove, title: hideMore ? 'Choose a topic' : item.title, isMove: isMove ? true : false, hideMore: hideMore ? true : false, }); }; const onMenuHide = () => { if (show) { if (show === 'delete') { setVisible(true); show = null; } else if (show === 'topic') { isTopic ? setAddTopic(true) : setAddNotebook(true); } } }; const hideMenu = () => { setMenuRef[index].hide(); }; const showMenu = () => { setMenuRef[index].show(); }; return ( setVisible(false)} /> setAddTopic(false)} /> { setAddNotebook(false); }} /> {item.title} {isTopic || !item.description ? null : ( {item.description} )} {isTopic ? null : ( {item.topics.slice(1, 4).map(topic => ( {topic.title} ))} )} {isTrash ? ( {'Deleted on: ' + new Date(item.dateDeleted).toISOString().slice(0, 10) + ' '} {item.type[0].toUpperCase() + item.type.slice(1) + ' '} ) : null} {isTopic || isTrash ? null : ( {new Date(item.dateCreated).toDateString().substring(4)} )} {isTopic ? ( {item.totalNotes.length == 1 ? item.totalNotes + ' notes' : item.totalNotes + ' note'} ) : null} {hideMore ? null : ( { ActionSheet._setModalVisible(); }}> )} {hideMore && isTopic ? ( { 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, }); } NavigationService.navigate('Home'); ToastEvent.show( `Note moved to ${item.title}`, 'success', 3000, () => {}, '', ); }} style={{ borderWidth: 1, borderRadius: 5, width: '20%', paddingHorizontal: ph - 5, borderColor: colors.nav, paddingVertical: pv - 5, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: colors.accent, }}> Move ) : null} { onMenuHide(); if (willRefresh) { refresh(); } }} children={ { willRefresh = true; }} rowItems={ isTrash ? ['Restore', 'Remove'] : [ item.type == 'topic' ? 'Edit Topic' : 'Edit Notebook', 'Delete', ] } columnItems={['Pin', 'Favorite']} close={value => { if (value) { show = value; } ActionSheet._setModalVisible(); }} /> } /> ); };