import React, { createRef, useEffect, useState } from 'react'; import { FlatList, Modal, Text, TextInput, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import { pv, SIZE, WEIGHT } from '../../common/common'; import { useTracked } from '../../provider'; import { ACTIONS } from '../../provider/actions'; import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/eventManager'; import { eOpenMoveNoteDialog } from '../../services/events'; import { db, DDS, getElevation, ToastEvent } from '../../utils/utils'; import { PressableButton } from '../PressableButton'; import { Toast } from '../Toast'; const MoveNoteDialog = () => { const [state, dispatch] = useTracked(); const {notebooks, colors, selectedItemsList} = state; const [visible, setVisible] = useState(true); const [animated, setAnimated] = useState(false); const [expanded, setExpanded] = useState(''); const [notebookInputFocused, setNotebookInputFocused] = useState(false); const [topicInputFocused, setTopicInputFocused] = useState(false); const insets = useSafeAreaInsets(); const notebookInput = createRef(); const topicInput = createRef(); let newNotebookTitle = null; let newTopicTitle = null; function open() { setVisible(true); } const close = () => { setVisible(false); setAnimated(false); }; useEffect(() => { console.log(notebooks); eSubscribeEvent(eOpenMoveNoteDialog, open); return () => { eUnSubscribeEvent(eOpenMoveNoteDialog, open); }; }, []); const addNewNotebook = async () => { if (!newNotebookTitle || newNotebookTitle.trim().length === 0) return ToastEvent.show('Title is required', 'error', 'local'); await db.notebooks.add({ title: newNotebookTitle, description: 'this.description', topics: [], id: null, }); dispatch({type: ACTIONS.NOTEBOOKS}); dispatch({type: ACTIONS.PINNED}); }; const addNewTopic = async () => { if (!newTopicTitle || newTopicTitle.trim().length === 0) return ToastEvent.show('Title is required', 'error', 'local'); await db.notebooks.notebook(expanded).topics.add(newTopicTitle); }; return ( { setVisible(false); }} style={{ width: 50, height: 50, marginLeft: 12, position: 'absolute', textAlignVertical: 'center', left: 0, marginBottom: 15, }} color={colors.heading} /> {selectedItemsList.length > 1 ? 'Add notes to notebook' : 'Add note to notebook'} { newNotebookTitle = value; }} blurOnSubmit={false} onFocus={() => { setNotebookInputFocused(true); }} onBlur={() => { setNotebookInputFocused(false); }} onSubmitEditing={addNewNotebook} style={[ { color: colors.pri, width: '85%', maxWidth: '85%', paddingHorizontal: 0, borderRadius: 5, minHeight: 45, fontSize: SIZE.md, fontFamily: WEIGHT.regular, padding: pv - 2, }, ]} placeholder="Create a new notebook" placeholderTextColor={colors.icon} /> } renderItem={({item, index}) => ( { setExpanded(item.id === expanded ? null : item.id); }} color={expanded === item.id ? colors.shade : 'transparent'} selectedColor={ expanded === item.id ? colors.accent : colors.nav } alpha={colors.night ? 0.02 : -0.02} opacity={expanded === item.id ? 0.12 : 1} customStyle={{ height: 50, width: '100%', borderRadius: 0, alignItems: 'flex-start', paddingHorizontal: 12, marginBottom: 5, }}> {item.title} {'\n'} {item.totalNotes + ' notes' + ' & ' + item.topics.length + ' topics'} {expanded === item.id ? ( { newTopicTitle = value; }} blurOnSubmit={false} onFocus={() => { setTopicInputFocused(true); }} onBlur={() => { setTopicInputFocused(false); }} onSubmitEditing={() => {}} style={[ { color: colors.pri, width: '85%', maxWidth: '85%', paddingHorizontal: 0, borderRadius: 5, height: 40, fontSize: SIZE.sm, fontFamily: WEIGHT.regular, padding: pv - 2, }, ]} placeholder="Create a new topic" placeholderTextColor={colors.icon} /> {}} style={[ { borderRadius: 5, width: 40, minHeight: 40, justifyContent: 'center', alignItems: 'center', }, ]}> } renderItem={({item, index}) => ( { let noteIds = []; selectedItemsList.forEach((item) => noteIds.push(item.id), ); await db.notes.move( { topic: item.title, id: item.notebookId, }, ...noteIds, ); dispatch({type: ACTIONS.CLEAR_SELECTION}); close(); let notebookName = db.notebooks.notebook( item.notebookId, ).title; ToastEvent.show( `Note moved to ${item.title} in ${notebookName}`, 'success', ); }} color="transparent" selectedColor={colors.nav} alpha={colors.night ? 0.02 : -0.02} customStyle={{ height: 50, borderTopWidth: index === 0 ? 0 : 1, borderTopColor: colors.nav, width: '87%', alignSelf: 'flex-end', borderRadius: 0, alignItems: 'center', marginRight: 12, flexDirection: 'row', justifyContent: 'space-between', }}> {item.title} {'\n'} {item.totalNotes + ' notes'} Move )} /> ) : null} )} /> ); }; export default MoveNoteDialog;