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

518 lines
15 KiB
JavaScript
Raw Normal View History

2020-01-18 18:14:08 +05:00
import React, {useState} from 'react';
2019-12-03 18:12:50 +05:00
import {
2020-01-18 01:04:33 +05:00
KeyboardAvoidingView,
Modal,
Platform,
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-01-18 01:04:33 +05:00
import Icon from 'react-native-vector-icons/Feather';
2019-12-14 16:00:16 +05:00
import {db, DDS} from '../../../App';
2020-01-18 01:04:33 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-01-18 18:14:08 +05:00
import {ACTIONS} from '../../provider';
2020-01-20 15:32:32 +05:00
import {getElevation, ToastEvent, timeSince} from '../../utils/utils';
2020-01-18 18:14:08 +05:00
import {updateEvent} from '../DialogManager';
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;
this.timestamp = null;
this.backPressCount = 0;
2020-01-20 15:32:32 +05:00
this.currentInputValue = null;
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-01-20 15:32:32 +05:00
console.log(toEdit);
if (toEdit && toEdit.type === 'notebook') {
2020-01-18 18:14:08 +05:00
let topicsList = [];
toEdit.topics.forEach(item => {
if (item.title !== 'General') {
topicsList.push(item.title);
}
});
2020-01-20 15:32:32 +05:00
this.timestamp = toEdit.dateCreated;
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;
this.timestamp = 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-01-18 18:14:08 +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);
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-01-20 15:32:32 +05:00
let {topics} = this.state;
2020-01-18 18:14:08 +05:00
let {toEdit} = this.props;
2020-01-20 15:32:32 +05:00
if (!this.title)
2019-12-06 11:09:45 +05:00
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
let dateCreated = toEdit && toEdit.dateCreated ? toEdit.dateCreated : null;
2019-12-11 01:10:04 +05:00
await db.addNotebook({
2020-01-20 15:32:32 +05:00
title: this.title,
description: this.description,
2019-12-06 11:09:45 +05:00
topics,
dateCreated: dateCreated,
2019-12-06 11:09:45 +05:00
});
2020-01-18 18:14:08 +05:00
updateEvent({type: ACTIONS.NOTEBOOKS});
this.close();
ToastEvent.show('New notebook added', 'success', 3000, () => {}, '');
2019-12-06 11:09:45 +05:00
};
2020-01-20 15:32:32 +05:00
onSubmit = () => {
if (
!this.currentInputValue ||
this.currentInputValue === '' ||
this.currentInputValue === ' '
)
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);
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics,
});
this.currentInputValue = null;
if (prevTopics[this.prevIndex + 1]) {
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,
});
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="fade"
2020-01-20 15:32:32 +05:00
onRequestClose={this.close}>
2020-01-18 18:14:08 +05:00
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null}
2020-01-08 22:47:49 +05:00
style={{
width: '100%',
height: '100%',
2020-01-18 18:14:08 +05:00
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
2019-12-03 18:12:50 +05:00
}}>
2020-01-18 18:14:08 +05:00
<TouchableOpacity
2020-01-20 15:32:32 +05:00
onPress={this.close}
2019-12-03 18:12:50 +05:00
style={{
2020-01-18 18:14:08 +05:00
width: '100%',
height: '100%',
position: 'absolute',
2020-01-08 22:47:49 +05:00
}}
2019-12-03 22:05:47 +05:00
/>
<View
style={{
2020-01-18 18:14:08 +05:00
...getElevation(5),
width: DDS.isTab ? '50%' : '80%',
2020-01-20 15:32:32 +05:00
maxHeight: '80%',
height: '80%',
2020-01-18 18:14:08 +05:00
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-03 18:12:50 +05:00
}}>
2020-01-18 18:14:08 +05:00
<View
2019-12-03 18:12:50 +05:00
style={{
2020-01-18 18:14:08 +05:00
flexDirection: 'row',
2019-12-03 18:12:50 +05:00
justifyContent: 'center',
alignItems: 'center',
}}>
2020-01-18 18:14:08 +05:00
<Icon name="book-open" color={colors.accent} size={SIZE.lg} />
2019-12-03 18:12:50 +05:00
<Text
style={{
2020-01-18 18:14:08 +05:00
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
2019-12-03 18:12:50 +05:00
}}>
2020-01-20 15:32:32 +05:00
{toEdit && toEdit.dateCreated
? 'Edit Notebook'
: 'New Notebook'}
2019-12-03 18:12:50 +05:00
</Text>
2020-01-18 18:14:08 +05:00
</View>
<TextInput
style={{
padding: pv - 5,
borderWidth: 1.5,
borderColor: titleFocused ? colors.accent : colors.nav,
paddingHorizontal: ph,
borderRadius: 5,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
marginTop: 20,
marginBottom: 5,
}}
onFocus={() => {
this.setState({
titleFocused: true,
});
2019-12-03 22:05:47 +05:00
}}
2020-01-18 18:14:08 +05:00
onBlur={() => {
this.setState({
titleFocused: false,
});
}}
defaultValue={toEdit ? toEdit.title : null}
onChangeText={value => {
2020-01-20 15:32:32 +05:00
this.title = value;
2020-01-18 18:14:08 +05:00
}}
placeholder="Title of notebook"
placeholderTextColor={colors.icon}
/>
<TextInput
2019-12-03 18:12:50 +05:00
style={{
2020-01-18 18:14:08 +05:00
padding: pv - 5,
borderWidth: 1.5,
borderColor: descFocused ? colors.accent : colors.nav,
2019-12-03 18:12:50 +05:00
paddingHorizontal: ph,
borderRadius: 5,
2020-01-18 18:14:08 +05:00
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
marginTop: 5,
marginBottom: 10,
}}
onFocus={() => {
this.setState({
descFocused: true,
});
}}
onBlur={() => {
this.setState({
descFocused: false,
});
}}
defaultValue={toEdit ? toEdit.description : null}
onChangeText={value => {
2020-01-20 15:32:32 +05:00
this.description = value;
2020-01-18 18:14:08 +05:00
}}
placeholder="write a description"
placeholderTextColor={colors.icon}
/>
2020-01-20 15:32:32 +05:00
<View
2020-01-18 18:14:08 +05:00
style={{
2020-01-20 15:32:32 +05:00
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 10,
2020-01-18 18:14:08 +05:00
}}>
2020-01-20 15:32:32 +05:00
<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.setState({
topicInputFoused: false,
});
}}
onSubmitEditing={this.onSubmit}
style={{
padding: pv - 5,
paddingHorizontal: 0,
borderRadius: 5,
borderWidth: 1.5,
fontSize: SIZE.sm,
borderColor: topicInputFoused ? colors.accent : colors.nav,
fontFamily: WEIGHT.regular,
color: colors.pri,
paddingHorizontal: ph,
width: '85%',
maxWidth: '85%',
}}
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
<TouchableOpacity
onPress={this.onSubmit}
style={{
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1.5,
borderColor: topicInputFoused ? colors.accent : colors.nav,
borderRadius: 5,
width: 40,
height: 40,
}}>
<Icon
name="plus"
size={SIZE.lg}
color={topicInputFoused ? colors.accent : colors.icon}
/>
</TouchableOpacity>
</View>
2020-01-18 18:14:08 +05:00
<FlatList
data={topics}
ref={ref => (this.listRef = ref)}
removeClippedSubviews={false}
enableEmptySections={false}
getItemLayout={(data, index) => ({
length: 50,
offset: 50 * index,
index,
})}
2020-01-20 15:32:32 +05:00
keyExtractor={(item, index) => item + index.toString()}
2020-01-18 18:14:08 +05:00
renderItem={({item, index}) => (
<TopicItem
item={item}
2020-01-20 15:32:32 +05:00
onPress={(item, index) => {
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.setNativeProps({
text: item,
});
this.topicInputRef.focus();
this.currentInputValue = item;
}}
onDelete={this.onDelete}
2020-01-18 18:14:08 +05:00
index={index}
colors={colors}
/>
)}
/>
<View
style={{
justifyContent: 'space-between',
2019-12-03 18:12:50 +05:00
alignItems: 'center',
2020-01-18 18:14:08 +05:00
flexDirection: 'row',
width: '100%',
marginTop: 20,
2019-12-03 18:12:50 +05:00
}}>
2020-01-18 18:14:08 +05:00
<TouchableOpacity
activeOpacity={opacity}
2020-01-20 15:32:32 +05:00
onPress={this.addNewNotebook}
2019-12-03 18:12:50 +05:00
style={{
2020-01-18 18:14:08 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '48%',
justifyContent: 'center',
alignItems: 'center',
borderColor: colors.accent,
backgroundColor: colors.accent,
borderWidth: 1,
2019-12-03 18:12:50 +05:00
}}>
2020-01-18 18:14:08 +05:00
<Text
style={{
fontFamily: WEIGHT.medium,
color: 'white',
fontSize: SIZE.sm,
}}>
Add
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={opacity}
2020-01-20 15:32:32 +05:00
onPress={this.close}
2020-01-18 18:14:08 +05:00
style={{
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '48%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.nav,
}}>
<Text
style={{
fontFamily: WEIGHT.medium,
color: colors.icon,
fontSize: SIZE.sm,
}}>
Cancel
</Text>
</TouchableOpacity>
</View>
2019-12-03 18:12:50 +05:00
</View>
2020-01-18 18:14:08 +05:00
</KeyboardAvoidingView>
</Modal>
);
}
}
2019-12-03 18:12:50 +05:00
2020-01-20 15:32:32 +05:00
const TopicItem = ({item, index, colors, onPress, onDelete}) => {
2019-12-03 18:12:50 +05:00
const topicRef = ref => (refs[index] = ref);
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 10,
}}>
2020-01-20 15:32:32 +05:00
<TouchableOpacity
2019-12-03 18:12:50 +05:00
style={{
2020-01-20 15:32:32 +05:00
width: '100%',
2019-12-03 18:12:50 +05:00
}}
2020-01-20 15:32:32 +05:00
onPress={() => {
onPress(item, index);
}}>
<TextInput
ref={topicRef}
editable={false}
style={{
padding: pv - 5,
paddingHorizontal: 0,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
paddingHorizontal: ph,
borderBottomWidth: 1.5,
borderBottomColor: colors.nav,
paddingRight: 40,
width: '100%',
maxWidth: '100%',
}}
defaultValue={index + 1 + '. ' + item}
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
</TouchableOpacity>
2019-12-03 22:05:47 +05:00
<TouchableOpacity
2020-01-20 15:32:32 +05:00
onPress={() => {
onDelete(index);
}}
2019-12-03 22:05:47 +05:00
style={{
justifyContent: 'center',
alignItems: 'center',
2020-01-20 15:32:32 +05:00
position: 'absolute',
right: 0,
borderColor: colors.nav,
2020-01-08 22:47:49 +05:00
borderRadius: 5,
width: 40,
height: 40,
2019-12-03 22:05:47 +05:00
}}>
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>
);
};