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

353 lines
8.6 KiB
JavaScript
Raw Normal View History

2019-12-03 22:05:47 +05:00
import React, {useEffect, useState, createRef} from 'react';
2019-12-03 18:12:50 +05:00
import {
ScrollView,
View,
Text,
TouchableOpacity,
Dimensions,
Image,
SafeAreaView,
Platform,
Modal,
} from 'react-native';
import NavigationService from '../../services/NavigationService';
import {
COLOR_SCHEME,
SIZE,
br,
ph,
pv,
opacity,
FONT,
WEIGHT,
} from '../../common/common';
import Icon from 'react-native-vector-icons/Feather';
2019-12-06 11:09:45 +05:00
import {getElevation, h, w, timeSince, ToastEvent} from '../../utils/utils';
2019-12-03 18:12:50 +05:00
import {FlatList, TextInput} from 'react-native-gesture-handler';
import {useForceUpdate} from '../../views/ListsEditor';
2019-12-11 01:10:04 +05:00
import {db} from '../../../App';
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-03 22:05:47 +05:00
export const AddNotebookDialog = ({visible, close}) => {
2019-12-03 18:12:50 +05:00
const [colors, setColors] = useState(COLOR_SCHEME);
2019-12-06 11:09:45 +05:00
const [topics, setTopics] = useState(['']);
const [title, setTitle] = useState(null);
2019-12-03 18:12:50 +05:00
const forceUpdate = useForceUpdate();
2019-12-06 11:09:45 +05:00
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;
let currentSelectedItem = null;
2019-12-06 11:09:45 +05:00
let description = 'my first notebook';
2019-12-03 18:12:50 +05:00
const onSubmit = (text, index, willFocus = true) => {
2019-12-06 11:09:45 +05:00
let oldData = topics;
2019-12-03 18:12:50 +05:00
oldData[index] = text;
if (
oldData.length === index + 1 &&
prevIndex !== null &&
prevItem !== null
) {
oldData.push('');
}
2019-12-06 11:09:45 +05:00
setTopics(oldData);
2019-12-03 18:12:50 +05:00
forceUpdate();
currentSelectedItem = null;
2019-12-03 22:05:47 +05:00
//if (!willFocus) return;
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();
}, 400);
} else {
2019-12-03 22:05:47 +05:00
setTimeout(() => {
refs[index + 1].focus();
}, 400);
2019-12-03 18:12:50 +05:00
}
};
const onFocus = index => {
currentSelectedItem = index;
if (currentSelectedItem) {
2019-12-06 11:09:45 +05:00
let oldData = topics;
2019-12-03 18:12:50 +05:00
oldData[prevIndex] = prevItem;
if (oldData.length === prevIndex + 1) {
oldData.push('');
}
prevIndex = null;
prevItem = null;
2019-12-06 11:09:45 +05:00
setTopics(oldData);
2019-12-03 18:12:50 +05:00
forceUpdate();
}
};
const onChange = (text, index) => {
prevIndex = index;
prevItem = text;
};
const onDelete = index => {
2019-12-06 11:09:45 +05:00
let listData = topics;
2019-12-03 22:05:47 +05:00
if (listData.length === 1) return;
refs.splice(index, 1);
2019-12-03 18:12:50 +05:00
listData.splice(index, 1);
2019-12-06 11:09:45 +05:00
setTopics(listData);
2019-12-03 18:12:50 +05:00
forceUpdate();
};
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,
});
ToastEvent.show('New notebook added', 'success', 3000, () => {}, '');
setTopics(['']);
prevIndex = null;
prevItem = null;
currentSelectedItem = null;
refs = [];
close(true);
};
2019-12-03 18:12:50 +05:00
return (
2019-12-03 22:05:47 +05:00
<Modal
visible={visible}
transparent={true}
onRequestClose={() => (refs = [])}>
<View
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-03 22:05:47 +05:00
width: '80%',
maxHeight: 350,
elevation: 5,
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,
fontFamily: WEIGHT.semibold,
marginLeft: 10,
fontSize: SIZE.lg,
marginTop: -5,
}}>
New Notebook
</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-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,
fontFamily: WEIGHT.semibold,
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,
})}
renderItem={({item, index}) => (
<TopicItem
item={item}
index={index}
colors={colors}
onSubmit={onSubmit}
onChange={onChange}
onFocus={onFocus}
onDelete={onDelete}
/>
)}
/>
<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-03 22:05:47 +05:00
prevIndex = null;
prevItem = null;
currentSelectedItem = null;
2019-12-06 11:09:45 +05:00
refs = [];
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-03 22:05:47 +05:00
</View>
</Modal>
2019-12-03 18:12:50 +05:00
);
};
const TopicItem = ({
item,
index,
onFocus,
onSubmit,
onDelete,
onChange,
colors,
}) => {
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={() => {
onFocus(index);
setFocus(true);
}}
onBlur={() => {
onSubmit(text, index, false);
setFocus(false);
}}
onChangeText={value => {
onChange(value, index);
text = value;
}}
onSubmit={() => onSubmit(text, index, true)}
blurOnSubmit
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%',
}}
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>
);
};