import React, {createRef, useEffect, useState} from 'react';
import {Keyboard, TextInput, TouchableOpacity, View} from 'react-native';
import {FlatList} from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {notesnook} from '../../../e2e/test.ids';
import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
import { useNotebookStore, useSelectionStore } from '../../provider/stores';
import {
eSubscribeEvent,
eUnSubscribeEvent,
sendNoteEditedEvent,
ToastEvent,
} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import { getTotalNotes } from '../../utils';
import {db} from '../../utils/DB';
import {eOpenMoveNoteDialog} from '../../utils/Events';
import {pv, SIZE} from '../../utils/SizeUtils';
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
import {Button} from '../Button';
import DialogHeader from '../Dialog/dialog-header';
import {PressableButton} from '../PressableButton';
import Heading from '../Typography/Heading';
import Paragraph from '../Typography/Paragraph';
let newNotebookTitle = null;
let newTopicTitle = null;
const notebookInput = createRef();
const topicInput = createRef();
const actionSheetRef = createRef();
const MoveNoteDialog = () => {
const [visible, setVisible] = useState(false);
const [note, setNote] = useState(null);
function open(note) {
setNote(note);
setVisible(true);
actionSheetRef.current?.setModalVisible(true);
}
const close = () => {
actionSheetRef.current?.setModalVisible(false);
};
useEffect(() => {
eSubscribeEvent(eOpenMoveNoteDialog, open);
return () => {
eUnSubscribeEvent(eOpenMoveNoteDialog, open);
};
}, []);
const _onClose = () => {
setVisible(false);
newTopicTitle = null;
newNotebookTitle = null;
setNote(null);
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.Favorites,
Navigation.routeNames.NotesPage,
Navigation.routeNames.Notebook,
Navigation.routeNames.Notebooks,
]);
};
const update = (note) => {
setNote(note);
};
return !visible ? null : (
);
};
export default MoveNoteDialog;
const MoveNoteComponent = ({close, note, setNote}) => {
const [state, dispatch] = useTracked();
const {colors} = state;
const notebooks = useNotebookStore(state => state.notebooks);
const selectedItemsList = useSelectionStore(state => state.selectedItemsList);
const setNotebooks = useNotebookStore(state => state.setNotebooks);
const [expanded, setExpanded] = useState('');
const [notebookInputFocused, setNotebookInputFocused] = useState(false);
const [topicInputFocused, setTopicInputFocused] = useState(false);
const [noteExists, setNoteExists] = useState([]);
const addNewNotebook = async () => {
if (!newNotebookTitle || newNotebookTitle.trim().length === 0)
return ToastEvent.show({
heading: 'Notebook title is required',
type: 'error',
context: 'local',
});
await db.notebooks.add({
title: newNotebookTitle,
description: null,
topics: [],
id: null,
});
notebookInput.current?.clear();
notebookInput.current?.blur();
setNotebooks();
};
const addNewTopic = async () => {
if (!newTopicTitle || newTopicTitle.trim().length === 0) {
return ToastEvent.show({
heading: 'Topic title is required',
type: 'error',
context: 'local',
});
}
await db.notebooks.notebook(expanded).topics.add(newTopicTitle);
setNotebooks();
topicInput.current?.clear();
topicInput.current?.blur();
newTopicTitle = null;
};
const handlePress = async (item, index) => {
if (note && item.notes.indexOf(note.id) > -1) {
await db.notebooks
.notebook(item.notebookId)
.topics.topic(item.id)
.delete(note.id);
if (note && note.id) {
setNote({...db.notes.note(note.id).data});
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Notes,
]);
}
setNotebooks();
return;
}
let noteIds = [];
selectedItemsList.forEach((i) => noteIds.push(i.id));
await db.notes.move(
{
topic: item.id,
id: item.notebookId,
},
...noteIds,
);
if (note && note.id) {
setNote({...db.notes.note(note.id).data});
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Notes,
]);
}
setNotebooks();
};
useEffect(() => {
if (!note?.id) return;
let ids = [];
for (let i = 0; i < notebooks.length; i++) {
for (let t = 0; t < notebooks[i].topics.length; t++) {
if (notebooks[i].topics[t].notes.indexOf(note.id) > -1) {
ids.push(notebooks[i].id);
}
}
}
setNoteExists(ids);
}, []);
return (
<>
{
Keyboard.dismiss();
}}
/>
{
actionSheetRef.current?.handleChildScrollEnd();
}}
onMomentumScrollEnd={() => {
actionSheetRef.current?.handleChildScrollEnd();
}}
onScrollAnimationEnd={() => {
actionSheetRef.current?.handleChildScrollEnd();
}}
keyboardShouldPersistTaps="always"
keyboardDismissMode="none"
data={notebooks}
ListFooterComponent={
}
ListHeaderComponent={
{
newNotebookTitle = value;
}}
testID={notesnook.ids.dialogs.addTo.addNotebook}
blurOnSubmit={false}
onFocus={() => {
setNotebookInputFocused(true);
}}
onBlur={() => {
setNotebookInputFocused(false);
}}
onSubmitEditing={addNewNotebook}
style={[
{
color: colors.pri,
width: '90%',
maxWidth: '90%',
paddingHorizontal: 0,
borderRadius: 5,
minHeight: 45,
fontSize: SIZE.md,
//fontFamily: "sans-serif",
padding: pv - 2,
},
]}
placeholder="Create a new notebook"
placeholderTextColor={colors.icon}
/>
}
renderItem={({item, index}) => (
{
setExpanded(item.id === expanded ? null : item.id);
setTopicInputFocused(false);
setNotebookInputFocused(false);
}}
type="gray"
customStyle={{
height: 50,
width: '100%',
borderRadius: 0,
alignItems: 'flex-start',
marginBottom: 5,
borderBottomWidth: 1,
borderBottomColor:
expanded === item.id || noteExists.indexOf(item.id) > -1
? colors.accent
: colors.nav,
}}>
-1
? colors.accent
: null
}
size={SIZE.md}>
{item.title}
{getTotalNotes(item) +
' notes' +
' & ' +
item.topics.length +
' topics'}
{expanded === item.id ? (
{
actionSheetRef.current?.handleChildScrollEnd();
}}
onMomentumScrollEnd={() => {
actionSheetRef.current?.handleChildScrollEnd();
}}
onScrollAnimationEnd={() => {
actionSheetRef.current?.handleChildScrollEnd();
}}
style={{
width: '90%',
alignSelf: 'flex-end',
maxHeight: 500,
}}
ListHeaderComponent={
{
newTopicTitle = value;
}}
testID={notesnook.ids.dialogs.addTo.addTopic}
blurOnSubmit={false}
onFocus={() => {
setTopicInputFocused(true);
}}
onBlur={() => {
setTopicInputFocused(false);
}}
onSubmitEditing={addNewTopic}
style={[
{
color: colors.pri,
width: '90%',
maxWidth: '90%',
paddingHorizontal: 0,
borderRadius: 5,
height: 40,
fontSize: SIZE.sm,
padding: pv - 2,
},
]}
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
}
renderItem={({item, index}) => (
handlePress(item, index)}
type="gray"
customStyle={{
height: 50,
borderTopWidth: index === 0 ? 0 : 1,
borderTopColor: colors.nav,
width: '100%',
borderRadius: 0,
alignItems: 'center',
flexDirection: 'row',
paddingHorizontal: 12,
justifyContent: 'space-between',
}}>
{item.title}
{item.notes.length + ' notes'}
{note && item.notes.indexOf(note.id) > -1 ? (
)}
/>
) : null}
)}
/>
>
);
};