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

599 lines
16 KiB
JavaScript
Raw Normal View History

2020-03-21 10:03:11 +05:00
import React from 'react';
2019-12-03 18:12:50 +05:00
import {
2020-01-18 01:04:33 +05:00
KeyboardAvoidingView,
Modal,
Platform,
2020-09-07 13:12:41 +05:00
SafeAreaView,
StyleSheet,
2019-12-03 18:12:50 +05:00
Text,
TouchableOpacity,
2020-01-18 01:04:33 +05:00
View,
2019-12-03 18:12:50 +05:00
} from 'react-native';
import {FlatList, TextInput} from 'react-native-gesture-handler';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-10-13 17:02:14 +05:00
import {Actions} from '../../provider/Actions';
2020-09-07 13:12:41 +05:00
import {Button} from '../Button';
2020-03-14 13:37:07 +05:00
import {updateEvent} from '../DialogManager/recievers';
2020-03-15 09:27:10 +05:00
import {Toast} from '../Toast';
2020-10-13 17:02:14 +05:00
import {ToastEvent} from "../../services/EventManager";
import {ph, pv, SIZE, WEIGHT} from "../../utils/SizeUtils";
import {db} from "../../utils/DB";
import {DDS} from "../../services/DeviceDetection";
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 {
constructor(props) {
super(props);
this.state = {
visible: false,
2020-01-20 15:32:32 +05:00
topics: [],
2020-01-18 18:14:08 +05:00
description: null,
titleFocused: false,
descFocused: false,
2020-01-20 15:32:32 +05:00
count: 0,
topicInputFoused: false,
2020-01-18 18:14:08 +05:00
};
2020-01-20 15:32:32 +05:00
this.title = null;
this.description = null;
2020-01-18 18:14:08 +05:00
this.listRef;
this.prevItem = null;
this.prevIndex = null;
this.currentSelectedInput = null;
2020-02-06 13:08:35 +05:00
this.id = null;
2020-01-18 18:14:08 +05:00
this.backPressCount = 0;
2020-01-20 15:32:32 +05:00
this.currentInputValue = null;
2020-02-02 23:50:55 +05:00
this.titleRef;
this.descriptionRef;
2020-05-12 14:27:22 +05:00
this.topicsToDelete = [];
2020-01-18 18:14:08 +05:00
}
2020-01-20 15:32:32 +05:00
open = () => {
2020-01-18 18:14:08 +05:00
refs = [];
let {toEdit} = this.props;
2020-02-02 23:50:55 +05:00
2020-01-20 15:32:32 +05:00
if (toEdit && toEdit.type === 'notebook') {
2020-01-18 18:14:08 +05:00
let topicsList = [];
2020-09-07 13:12:41 +05:00
toEdit.topics.forEach((item) => {
if (item.id !== 'General') {
2020-01-18 18:14:08 +05:00
topicsList.push(item.title);
}
});
2020-02-06 13:08:35 +05:00
this.id = toEdit.id;
2020-01-20 15:32:32 +05:00
this.title = toEdit.title;
this.description = toEdit.description;
2020-01-18 18:14:08 +05:00
this.setState({
topics: [...topicsList],
2020-01-20 15:32:32 +05:00
visible: true,
});
} else {
this.setState({
2020-01-18 18:14:08 +05:00
visible: true,
});
}
2020-01-20 15:32:32 +05:00
};
close = () => {
2020-01-18 18:14:08 +05:00
refs = [];
this.prevIndex = null;
this.prevItem = null;
this.currentSelectedInput = null;
2020-01-20 15:32:32 +05:00
this.title = null;
this.description = null;
2020-02-06 13:08:35 +05:00
this.id = null;
2020-01-18 18:14:08 +05:00
this.setState({
visible: false,
2020-01-20 15:32:32 +05:00
topics: [],
descFocused: false,
titleFocused: false,
2020-01-18 18:14:08 +05:00
});
2019-12-03 18:12:50 +05:00
};
2019-12-14 21:52:14 +05:00
2020-09-07 13:12:41 +05:00
onDelete = (index) => {
2020-01-20 15:32:32 +05:00
let {topics} = this.state;
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
refs = [];
prevTopics.splice(index, 1);
2020-05-12 14:27:22 +05:00
let edit = this.props.toEdit;
if (edit && edit.id) {
let topicToDelete = edit.topics[index + 1];
if (topicToDelete) {
this.topicsToDelete.push(topicToDelete.id);
}
}
2019-12-14 21:52:14 +05:00
let nextTopics = [...prevTopics];
2020-01-20 15:32:32 +05:00
if (this.prevIndex === index) {
this.prevIndex = null;
this.prevItem = null;
this.currentInputValue = null;
this.topicInputRef.setNativeProps({
text: null,
});
}
2020-01-18 18:14:08 +05:00
this.setState({
topics: nextTopics,
});
2019-12-03 18:12:50 +05:00
};
2020-01-18 18:14:08 +05:00
addNewNotebook = async () => {
2020-09-14 22:59:19 +05:00
let {topics} = this.state;
let edit = this.props.toEdit;
2020-03-01 12:23:35 +05:00
2020-09-14 22:59:19 +05:00
if (!this.title || this.title?.trim().length === 0)
return ToastEvent.show('Title is required', 'error', 'local');
2020-05-13 01:46:02 +05:00
2020-09-14 22:59:19 +05:00
let id = edit && edit.id ? edit.id : null;
2020-05-13 01:46:02 +05:00
2020-09-14 22:59:19 +05:00
let toEdit;
if (id) {
toEdit = db.notebooks.notebook(edit.id).data;
}
2020-05-12 14:27:22 +05:00
2020-09-14 22:59:19 +05:00
let prevTopics = [...topics];
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;
}
}
if (id) {
if (this.topicsToDelete?.length > 0) {
await db.notebooks
.notebook(toEdit.id)
.topics.delete(...this.topicsToDelete);
toEdit = db.notebooks.notebook(toEdit.id).data;
2020-03-03 11:45:06 +05:00
}
2020-09-14 21:15:52 +05:00
2020-09-14 22:59:19 +05:00
await db.notebooks.add({
title: this.title,
description: this.description,
id: id,
});
2020-09-14 21:15:52 +05:00
2020-09-14 22:59:19 +05:00
let nextTopics = toEdit.topics.map((topic, index) => {
if (index === 0) return topic;
let copy = {...topic};
copy.title = prevTopics[index - 1];
return copy;
});
2020-09-14 22:59:19 +05:00
prevTopics.forEach((title, index) => {
if (!nextTopics[index + 1]) {
nextTopics.push(title);
}
});
await db.notebooks.notebook(id).topics.add(...nextTopics);
} else {
await db.notebooks.add({
title: this.title,
description: this.description,
topics: prevTopics,
id: id,
});
}
this.close();
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.NOTEBOOKS});
updateEvent({type: Actions.PINNED});
2020-02-06 13:08:35 +05:00
2020-09-14 22:59:19 +05:00
//ToastEvent.show('New notebook added', 'success', 'local');
2019-12-06 11:09:45 +05:00
};
2020-05-12 14:09:56 +05:00
onSubmit = (forward = true) => {
2020-02-02 16:18:52 +05:00
let {topics} = this.state;
2020-09-14 22:59:19 +05:00
if (!this.currentInputValue || this.currentInputValue?.trim().length === 0)
2020-01-20 15:32:32 +05:00
return;
let prevTopics = [...topics];
if (this.prevItem === null) {
prevTopics.push(this.currentInputValue);
this.setState({
topics: prevTopics,
});
this.topicInputRef.setNativeProps({
text: null,
});
2020-01-06 19:03:21 +05:00
2020-01-20 15:32:32 +05:00
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
2020-03-01 12:23:35 +05:00
this.currentInputValue = null;
2020-01-20 15:32:32 +05:00
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics,
});
this.currentInputValue = null;
2020-05-12 14:09:56 +05:00
if (prevTopics[this.prevIndex + 1] && forward) {
2020-01-20 15:32:32 +05:00
this.prevIndex = this.prevIndex + 1;
this.prevItem = prevTopics[this.prevIndex];
this.currentInputValue = this.prevItem;
this.topicInputRef.setNativeProps({
text: null,
});
this.topicInputRef.setNativeProps({
text: prevTopics[this.prevIndex],
});
} else {
this.prevItem = null;
this.prevIndex = null;
this.currentInputValue = null;
this.topicInputRef.setNativeProps({
text: null,
});
2020-05-12 14:09:56 +05:00
if (forward) {
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
}
2020-01-06 19:03:21 +05:00
}
}
};
2020-01-18 18:14:08 +05:00
render() {
const {colors, toEdit} = this.props;
const {
titleFocused,
descFocused,
topics,
visible,
2020-01-20 15:32:32 +05:00
topicInputFoused,
2020-01-18 18:14:08 +05:00
} = this.state;
return (
<Modal
visible={visible}
transparent={true}
animated
animationType={DDS.isTab ? "fade" :"slide"}
onShow={() => {
this.topicsToDelete = [];
2020-03-02 12:12:23 +05:00
this.titleRef.focus();
}}
2020-01-20 15:32:32 +05:00
onRequestClose={this.close}>
2020-09-07 13:12:41 +05:00
<SafeAreaView>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null}
style={styles.wrapper}>
<TouchableOpacity onPress={this.close} style={styles.overlay} />
2020-01-18 18:14:08 +05:00
<View
style={[
styles.container,
{
backgroundColor: colors.bg,
},
]}>
2020-09-07 13:12:41 +05:00
<View style={styles.headingContainer}>
<Icon
name="book-outline"
color={colors.accent}
size={SIZE.xl}
/>
<Text style={[styles.headingText, {color: colors.accent}]}>
2020-09-07 13:12:41 +05:00
{toEdit && toEdit.dateCreated
? 'Edit Notebook'
: 'New Notebook'}
</Text>
</View>
2020-01-20 15:32:32 +05:00
<TextInput
2020-09-07 13:12:41 +05:00
ref={(ref) => (this.titleRef = ref)}
style={[
styles.input,
{
borderColor: titleFocused ? colors.accent : colors.nav,
color: colors.pri,
},
]}
numberOfLines={1}
multiline={false}
onFocus={() => {
this.setState({
titleFocused: true,
});
}}
onBlur={() => {
this.setState({
titleFocused: false,
});
2020-01-20 15:32:32 +05:00
}}
2020-09-07 13:12:41 +05:00
defaultValue={toEdit ? toEdit.title : null}
onChangeText={(value) => {
this.title = value;
}}
onSubmitEditing={() => {
this.descriptionRef.focus();
}}
placeholder="Title"
2020-09-07 13:12:41 +05:00
placeholderTextColor={colors.icon}
/>
<TextInput
ref={(ref) => (this.descriptionRef = ref)}
style={[
styles.input,
{
borderColor: descFocused ? colors.accent : colors.nav,
minHeight: 45,
2020-09-07 13:12:41 +05:00
color: colors.pri,
},
]}
maxLength={150}
2020-01-20 15:32:32 +05:00
onFocus={() => {
this.setState({
2020-09-07 13:12:41 +05:00
descFocused: true,
2020-01-20 15:32:32 +05:00
});
}}
onBlur={() => {
this.setState({
2020-09-07 13:12:41 +05:00
descFocused: false,
2020-01-20 15:32:32 +05:00
});
}}
2020-09-07 13:12:41 +05:00
defaultValue={toEdit ? toEdit.description : null}
onChangeText={(value) => {
this.description = value;
}}
onSubmitEditing={() => {
this.topicInputRef.focus();
2020-01-20 15:32:32 +05:00
}}
placeholder="Describe your notebook."
2020-01-20 15:32:32 +05:00
placeholderTextColor={colors.icon}
/>
2020-01-18 18:14:08 +05:00
2020-09-07 13:12:41 +05:00
<View style={styles.topicContainer}>
<TextInput
ref={(ref) => (this.topicInputRef = ref)}
onChangeText={(value) => {
this.currentInputValue = value;
if (this.prevItem !== null) {
refs[this.prevIndex].setNativeProps({
text: this.prevIndex + 1 + '. ' + value,
style: {
borderBottomColor: colors.accent,
},
});
}
}}
blurOnSubmit={false}
onFocus={() => {
this.setState({
topicInputFoused: true,
});
}}
onBlur={() => {
this.onSubmit(false);
this.setState({
topicInputFoused: false,
2020-01-20 15:32:32 +05:00
});
}}
2020-09-07 13:12:41 +05:00
onSubmitEditing={this.onSubmit}
style={[
styles.input,
{
borderColor: topicInputFoused
? colors.accent
: colors.nav,
color: colors.pri,
width: '85%',
maxWidth: '85%',
2020-09-14 22:59:19 +05:00
marginTop: 5,
2020-09-07 13:12:41 +05:00
},
]}
placeholder="Add a topic"
2020-09-07 13:12:41 +05:00
placeholderTextColor={colors.icon}
2020-01-18 18:14:08 +05:00
/>
2020-09-07 13:12:41 +05:00
<TouchableOpacity
onPress={this.onSubmit}
style={[
styles.addBtn,
{
borderColor: topicInputFoused
? colors.accent
: colors.nav,
},
]}>
<Icon
name="plus"
size={SIZE.lg}
color={topicInputFoused ? colors.accent : colors.icon}
/>
</TouchableOpacity>
</View>
2020-01-18 18:14:08 +05:00
<View
style={{
zIndex:10
}}
>
2020-09-07 13:12:41 +05:00
<FlatList
data={topics}
ref={(ref) => (this.listRef = ref)}
keyExtractor={(item, index) => item + index.toString()}
renderItem={({item, index}) => (
<TopicItem
item={item}
onPress={(item, index) => {
console.log("here")
2020-09-07 13:12:41 +05:00
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.setNativeProps({
text: item,
});
this.topicInputRef.focus();
this.currentInputValue = item;
}}
onDelete={this.onDelete}
index={index}
colors={colors}
/>
)}
/>
</View>
2020-09-07 13:12:41 +05:00
<View style={styles.buttonContainer}>
<Button
title={toEdit && toEdit.dateCreated ? 'Save' : 'Add'}
width="48%"
onPress={this.addNewNotebook}
/>
<Button
title="Cancel"
grayed
width="48%"
onPress={this.close}
/>
</View>
2020-01-18 18:14:08 +05:00
</View>
2020-09-07 13:12:41 +05:00
</KeyboardAvoidingView>
<Toast context="local" />
</SafeAreaView>
2020-01-18 18:14:08 +05:00
</Modal>
);
}
}
2019-12-03 18:12:50 +05:00
2020-01-20 15:32:32 +05:00
const TopicItem = ({item, index, colors, onPress, onDelete}) => {
2020-09-07 13:12:41 +05:00
const topicRef = (ref) => (refs[index] = ref);
2019-12-03 18:12:50 +05:00
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
2020-01-20 15:32:32 +05:00
<TouchableOpacity
2019-12-03 18:12:50 +05:00
style={{
width: '80%',
backgroundColor:"transparent",
zIndex:10,
position:'absolute',
height:30
2019-12-03 18:12:50 +05:00
}}
2020-01-20 15:32:32 +05:00
onPress={() => {
onPress(item, index)
}}
/>
2020-01-20 15:32:32 +05:00
<TextInput
ref={topicRef}
editable={false}
2020-09-07 13:12:41 +05:00
style={[
styles.topicInput,
{
color: colors.pri,
borderBottomColor: colors.nav,
},
]}
2020-01-20 15:32:32 +05:00
defaultValue={index + 1 + '. ' + item}
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
2019-12-03 22:05:47 +05:00
<TouchableOpacity
2020-01-20 15:32:32 +05:00
onPress={() => {
onDelete(index);
}}
2020-09-07 13:12:41 +05:00
style={[
styles.topicBtn,
{
borderColor: colors.nav,
},
]}>
2020-01-20 15:32:32 +05:00
<Icon name="minus" size={SIZE.lg} color={colors.icon} />
2019-12-03 22:05:47 +05:00
</TouchableOpacity>
2019-12-03 18:12:50 +05:00
</View>
);
};
2020-09-07 13:12:41 +05:00
const styles = StyleSheet.create({
wrapper: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
},
container: {
2020-10-12 11:23:58 +05:00
width: DDS.isTab ? 500 : '100%',
height: DDS.isTab ? 600 : '100%',
maxHeight: DDS.isTab ? 600 : '100%',
borderRadius: DDS.isTab ? 5 : 0,
paddingHorizontal: ph,
paddingVertical: pv,
},
2020-09-07 13:12:41 +05:00
overlay: {
width: '100%',
height: '100%',
position: 'absolute',
},
headingContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
headingText: {
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.xl,
},
input: {
paddingHorizontal: ph,
borderRadius: 5,
minHeight: 45,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
padding: pv - 2,
borderWidth: 1.5,
marginTop: 10,
2020-09-07 13:12:41 +05:00
marginBottom: 5,
},
addBtn: {
borderRadius: 5,
width: '12%',
minHeight: 45,
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1.5,
},
buttonContainer: {
justifyContent: 'space-between',
alignItems: 'center',
flexDirection: 'row',
width: '100%',
marginTop: 20,
},
topicContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 10,
},
topicInput: {
padding: pv - 5,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
paddingHorizontal: ph,
borderBottomWidth: 1.5,
paddingRight: 40,
paddingVertical:10,
2020-09-07 13:12:41 +05:00
width: '100%',
maxWidth: '100%',
},
topicBtn: {
borderRadius: 5,
width: 40,
height: 40,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
right: 0,
},
});