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

489 lines
13 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-18 01:04:33 +05:00
import {getElevation, ToastEvent} 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,
topics: [''],
title: null,
description: null,
titleFocused: false,
descFocused: false,
};
this.listRef;
this.prevItem = null;
this.prevIndex = null;
this.currentSelectedInput = null;
this.timestamp = null;
this.backPressCount = 0;
}
open() {
refs = [];
let {toEdit} = this.props;
if (toEdit !== null) {
let topicsList = [];
toEdit.topics.forEach(item => {
if (item.title !== 'General') {
topicsList.push(item.title);
}
});
topicsList.push('');
this.setState({
topics: [...topicsList],
title: toEdit.title,
visible: true,
});
this.timestamp = toEdit.dateCreated;
}
}
close() {
refs = [];
this.prevIndex = null;
this.prevItem = null;
this.currentSelectedInput = null;
this.setState({
visible: false,
topics: [''],
});
}
onSubmit = (text, index, willFocus = false) => {
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
prevTopics[index] = text;
2020-01-18 18:14:08 +05:00
this.prevIndex = index;
this.prevItem = text;
2019-12-03 18:12:50 +05:00
if (
2019-12-14 21:52:14 +05:00
prevTopics.length === index + 1 &&
2020-01-18 18:14:08 +05:00
this.prevIndex !== null &&
this.prevItem !== null
2019-12-03 18:12:50 +05:00
) {
2019-12-14 21:52:14 +05:00
prevTopics.push('');
2019-12-03 18:12:50 +05:00
}
2019-12-14 21:52:14 +05:00
let nextTopics = [...prevTopics];
2020-01-18 18:14:08 +05:00
this.setState({
topics: nextTopics,
});
this.currentSelectedInput = null;
2019-12-03 18:12:50 +05:00
if (!refs[index + 1]) {
setTimeout(() => {
2019-12-03 22:05:47 +05:00
if (!refs[index + 1]) return;
2019-12-03 18:12:50 +05:00
refs[index + 1].focus();
2020-01-06 19:03:21 +05:00
}, 300);
2019-12-03 18:12:50 +05:00
} else {
2019-12-03 22:05:47 +05:00
setTimeout(() => {
refs[index + 1].focus();
2020-01-06 19:03:21 +05:00
}, 300);
2019-12-03 18:12:50 +05:00
}
};
2020-01-06 19:03:21 +05:00
2020-01-18 18:14:08 +05:00
onBlur = (text, index) => {};
2020-01-06 19:03:21 +05:00
2020-01-18 18:14:08 +05:00
onFocus = index => {
this.currentSelectedInput = index;
2019-12-14 21:52:14 +05:00
2020-01-18 18:14:08 +05:00
if (this.currentSelectedInput) {
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
2020-01-18 18:14:08 +05:00
prevTopics[this.prevIndex] = this.prevItem;
if (prevTopics.length === this.prevIndex + 1) {
2019-12-14 21:52:14 +05:00
prevTopics.push('');
2019-12-03 18:12:50 +05:00
}
2020-01-18 18:14:08 +05:00
this.prevIndex = null;
this.prevItem = null;
2019-12-14 21:52:14 +05:00
let nextTopics = [...prevTopics];
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
onChange = (text, index) => {
this.prevIndex = index;
this.prevItem = text;
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 => {
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
if (prevTopics.length === 1) return;
refs = [];
prevTopics.splice(index, 1);
let nextTopics = [...prevTopics];
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 () => {
let {toEdit} = this.props;
2019-12-06 11:09:45 +05:00
if (!title)
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({
2019-12-06 11:09:45 +05:00
title,
description,
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-06 19:03:21 +05:00
onKeyPress = (event, index, text) => {
if (event.nativeEvent.key === 'Backspace') {
2020-01-18 18:14:08 +05:00
if (this.backPressCount === 0 && (!text || text.length == 0)) {
this.backPressCount = 1;
2020-01-06 19:03:21 +05:00
return;
}
2020-01-18 18:14:08 +05:00
if (this.backPressCount === 1 && (!text || text.length == 0)) {
this.backPressCount = 0;
2020-01-06 19:03:21 +05:00
if (!refs[index] == 0) {
refs[index - 1].focus();
}
2020-01-18 18:14:08 +05:00
this.onDelete(index);
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,
description,
title,
topics,
visible,
} = this.state;
return (
<Modal
visible={visible}
transparent={true}
animated
animationType="fade"
onRequestClose={() => (refs = [])}>
<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
onPress={() => 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%',
maxHeight: 350,
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-18 18:14:08 +05:00
{toEdit ? '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 => {
this.setState({
title: value,
});
}}
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 => {
this.setState({
description: value,
});
}}
placeholder="write a description"
placeholderTextColor={colors.icon}
/>
<Text
style={{
fontSize: SIZE.sm,
fontFamily: WEIGHT.bold,
color: colors.pri,
}}>
Topics:
</Text>
<FlatList
data={topics}
ref={ref => (this.listRef = ref)}
removeClippedSubviews={false}
enableEmptySections={false}
getItemLayout={(data, index) => ({
length: 50,
offset: 50 * index,
index,
})}
keyExtractor={(item, index) => item + index}
renderItem={({item, index}) => (
<TopicItem
item={item}
toEdit={toEdit ? true : false}
index={index}
colors={colors}
onSubmit={this.onSubmit}
onChange={this.onChange}
onFocus={this.onFocus}
onDelete={this.onDelete}
onKeyPress={this.onKeyPress}
onBlur={this.onBlur}
/>
)}
/>
<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}
onPress={async () => {
await 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}
onPress={() => {
this.close();
}}
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
const TopicItem = ({
item,
index,
onFocus,
onSubmit,
onDelete,
onChange,
colors,
2019-12-21 09:46:08 +05:00
toEdit,
2020-01-06 19:03:21 +05:00
onKeyPress,
onBlur,
2019-12-03 18:12:50 +05:00
}) => {
2020-01-08 22:47:49 +05:00
const [focus, setFocus] = useState(false);
2019-12-03 18:12:50 +05:00
const topicRef = ref => (refs[index] = ref);
let text = item;
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
2020-01-08 22:47:49 +05:00
2019-12-03 18:12:50 +05:00
marginTop: 10,
}}>
<TextInput
ref={topicRef}
onFocus={() => {
2020-01-06 19:03:21 +05:00
text = item;
2019-12-03 18:12:50 +05:00
onFocus(index);
setFocus(true);
}}
onBlur={() => {
2020-01-06 19:03:21 +05:00
onBlur(text, index);
2019-12-03 18:12:50 +05:00
setFocus(false);
}}
onChangeText={value => {
onChange(value, index);
text = value;
}}
2020-01-06 19:03:21 +05:00
onSubmitEditing={() => {
onSubmit(text, index, true);
}}
2019-12-03 18:12:50 +05:00
blurOnSubmit
2020-01-06 19:03:21 +05:00
onKeyPress={event => onKeyPress(event, index, text)}
2019-12-03 18:12:50 +05:00
style={{
padding: pv - 5,
paddingHorizontal: 0,
2020-01-08 22:47:49 +05:00
borderRadius: 5,
borderWidth: 1.5,
2019-12-03 18:12:50 +05:00
fontSize: SIZE.sm,
2020-01-08 22:47:49 +05:00
borderColor: focus ? colors.accent : colors.nav,
2019-12-03 18:12:50 +05:00
fontFamily: WEIGHT.regular,
2019-12-09 13:17:40 +05:00
color: colors.pri,
2020-01-08 22:47:49 +05:00
paddingHorizontal: ph,
width: '85%',
maxWidth: '85%',
2019-12-03 18:12:50 +05:00
}}
2019-12-21 09:46:08 +05:00
defaultValue={item}
2019-12-03 18:12:50 +05:00
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
2019-12-03 22:05:47 +05:00
<TouchableOpacity
onPress={() => (!focus ? onDelete(index) : onSubmit(text, index, true))}
style={{
justifyContent: 'center',
alignItems: 'center',
2020-01-08 22:47:49 +05:00
borderWidth: 1.5,
borderColor: focus ? colors.accent : colors.nav,
borderRadius: 5,
width: 40,
height: 40,
2019-12-03 22:05:47 +05:00
}}>
<Icon
name={!focus ? 'minus' : 'plus'}
size={SIZE.lg}
2020-01-08 22:47:49 +05:00
color={focus ? colors.accent : colors.icon}
2019-12-03 22:05:47 +05:00
/>
</TouchableOpacity>
2019-12-03 18:12:50 +05:00
</View>
);
};