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

627 lines
17 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-11-02 09:22:26 +05:00
Keyboard,
KeyboardAvoidingView,
Modal,
Platform,
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
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-11-02 09:22:26 +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';
import {ActionIcon} from '../ActionIcon';
2020-11-14 15:46:26 +05:00
import DialogButtons from '../Dialog/dialog-buttons';
import DialogHeader from '../Dialog/dialog-header';
2020-11-20 01:23:05 +05:00
import Paragraph from '../Typography/Paragraph';
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 {
2020-11-02 09:22:26 +05:00
constructor(props) {
super(props);
this.state = {
visible: false,
topics: [],
description: null,
titleFocused: false,
descFocused: false,
count: 0,
topicInputFocused: false,
editTopic: false,
2020-10-18 15:14:46 +05:00
};
2020-11-02 09:22:26 +05:00
this.title = null;
this.description = null;
this.listRef;
this.prevItem = null;
this.prevIndex = null;
this.currentSelectedInput = null;
this.id = null;
this.backPressCount = 0;
this.currentInputValue = null;
this.titleRef;
this.descriptionRef;
this.topicsToDelete = [];
}
open = () => {
refs = [];
let {toEdit} = this.props;
if (toEdit && toEdit.type === 'notebook') {
let topicsList = [];
toEdit.topics.forEach((item, index) => {
if (index === 0) return;
topicsList.push(item.title);
});
console.log(topicsList);
this.id = toEdit.id;
this.title = toEdit.title;
this.description = toEdit.description;
this.setState({
topics: [...topicsList],
visible: true,
});
} else {
this.setState({
visible: true,
});
}
};
close = () => {
refs = [];
this.prevIndex = null;
this.prevItem = null;
this.currentSelectedInput = null;
this.title = null;
this.description = null;
this.id = null;
this.setState({
visible: false,
topics: [],
descFocused: false,
titleFocused: false,
});
};
onDelete = (index) => {
let {topics} = this.state;
let prevTopics = topics;
refs = [];
prevTopics.splice(index, 1);
let edit = this.props.toEdit;
if (edit && edit.id) {
let topicToDelete = edit.topics[index + 1];
console.log(topicToDelete);
if (topicToDelete) {
this.topicsToDelete.push(topicToDelete.id);
}
}
let nextTopics = [...prevTopics];
if (this.prevIndex === index) {
this.prevIndex = null;
this.prevItem = null;
this.currentInputValue = null;
this.topicInputRef.setNativeProps({
text: null,
});
}
this.setState({
topics: nextTopics,
});
};
2020-05-13 01:46:02 +05:00
2020-11-02 09:22:26 +05:00
addNewNotebook = async () => {
let {topics} = this.state;
let edit = this.props.toEdit;
2020-05-12 14:27:22 +05:00
2020-11-02 09:22:26 +05:00
if (!this.title || this.title?.trim().length === 0)
return ToastEvent.show('Title is required', 'error', 'local');
2020-09-14 22:59:19 +05:00
2020-11-02 09:22:26 +05:00
let id = edit && edit.id ? edit.id : null;
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
let toEdit;
if (id) {
toEdit = db.notebooks.notebook(edit.id).data;
}
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
let prevTopics = [...topics];
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
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;
}
await db.notebooks.add({
title: this.title,
description: this.description,
id: id,
});
let nextTopics = toEdit.topics.map((topic, index) => {
if (index === 0) return topic;
let copy = {...topic};
copy.title = prevTopics[index - 1];
return copy;
});
prevTopics.forEach((title, index) => {
if (!nextTopics[index + 1]) {
nextTopics.push(title);
2020-05-12 14:09:56 +05:00
}
2020-11-02 09:22:26 +05:00
});
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();
updateEvent({type: Actions.NOTEBOOKS});
updateEvent({type: Actions.PINNED});
//ToastEvent.show('New notebook added', 'success', 'local');
};
onSubmit = (forward = true) => {
let {topics} = this.state;
if (!this.currentInputValue || this.currentInputValue?.trim().length === 0)
return;
let prevTopics = [...topics];
if (this.prevItem === null) {
prevTopics.push(this.currentInputValue);
this.setState({
topics: prevTopics,
});
this.topicInputRef.setNativeProps({
text: null,
});
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
this.currentInputValue = null;
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics,
});
this.currentInputValue = null;
/* if (prevTopics[this.prevIndex + 1] && forward) {
2020-10-18 15:14:46 +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],
});
2020-11-02 09:22:26 +05:00
this.setState({
editTopic:true
})
} else {} */
if (this.state.editTopic) {
this.topicInputRef.blur();
Keyboard.dismiss();
this.setState({
editTopic: false,
});
}
this.prevItem = null;
this.prevIndex = null;
this.currentInputValue = null;
this.topicInputRef.setNativeProps({
text: null,
});
if (forward) {
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
}
2020-01-06 19:03:21 +05:00
}
2020-11-02 09:22:26 +05:00
};
render() {
const {colors, toEdit} = this.props;
const {
titleFocused,
descFocused,
topics,
visible,
topicInputFocused,
} = this.state;
2020-01-18 18:14:08 +05:00
return (
2020-11-02 09:22:26 +05:00
<Modal
visible={visible}
transparent={true}
animated
animationType={DDS.isTab ? 'fade' : 'slide'}
onShow={() => {
this.topicsToDelete = [];
this.titleRef.focus();
}}
onRequestClose={this.close}>
<SafeAreaView>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null}
style={styles.wrapper}>
<TouchableOpacity onPress={this.close} style={styles.overlay} />
<View
style={[
styles.container,
{
backgroundColor: colors.bg,
},
]}>
2020-11-14 15:46:26 +05:00
<DialogHeader
title={
toEdit && toEdit.dateCreated
2020-11-02 09:22:26 +05:00
? 'Edit Notebook'
2020-11-14 15:46:26 +05:00
: 'New Notebook'
}
paragraph={
toEdit && toEdit.dateCreated
? 'Edit your notebook'
: 'Add a new notebook to your notebooks.'
}
/>
2020-11-02 09:22:26 +05:00
<TextInput
ref={(ref) => (this.titleRef = ref)}
style={[
styles.input,
{
borderColor: titleFocused ? colors.accent : colors.nav,
color: colors.pri,
2020-11-14 15:46:26 +05:00
fontSize: SIZE.md,
2020-11-02 09:22:26 +05:00
},
]}
numberOfLines={1}
multiline={false}
onFocus={() => {
this.setState({
titleFocused: true,
});
2020-01-20 15:32:32 +05:00
}}
2020-11-02 09:22:26 +05:00
onBlur={() => {
this.setState({
titleFocused: false,
});
2020-09-07 13:12:41 +05:00
}}
2020-11-02 09:22:26 +05:00
defaultValue={toEdit ? toEdit.title : null}
onChangeText={(value) => {
this.title = value;
}}
onSubmitEditing={() => {
this.descriptionRef.focus();
}}
placeholder="Title"
placeholderTextColor={colors.icon}
/>
<TextInput
ref={(ref) => (this.descriptionRef = ref)}
2020-09-07 13:12:41 +05:00
style={[
2020-11-02 09:22:26 +05:00
styles.input,
{
borderColor: descFocused ? colors.accent : colors.nav,
minHeight: 45,
color: colors.pri,
},
2020-10-18 15:14:46 +05:00
]}
2020-11-02 09:22:26 +05:00
maxLength={150}
onFocus={() => {
this.setState({
descFocused: true,
});
2020-10-18 15:14:46 +05:00
}}
2020-11-02 09:22:26 +05:00
onBlur={() => {
this.setState({
descFocused: false,
});
}}
defaultValue={toEdit ? toEdit.description : null}
onChangeText={(value) => {
this.description = value;
}}
onSubmitEditing={() => {
this.topicInputRef.focus();
}}
placeholder="Describe your notebook."
placeholderTextColor={colors.icon}
/>
<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({
topicInputFocused: true,
});
}}
onBlur={() => {
this.onSubmit(false);
this.setState({
topicInputFocused: false,
editTopic: false,
});
}}
onSubmitEditing={this.onSubmit}
style={[
styles.input,
2020-09-07 13:12:41 +05:00
{
2020-11-02 09:22:26 +05:00
borderColor: topicInputFocused
? colors.accent
: colors.nav,
color: colors.pri,
2020-11-02 20:45:56 +05:00
width: '100%',
maxWidth: '100%',
2020-11-02 09:22:26 +05:00
marginTop: 5,
2020-11-02 20:45:56 +05:00
paddingRight: '15%',
2020-09-07 13:12:41 +05:00
},
2020-11-02 09:22:26 +05:00
]}
placeholder="Add a topic"
placeholderTextColor={colors.icon}
/>
<TouchableOpacity
onPress={this.onSubmit}
style={[
styles.addBtn,
{
borderColor: topicInputFocused
? colors.accent
: colors.nav,
},
]}>
<Icon
name={this.state.editTopic ? 'check' : 'plus'}
size={SIZE.lg}
color={topicInputFocused ? colors.accent : colors.icon}
/>
</TouchableOpacity>
</View>
<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');
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.setNativeProps({
text: item,
});
this.topicInputRef.focus();
this.currentInputValue = item;
this.setState({
editTopic: true,
});
}}
onDelete={this.onDelete}
index={index}
colors={colors}
/>
)}
/>
2020-11-14 15:46:26 +05:00
<DialogButtons
negativeTitle="Cancel"
positiveTitle={toEdit && toEdit.dateCreated ? 'Save' : 'Add'}
onPressPositive={this.addNewNotebook}
onPressNegative={this.close}
/>
2020-11-02 09:22:26 +05:00
</View>
</KeyboardAvoidingView>
<Toast context="local" />
</SafeAreaView>
</Modal>
2020-01-18 18:14:08 +05:00
);
2020-11-02 09:22:26 +05:00
}
}
2019-12-03 18:12:50 +05:00
2020-11-02 09:22:26 +05:00
const TopicItem = ({item, index, colors, onPress, onDelete}) => {
const topicRef = (ref) => (refs[index] = ref);
2020-09-07 13:12:41 +05:00
2020-11-02 09:22:26 +05:00
return (
<View
style={{
2020-10-18 15:14:46 +05:00
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
2020-11-02 20:45:56 +05:00
borderBottomWidth: 1,
borderColor: colors.nav,
2020-11-02 09:22:26 +05:00
}}>
<TouchableOpacity
style={{
width: '80%',
backgroundColor: 'transparent',
zIndex: 10,
position: 'absolute',
height: 30,
}}
onPress={() => {
onPress(item, index);
}}
/>
2020-11-20 01:23:05 +05:00
<Paragraph
2020-11-02 20:45:56 +05:00
style={{
marginRight: index === 0 ? 2 : 0,
}}>
{index + 1 + '.'}
2020-11-20 01:23:05 +05:00
</Paragraph>
2020-11-02 09:22:26 +05:00
<TextInput
ref={topicRef}
editable={false}
style={[
styles.topicInput,
{
color: colors.pri,
},
]}
defaultValue={item}
placeholderTextColor={colors.icon}
/>
<View
2020-11-02 20:45:56 +05:00
style={{
width: 80,
position: 'absolute',
right: 0,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'flex-end',
}}>
<ActionIcon
onPress={() => {
2020-11-02 09:22:26 +05:00
onPress(item, index);
}}
name="pencil"
size={SIZE.lg - 5}
color={colors.icon}
2020-11-02 20:45:56 +05:00
/>
2020-11-02 09:22:26 +05:00
<ActionIcon
onPress={() => {
onDelete(index);
}}
name="minus"
size={SIZE.lg}
color={colors.icon}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
wrapper: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
},
container: {
2020-11-14 15:46:26 +05:00
width: DDS.isTab ? 500 : '80%',
height: DDS.isTab ? 600 : null,
maxHeight: DDS.isTab ? 600 :"90%",
borderRadius: 5,
2020-11-02 20:45:56 +05:00
paddingHorizontal: 12,
2020-11-02 09:22:26 +05:00
paddingVertical: pv,
},
overlay: {
width: '100%',
height: '100%',
position: 'absolute',
},
headingContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
headingText: {
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.xl,
},
input: {
2020-11-14 15:46:26 +05:00
paddingRight: 12,
paddingHorizontal: 0,
2020-11-02 20:45:56 +05:00
borderRadius: 0,
2020-11-02 09:22:26 +05:00
minHeight: 45,
2020-11-14 15:46:26 +05:00
fontSize: SIZE.md,
2020-11-02 09:22:26 +05:00
fontFamily: WEIGHT.regular,
padding: pv - 2,
2020-11-02 20:45:56 +05:00
borderBottomWidth: 1,
2020-11-02 09:22:26 +05:00
marginTop: 10,
marginBottom: 5,
},
addBtn: {
width: '12%',
minHeight: 45,
justifyContent: 'center',
alignItems: 'center',
2020-11-02 20:45:56 +05:00
position: 'absolute',
right: 0,
2020-11-02 09:22:26 +05:00
},
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,
paddingRight: 40,
paddingVertical: 10,
width: '100%',
maxWidth: '100%',
},
topicBtn: {
borderRadius: 5,
width: 40,
height: 40,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
right: 0,
},
2020-09-07 13:12:41 +05:00
});