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

589 lines
15 KiB
JavaScript
Raw Normal View History

2022-01-07 23:42:58 +05:00
import React, { createRef } from 'react';
2021-12-31 16:30:43 +05:00
import {
Keyboard,
2022-01-07 23:42:58 +05:00
StyleSheet, TextInput, TouchableOpacity,
View
2021-12-31 16:30:43 +05:00
} from 'react-native';
2022-01-07 23:42:58 +05:00
import { FlatList } from 'react-native-gesture-handler';
import { notesnook } from '../../../e2e/test.ids';
import { useMenuStore } from '../../provider/stores';
import { DDS } from '../../services/DeviceDetection';
import {
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent
} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
2022-01-07 23:42:58 +05:00
import { db } from '../../utils/database';
import {
eCloseAddNotebookDialog,
eOpenAddNotebookDialog
} from '../../utils/Events';
import { ph, pv, SIZE } from '../../utils/SizeUtils';
import { sleep } from '../../utils/TimeUtils';
import { ActionIcon } from '../ActionIcon';
import { Button } from '../Button';
2020-11-14 15:46:26 +05:00
import DialogHeader from '../Dialog/dialog-header';
2020-12-17 15:42:55 +05:00
import Input from '../Input';
import Seperator from '../Seperator';
2022-01-07 23:42:58 +05:00
import SheetWrapper from '../Sheet';
import { Toast } from '../Toast';
2019-12-03 18:12:50 +05:00
2019-12-06 11:09:45 +05:00
let refs = [];
2019-12-03 18:12:50 +05:00
2020-01-18 18:14:08 +05:00
export class AddNotebookDialog extends React.Component {
2020-11-02 09:22:26 +05:00
constructor(props) {
super(props);
this.state = {
2022-01-07 23:42:58 +05:00
notebook: null,
2020-11-02 09:22:26 +05:00
visible: false,
topics: [],
description: null,
titleFocused: false,
descFocused: false,
count: 0,
topicInputFocused: false,
editTopic: false,
loading: false
2020-10-18 15:14:46 +05:00
};
2020-11-02 09:22:26 +05:00
this.title = null;
this.description = null;
this.listRef;
this.prevItem = null;
this.prevIndex = null;
this.currentSelectedInput = null;
this.id = null;
this.backPressCount = 0;
this.currentInputValue = null;
this.titleRef;
this.descriptionRef;
this.topicsToDelete = [];
2020-12-31 18:53:05 +05:00
this.hiddenInput = createRef();
this.topicInputRef = createRef();
this.addingTopic = false;
this.actionSheetRef = createRef();
2020-11-02 09:22:26 +05:00
}
2022-01-07 23:42:58 +05:00
componentDidMount() {
eSubscribeEvent(eOpenAddNotebookDialog, this.open);
eSubscribeEvent(eCloseAddNotebookDialog, this.close);
}
componentWillUnmount() {
eUnSubscribeEvent(eOpenAddNotebookDialog, this.open);
eUnSubscribeEvent(eCloseAddNotebookDialog, this.close);
}
open = notebook => {
2020-11-02 09:22:26 +05:00
refs = [];
2022-01-07 23:42:58 +05:00
if (notebook) {
2020-11-02 09:22:26 +05:00
let topicsList = [];
2022-01-07 23:42:58 +05:00
notebook.topics.forEach((item, index) => {
2020-11-02 09:22:26 +05:00
topicsList.push(item.title);
});
2022-01-07 23:42:58 +05:00
this.id = notebook.id;
this.title = notebook.title;
this.description = notebook.description;
2020-11-02 09:22:26 +05:00
this.setState({
topics: [...topicsList],
2022-01-07 23:42:58 +05:00
visible: true,
notebook: notebook
2020-11-02 09:22:26 +05:00
});
} else {
this.setState({
2022-01-07 23:42:58 +05:00
visible: true,
notebook:null
2020-11-02 09:22:26 +05:00
});
}
sleep(100).then(r => {
this.actionSheetRef.current?.show();
});
2020-11-02 09:22:26 +05:00
};
close = () => {
this.actionSheetRef.current?.hide();
2020-11-02 09:22:26 +05:00
refs = [];
this.prevIndex = null;
this.prevItem = null;
this.currentSelectedInput = null;
this.title = null;
this.description = null;
2020-12-28 16:17:39 +05:00
this.currentInputValue = null;
2020-11-02 09:22:26 +05:00
this.id = null;
};
onDelete = index => {
2020-11-02 09:22:26 +05:00
let {topics} = this.state;
let prevTopics = topics;
refs = [];
prevTopics.splice(index, 1);
2022-01-07 23:42:58 +05:00
let edit = this.state.notebook;
2020-11-02 09:22:26 +05:00
if (edit && edit.id) {
2021-04-28 12:56:48 +05:00
let topicToDelete = edit.topics[index];
2020-11-23 15:57:31 +05:00
2020-11-02 09:22:26 +05:00
if (topicToDelete) {
this.topicsToDelete.push(topicToDelete.id);
}
}
let nextTopics = [...prevTopics];
if (this.prevIndex === index) {
this.prevIndex = null;
this.prevItem = null;
this.currentInputValue = null;
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.setNativeProps({
text: null
2020-11-02 09:22:26 +05:00
});
}
this.setState({
topics: nextTopics
2020-11-02 09:22:26 +05:00
});
};
2020-05-13 01:46:02 +05:00
2020-11-02 09:22:26 +05:00
addNewNotebook = async () => {
this.setState({
loading: true
});
2022-01-07 23:42:58 +05:00
let {topics, notebook} = this.state;
2020-05-12 14:27:22 +05:00
if (!this.title || this.title?.trim().length === 0) {
ToastEvent.show({
heading: 'Notebook title is required',
type: 'error',
context: 'local'
});
this.setState({
loading: false
});
return;
}
2022-01-08 09:37:58 +05:00
let toEdit = null;
if (notebook) {
2022-01-07 23:42:58 +05:00
toEdit = db.notebooks.notebook(notebook.id).data;
2020-11-02 09:22:26 +05:00
}
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
let prevTopics = [...topics];
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
if (this.currentInputValue && this.currentInputValue.trim().length !== 0) {
if (this.prevItem != null) {
prevTopics[this.prevIndex] = this.currentInputValue;
} else {
prevTopics.push(this.currentInputValue);
this.currentInputValue = null;
}
}
2022-01-08 09:37:58 +05:00
if (notebook) {
2020-11-02 09:22:26 +05:00
if (this.topicsToDelete?.length > 0) {
await db.notebooks
.notebook(toEdit.id)
.topics.delete(...this.topicsToDelete);
toEdit = db.notebooks.notebook(toEdit.id).data;
}
await db.notebooks.add({
title: this.title,
description: this.description,
2022-01-07 23:42:58 +05:00
id: notebook.id
2020-11-02 09:22:26 +05:00
});
let nextTopics = toEdit.topics.map((topic, index) => {
let copy = {...topic};
2021-04-28 12:56:48 +05:00
copy.title = prevTopics[index];
2020-11-02 09:22:26 +05:00
return copy;
});
prevTopics.forEach((title, index) => {
2021-04-28 12:56:48 +05:00
if (!nextTopics[index]) {
2020-11-02 09:22:26 +05:00
nextTopics.push(title);
2020-05-12 14:09:56 +05:00
}
2020-11-02 09:22:26 +05:00
});
2022-01-07 23:42:58 +05:00
await db.notebooks.notebook(toEdit.id).topics.add(...nextTopics);
2020-11-02 09:22:26 +05:00
} else {
await db.notebooks.add({
title: this.title,
description: this.description,
2022-01-08 09:37:58 +05:00
topics: prevTopics,
id:null
2020-11-02 09:22:26 +05:00
});
}
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notebook,
Navigation.routeNames.NotesPage
]);
this.setState({
loading: false
});
2020-11-02 09:22:26 +05:00
this.close();
};
onSubmit = (forward = true) => {
2020-12-31 18:53:05 +05:00
this.hiddenInput.current?.focus();
2021-04-28 12:56:48 +05:00
let willFocus = true;
2020-11-02 09:22:26 +05:00
let {topics} = this.state;
if (!this.currentInputValue || this.currentInputValue?.trim().length === 0)
return;
let prevTopics = [...topics];
if (this.prevItem === null) {
prevTopics.push(this.currentInputValue);
this.setState({
topics: prevTopics
2020-11-02 09:22:26 +05:00
});
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
this.currentInputValue = null;
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics
2020-11-02 09:22:26 +05:00
});
this.currentInputValue = null;
2021-04-28 12:56:48 +05:00
2020-11-02 09:22:26 +05:00
if (this.state.editTopic) {
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.blur();
2020-11-02 09:22:26 +05:00
Keyboard.dismiss();
this.setState({
editTopic: false
2020-11-02 09:22:26 +05:00
});
2021-04-28 12:56:48 +05:00
willFocus = false;
2020-11-02 09:22:26 +05:00
}
this.prevItem = null;
this.prevIndex = null;
this.currentInputValue = null;
if (forward) {
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
}
2020-01-06 19:03:21 +05:00
}
2021-01-13 13:23:01 +05:00
this.topicInputRef.current?.setNativeProps({
text: ''
});
2021-04-28 12:56:48 +05:00
willFocus && this.topicInputRef.current?.focus();
2020-11-02 09:22:26 +05:00
};
render() {
2022-01-07 23:42:58 +05:00
const {colors} = this.props;
const {topics, visible, topicInputFocused, notebook} = this.state;
2020-11-23 12:32:33 +05:00
if (!visible) return null;
2020-01-18 18:14:08 +05:00
return (
2021-12-25 11:16:33 +05:00
<SheetWrapper
onOpen={async () => {
2020-11-02 09:22:26 +05:00
this.topicsToDelete = [];
await sleep(300);
2022-01-07 23:42:58 +05:00
!this.state.notebook && this.titleRef?.focus();
2020-11-02 09:22:26 +05:00
}}
fwdRef={this.actionSheetRef}
onClose={() => {
this.close();
this.setState({
visible: false,
topics: [],
descFocused: false,
titleFocused: false,
2022-01-07 23:42:58 +05:00
editTopic: false,
2022-01-08 09:37:58 +05:00
notebook: null
});
}}
statusBarTranslucent={false}
2020-11-02 09:22:26 +05:00
onRequestClose={this.close}>
<View
2021-07-24 13:15:53 +05:00
style={{
maxHeight: DDS.isTab ? '90%' : '100%',
borderRadius: DDS.isTab ? 5 : 0,
paddingHorizontal: 12
}}>
<TextInput
ref={this.hiddenInput}
style={{
width: 1,
height: 1,
opacity: 0,
position: 'absolute'
}}
blurOnSubmit={false}
/>
<DialogHeader
title={
2022-01-07 23:42:58 +05:00
notebook && notebook.dateCreated
? 'Edit Notebook'
: 'New Notebook'
}
paragraph={
2022-01-07 23:42:58 +05:00
notebook && notebook.dateCreated
2021-04-28 12:56:48 +05:00
? 'You are editing ' + this.title + ' notebook.'
: 'Notebooks are the best way to organize your notes.'
}
/>
<Seperator half />
<Input
fwdRef={ref => (this.titleRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.title}
onChangeText={value => {
this.title = value;
2020-12-31 18:53:05 +05:00
}}
placeholder="Enter a title"
onSubmit={() => {
this.descriptionRef.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
2022-01-07 23:42:58 +05:00
defaultValue={notebook ? notebook.title : null}
2020-12-31 18:53:05 +05:00
/>
2020-11-02 09:22:26 +05:00
<Input
fwdRef={ref => (this.descriptionRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.description}
onChangeText={value => {
this.description = value;
}}
placeholder="Describe your notebook."
onSubmit={() => {
this.topicInputRef.current?.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
2022-01-07 23:42:58 +05:00
defaultValue={notebook ? notebook.description : null}
/>
2020-12-17 15:42:55 +05:00
<Input
fwdRef={this.topicInputRef}
testID={notesnook.ids.dialogs.notebook.inputs.topic}
onChangeText={value => {
this.currentInputValue = value;
if (this.prevItem !== null) {
refs[this.prevIndex].setNativeProps({
text: value,
style: {
borderBottomColor: colors.accent
}
});
}
}}
returnKeyLabel="Done"
returnKeyType="done"
onSubmit={() => {
this.onSubmit();
}}
blurOnSubmit={false}
button={{
icon: this.state.editTopic ? 'check' : 'plus',
onPress: this.onSubmit,
color: topicInputFocused ? colors.accent : colors.icon
}}
placeholder="Add a topic"
/>
2020-11-02 09:22:26 +05:00
<FlatList
data={topics}
ref={ref => (this.listRef = ref)}
nestedScrollEnabled
keyExtractor={(item, index) => item + index.toString()}
onMomentumScrollEnd={() => {
this.actionSheetRef.current?.handleChildScrollEnd();
}}
keyboardShouldPersistTaps="always"
2021-12-31 16:30:43 +05:00
keyboardDismissMode="interactive"
ListFooterComponent={<View style={{height: 50}} />}
renderItem={({item, index}) => (
<TopicItem
item={item}
onPress={(item, index) => {
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.current?.setNativeProps({
text: item
});
this.topicInputRef.current?.focus();
this.currentInputValue = item;
this.setState({
editTopic: true
});
2020-12-17 15:42:55 +05:00
}}
onDelete={this.onDelete}
index={index}
colors={colors}
2020-12-17 15:42:55 +05:00
/>
)}
/>
<Seperator />
<Button
width="100%"
height={50}
fontSize={SIZE.md}
title={
2022-01-07 23:42:58 +05:00
notebook && notebook.dateCreated
? 'Save changes'
: 'Create notebook'
}
type="accent"
onPress={this.addNewNotebook}
/>
2022-01-07 23:42:58 +05:00
{/*
2021-12-31 16:30:43 +05:00
{Platform.OS === 'ios' && (
<View
style={{
height: 40
}}
/>
)} */}
</View>
2020-11-02 09:22:26 +05:00
<Toast context="local" />
2021-12-25 11:16:33 +05:00
</SheetWrapper>
2020-01-18 18:14:08 +05:00
);
2020-11-02 09:22:26 +05:00
}
}
2019-12-03 18:12:50 +05:00
2020-11-02 09:22:26 +05:00
const TopicItem = ({item, index, colors, onPress, onDelete}) => {
const topicRef = ref => (refs[index] = ref);
2020-09-07 13:12:41 +05:00
2020-11-02 09:22:26 +05:00
return (
<View
style={{
2020-10-18 15:14:46 +05:00
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
2021-07-10 11:49:59 +05:00
backgroundColor: colors.nav,
borderRadius: 5,
marginVertical: 5
2020-11-02 09:22:26 +05:00
}}>
<TouchableOpacity
style={{
width: '80%',
backgroundColor: 'transparent',
zIndex: 10,
position: 'absolute',
height: 30
2020-11-02 09:22:26 +05:00
}}
onPress={() => {
onPress(item, index);
}}
/>
<TextInput
ref={topicRef}
editable={false}
style={[
styles.topicInput,
{
color: colors.pri
}
2020-11-02 09:22:26 +05:00
]}
defaultValue={item}
2021-12-30 10:42:03 +05:00
placeholderTextColor={colors.placeholder}
2020-11-02 09:22:26 +05:00
/>
<View
2020-11-02 20:45:56 +05:00
style={{
width: 80,
position: 'absolute',
right: 0,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'flex-end'
2020-11-02 20:45:56 +05:00
}}>
<ActionIcon
onPress={() => {
2020-11-02 09:22:26 +05:00
onPress(item, index);
}}
name="pencil"
size={SIZE.lg - 5}
color={colors.icon}
2020-11-02 20:45:56 +05:00
/>
2020-11-02 09:22:26 +05:00
<ActionIcon
onPress={() => {
onDelete(index);
}}
name="minus"
size={SIZE.lg}
color={colors.icon}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
wrapper: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center'
2020-11-02 09:22:26 +05:00
},
overlay: {
width: '100%',
height: '100%',
position: 'absolute'
2020-11-02 09:22:26 +05:00
},
headingContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
2020-11-02 09:22:26 +05:00
},
headingText: {
marginLeft: 5,
fontSize: SIZE.xl
2020-11-02 09:22:26 +05:00
},
input: {
2020-11-14 15:46:26 +05:00
paddingRight: 12,
paddingHorizontal: 0,
2020-11-02 20:45:56 +05:00
borderRadius: 0,
2020-11-02 09:22:26 +05:00
minHeight: 45,
2020-11-14 15:46:26 +05:00
fontSize: SIZE.md,
2020-11-02 09:22:26 +05:00
padding: pv - 2,
2020-11-02 20:45:56 +05:00
borderBottomWidth: 1,
2020-11-02 09:22:26 +05:00
marginTop: 10,
marginBottom: 5
2020-11-02 09:22:26 +05:00
},
addBtn: {
width: '12%',
minHeight: 45,
justifyContent: 'center',
alignItems: 'center',
2020-11-02 20:45:56 +05:00
position: 'absolute',
right: 0
2020-11-02 09:22:26 +05:00
},
buttonContainer: {
justifyContent: 'space-between',
alignItems: 'center',
flexDirection: 'row',
width: '100%',
marginTop: 20
2020-11-02 09:22:26 +05:00
},
topicContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 10
2020-11-02 09:22:26 +05:00
},
topicInput: {
padding: pv - 5,
fontSize: SIZE.sm,
2021-01-03 10:38:07 +05:00
//fontFamily: "sans-serif",
2020-11-02 09:22:26 +05:00
paddingHorizontal: ph,
paddingRight: 40,
paddingVertical: 10,
width: '100%',
maxWidth: '100%'
2020-11-02 09:22:26 +05:00
},
topicBtn: {
borderRadius: 5,
width: 40,
height: 40,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
right: 0
}
2020-09-07 13:12:41 +05:00
});