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

402 lines
10 KiB
JavaScript
Raw Normal View History

2019-12-21 09:46:08 +05:00
import React, {useState, createRef, useEffect} from 'react';
2019-12-03 18:12:50 +05:00
import {
View,
Text,
TouchableOpacity,
Platform,
Modal,
2019-12-14 16:00:16 +05:00
KeyboardAvoidingView,
2019-12-03 18:12:50 +05:00
} from 'react-native';
2019-12-14 21:52:14 +05:00
import {SIZE, ph, pv, opacity, WEIGHT} from '../../common/common';
2019-12-03 18:12:50 +05:00
import Icon from 'react-native-vector-icons/Feather';
2019-12-14 21:52:14 +05:00
import {getElevation, ToastEvent} from '../../utils/utils';
2019-12-03 18:12:50 +05:00
import {FlatList, TextInput} from 'react-native-gesture-handler';
2019-12-14 16:00:16 +05:00
import {db, DDS} from '../../../App';
2019-12-14 19:26:44 +05:00
import {useAppContext} from '../../provider/useAppContext';
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
2019-12-21 09:46:08 +05:00
export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
2019-12-14 19:26:44 +05:00
const {colors} = useAppContext();
2019-12-06 11:09:45 +05:00
const [topics, setTopics] = useState(['']);
const [title, setTitle] = useState(null);
2019-12-03 22:05:47 +05:00
let listRef = createRef();
2019-12-03 18:12:50 +05:00
let prevItem = null;
let prevIndex = null;
2019-12-14 21:52:14 +05:00
let currentSelectedInput = null;
2019-12-21 09:46:08 +05:00
let timestamp = null;
2019-12-06 11:09:45 +05:00
let description = 'my first notebook';
2020-01-06 19:03:21 +05:00
let backPressCount = 0;
const onSubmit = (text, index, willFocus = false) => {
console.log('here');
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
prevTopics[index] = text;
2020-01-06 19:03:21 +05:00
prevIndex = index;
prevItem = text;
2019-12-03 18:12:50 +05:00
if (
2019-12-14 21:52:14 +05:00
prevTopics.length === index + 1 &&
2019-12-03 18:12:50 +05:00
prevIndex !== null &&
prevItem !== null
) {
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];
setTopics(nextTopics);
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
const onBlur = (text, index) => {};
2019-12-03 18:12:50 +05:00
const onFocus = index => {
2019-12-14 21:52:14 +05:00
currentSelectedInput = index;
if (currentSelectedInput) {
let prevTopics = topics;
prevTopics[prevIndex] = prevItem;
if (prevTopics.length === prevIndex + 1) {
prevTopics.push('');
2019-12-03 18:12:50 +05:00
}
prevIndex = null;
prevItem = null;
2019-12-14 21:52:14 +05:00
let nextTopics = [...prevTopics];
setTopics(nextTopics);
2019-12-03 18:12:50 +05:00
}
};
const onChange = (text, index) => {
prevIndex = index;
prevItem = text;
};
2019-12-14 21:52:14 +05:00
2019-12-03 18:12:50 +05:00
const onDelete = index => {
2019-12-14 21:52:14 +05:00
let prevTopics = topics;
2019-12-06 11:09:45 +05:00
2019-12-14 21:52:14 +05:00
if (prevTopics.length === 1) return;
refs = [];
prevTopics.splice(index, 1);
let nextTopics = [...prevTopics];
setTopics(nextTopics);
2019-12-03 18:12:50 +05:00
};
2019-12-06 11:09:45 +05:00
const addNewNotebook = async () => {
if (!title)
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
2019-12-11 01:10:04 +05:00
await db.addNotebook({
2019-12-06 11:09:45 +05:00
title,
description,
topics,
2019-12-21 09:46:08 +05:00
timestamp: toEdit ? toEdit.dateCreated : null,
2019-12-06 11:09:45 +05:00
});
2019-12-21 09:46:08 +05:00
console.log(title, description, topics, toEdit.dateCreated);
2019-12-06 11:09:45 +05:00
ToastEvent.show('New notebook added', 'success', 3000, () => {}, '');
setTopics(['']);
prevIndex = null;
prevItem = null;
2019-12-14 21:52:14 +05:00
currentSelectedInput = null;
2019-12-06 11:09:45 +05:00
refs = [];
close(true);
};
2020-01-06 19:03:21 +05:00
onKeyPress = (event, index, text) => {
if (event.nativeEvent.key === 'Backspace') {
if (backPressCount === 0 && (!text || text.length == 0)) {
backPressCount = 1;
return;
}
if (backPressCount === 1 && (!text || text.length == 0)) {
backPressCount = 0;
if (!refs[index] == 0) {
refs[index - 1].focus();
}
onDelete(index);
}
}
};
2019-12-03 18:12:50 +05:00
return (
2019-12-03 22:05:47 +05:00
<Modal
visible={visible}
transparent={true}
2019-12-11 15:20:18 +05:00
animated
animationType="fade"
2019-12-21 09:46:08 +05:00
onShow={() => {
2020-01-06 19:03:21 +05:00
refs = [];
2019-12-21 09:46:08 +05:00
if (toEdit !== null) {
let topicsList = [];
toEdit.topics.forEach(item => {
if (item.title !== 'General') {
topicsList.push(item.title);
}
});
topicsList.push('');
setTopics([...topicsList]);
setTitle(toEdit.title);
timestamp = toEdit.dateCreated;
setTimeout(() => {
console.log(timestamp, title, topics);
}, 400);
}
}}
2019-12-03 22:05:47 +05:00
onRequestClose={() => (refs = [])}>
2019-12-14 16:00:16 +05:00
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null}
2019-12-03 22:05:47 +05:00
style={{
width: '100%',
height: '100%',
backgroundColor: 'rgba(255,255,255,0.3)',
justifyContent: 'center',
alignItems: 'center',
}}>
2019-12-03 18:12:50 +05:00
<View
style={{
2019-12-11 15:20:18 +05:00
...getElevation(5),
2019-12-14 16:00:16 +05:00
width: DDS.isTab ? '50%' : '80%',
2019-12-03 22:05:47 +05:00
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-03 18:12:50 +05:00
}}>
<View
style={{
2019-12-03 22:05:47 +05:00
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}>
<Icon name="book-open" color={colors.accent} size={SIZE.lg} />
<Text
style={{
color: colors.accent,
2019-12-14 16:00:16 +05:00
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
2019-12-03 22:05:47 +05:00
}}>
2019-12-21 09:46:08 +05:00
{toEdit ? 'Edit Notebook' : 'New Notebook'}
2019-12-03 22:05:47 +05:00
</Text>
</View>
<TextInput
style={{
padding: pv - 5,
borderWidth: 1.5,
2019-12-07 12:05:15 +05:00
borderColor: colors.nav,
2019-12-03 18:12:50 +05:00
paddingHorizontal: ph,
2019-12-03 22:05:47 +05:00
borderRadius: 5,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
2019-12-09 13:17:40 +05:00
color: colors.pri,
2019-12-03 22:05:47 +05:00
marginTop: 20,
marginBottom: 10,
}}
2019-12-21 09:46:08 +05:00
defaultValue={toEdit ? toEdit.title : null}
2019-12-06 11:09:45 +05:00
onChangeText={value => {
setTitle(value);
}}
2019-12-03 22:05:47 +05:00
placeholder="Title of notebook"
placeholderTextColor={colors.icon}
/>
<Text
style={{
fontSize: SIZE.sm,
2019-12-14 16:00:16 +05:00
fontFamily: WEIGHT.bold,
2019-12-09 13:17:40 +05:00
color: colors.pri,
2019-12-03 22:05:47 +05:00
}}>
2019-12-09 13:17:40 +05:00
Topics:
2019-12-03 22:05:47 +05:00
</Text>
<FlatList
2019-12-06 11:09:45 +05:00
data={topics}
2019-12-03 22:05:47 +05:00
ref={listRef}
removeClippedSubviews={false}
enableEmptySections={false}
getItemLayout={(data, index) => ({
length: 50,
offset: 50 * index,
index,
})}
keyExtractor={(item, index) => item + index}
2019-12-03 22:05:47 +05:00
renderItem={({item, index}) => (
<TopicItem
item={item}
2019-12-21 09:46:08 +05:00
toEdit={toEdit ? true : false}
2019-12-03 22:05:47 +05:00
index={index}
colors={colors}
onSubmit={onSubmit}
onChange={onChange}
onFocus={onFocus}
onDelete={onDelete}
2020-01-06 19:03:21 +05:00
onKeyPress={onKeyPress}
onBlur={onBlur}
2019-12-03 22:05:47 +05:00
/>
)}
/>
<View
style={{
justifyContent: 'space-around',
alignItems: 'center',
flexDirection: 'row',
marginTop: 20,
2019-12-03 18:12:50 +05:00
}}>
2019-12-03 22:05:47 +05:00
<TouchableOpacity
activeOpacity={opacity}
2019-12-06 11:09:45 +05:00
onPress={async () => {
await addNewNotebook();
}}
2019-12-03 18:12:50 +05:00
style={{
2019-12-03 22:05:47 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '45%',
2019-12-03 18:12:50 +05:00
justifyContent: 'center',
alignItems: 'center',
2019-12-03 22:05:47 +05:00
borderColor: colors.accent,
backgroundColor: colors.accent,
borderWidth: 1,
2019-12-03 18:12:50 +05:00
}}>
<Text
style={{
fontFamily: WEIGHT.medium,
2019-12-03 22:05:47 +05:00
color: 'white',
fontSize: SIZE.sm,
2019-12-03 18:12:50 +05:00
}}>
2019-12-03 22:05:47 +05:00
Add
2019-12-03 18:12:50 +05:00
</Text>
2019-12-03 22:05:47 +05:00
</TouchableOpacity>
2019-12-03 18:12:50 +05:00
2019-12-03 22:05:47 +05:00
<TouchableOpacity
activeOpacity={opacity}
onPress={() => {
2019-12-06 11:09:45 +05:00
setTopics(['']);
2019-12-14 21:52:14 +05:00
refs = [];
2019-12-03 22:05:47 +05:00
prevIndex = null;
prevItem = null;
2019-12-14 21:52:14 +05:00
currentSelectedInput = null;
2019-12-03 22:05:47 +05:00
close();
}}
2019-12-03 18:12:50 +05:00
style={{
2019-12-03 22:05:47 +05:00
paddingVertical: pv,
2019-12-03 18:12:50 +05:00
paddingHorizontal: ph,
borderRadius: 5,
2019-12-03 22:05:47 +05:00
width: '45%',
justifyContent: 'center',
2019-12-03 18:12:50 +05:00
alignItems: 'center',
2019-12-07 12:05:15 +05:00
backgroundColor: colors.nav,
2019-12-03 18:12:50 +05:00
}}>
2019-12-03 22:05:47 +05:00
<Text
2019-12-03 18:12:50 +05:00
style={{
2019-12-03 22:05:47 +05:00
fontFamily: WEIGHT.medium,
color: colors.icon,
fontSize: SIZE.sm,
2019-12-03 18:12:50 +05:00
}}>
2019-12-03 22:05:47 +05:00
Cancel
</Text>
</TouchableOpacity>
2019-12-03 18:12:50 +05:00
</View>
</View>
2019-12-14 16:00:16 +05:00
</KeyboardAvoidingView>
2019-12-03 22:05:47 +05:00
</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
}) => {
const [focus, setFocus] = useState(true);
const topicRef = ref => (refs[index] = ref);
let text = item;
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
borderRadius: 5,
borderWidth: 1.5,
2019-12-07 12:05:15 +05:00
borderColor: colors.nav,
2019-12-03 18:12:50 +05:00
paddingHorizontal: ph,
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,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
2019-12-09 13:17:40 +05:00
color: colors.pri,
2019-12-03 18:12:50 +05:00
width: '90%',
maxWidth: '90%',
}}
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',
}}>
<Icon
name={!focus ? 'minus' : 'plus'}
size={SIZE.lg}
color={colors.accent}
/>
</TouchableOpacity>
2019-12-03 18:12:50 +05:00
</View>
);
};