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

559 lines
14 KiB
JavaScript
Raw Normal View History

import React, {createRef} 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,
TouchableOpacity,
View,
2019-12-03 18:12:50 +05:00
} from 'react-native';
import {FlatList, TextInput} from 'react-native-gesture-handler';
import {notesnook} from '../../../e2e/test.ids';
import {Actions} from '../../provider/Actions';
import {DDS} from '../../services/DeviceDetection';
import {ToastEvent} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {db} from '../../utils/DB';
import {ph, pv, SIZE} from '../../utils/SizeUtils';
import {sleep} from '../../utils/TimeUtils';
import {ActionIcon} from '../ActionIcon';
import BaseDialog from '../Dialog/base-dialog';
2020-11-14 15:46:26 +05:00
import DialogButtons from '../Dialog/dialog-buttons';
import DialogHeader from '../Dialog/dialog-header';
import {updateEvent} from '../DialogManager/recievers';
2020-12-17 15:42:55 +05:00
import Input from '../Input';
import {Toast} from '../Toast';
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,
loading: 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 = [];
2020-12-31 18:53:05 +05:00
this.hiddenInput = createRef();
this.topicInputRef = createRef();
this.addingTopic = false;
2020-11-02 09:22:26 +05:00
}
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);
});
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;
2020-12-28 16:17:39 +05:00
this.currentInputValue = null;
2020-11-02 09:22:26 +05:00
this.id = null;
this.setState({
visible: false,
topics: [],
descFocused: false,
titleFocused: false,
2020-12-28 16:17:39 +05:00
editTopic: false,
2020-11-02 09:22:26 +05:00
});
};
onDelete = index => {
2020-11-02 09:22:26 +05:00
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];
2020-11-23 15:57:31 +05:00
2020-11-02 09:22:26 +05:00
if (topicToDelete) {
this.topicsToDelete.push(topicToDelete.id);
}
}
let nextTopics = [...prevTopics];
if (this.prevIndex === index) {
this.prevIndex = null;
this.prevItem = null;
this.currentInputValue = null;
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.setNativeProps({
2020-11-02 09:22:26 +05:00
text: null,
});
}
this.setState({
topics: nextTopics,
});
};
2020-05-13 01:46:02 +05:00
2020-11-02 09:22:26 +05:00
addNewNotebook = async () => {
this.setState({
loading: true,
});
2020-11-02 09:22:26 +05:00
let {topics} = this.state;
let edit = this.props.toEdit;
2020-05-12 14:27:22 +05:00
if (!this.title || this.title?.trim().length === 0) {
ToastEvent.show({
heading: 'Notebook title is required',
type: 'error',
context: 'local',
});
this.setState({
loading: false,
});
return;
}
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,
});
}
updateEvent({type: Actions.MENU_PINS});
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notebook,
Navigation.routeNames.NotesPage,
]);
this.setState({
loading: false,
});
2020-11-02 09:22:26 +05:00
this.close();
};
onSubmit = (forward = true) => {
2020-12-31 18:53:05 +05:00
this.hiddenInput.current?.focus();
2020-11-02 09:22:26 +05:00
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,
});
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
this.currentInputValue = null;
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics,
});
this.currentInputValue = null;
if (this.state.editTopic) {
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.blur();
2020-11-02 09:22:26 +05:00
Keyboard.dismiss();
this.setState({
editTopic: false,
});
}
this.prevItem = null;
this.prevIndex = null;
this.currentInputValue = null;
if (forward) {
setTimeout(() => {
this.listRef.scrollToEnd({animated: true});
}, 30);
}
2020-01-06 19:03:21 +05:00
}
2021-01-13 13:23:01 +05:00
this.topicInputRef.current?.setNativeProps({
text: '',
});
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.focus();
2020-11-02 09:22:26 +05:00
};
render() {
const {colors, toEdit} = this.props;
const {topics, visible, topicInputFocused} = this.state;
2020-11-23 12:32:33 +05:00
if (!visible) return null;
2020-01-18 18:14:08 +05:00
return (
<BaseDialog
onShow={async () => {
2020-11-02 09:22:26 +05:00
this.topicsToDelete = [];
await sleep(300);
2020-12-31 19:42:09 +05:00
this.titleRef?.focus();
2020-11-02 09:22:26 +05:00
}}
onRequestClose={this.close}>
<TextInput
ref={this.hiddenInput}
style={{
width: 1,
height: 1,
opacity: 0,
position: 'absolute',
}}
blurOnSubmit={false}
/>
<View
style={[
styles.container,
{
backgroundColor: colors.bg,
},
]}>
<DialogHeader
title={
toEdit && toEdit.dateCreated ? 'Edit Notebook' : 'New Notebook'
}
paragraph={
toEdit && toEdit.dateCreated
? 'Edit your notebook'
: 'Add a new notebook'
}
/>
<Input
fwdRef={ref => (this.titleRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.title}
onChangeText={value => {
this.title = value;
2020-12-31 18:53:05 +05:00
}}
placeholder="Enter a title"
onSubmit={() => {
this.descriptionRef.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
defaultValue={toEdit ? toEdit.title : null}
2020-12-31 18:53:05 +05:00
/>
2020-11-02 09:22:26 +05:00
<Input
fwdRef={ref => (this.descriptionRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.description}
onChangeText={value => {
this.description = value;
}}
placeholder="Describe your notebook."
onSubmit={() => {
this.topicInputRef.current?.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
defaultValue={toEdit ? toEdit.description : null}
/>
2020-12-17 15:42:55 +05:00
<Input
fwdRef={this.topicInputRef}
testID={notesnook.ids.dialogs.notebook.inputs.topic}
onChangeText={value => {
this.currentInputValue = value;
if (this.prevItem !== null) {
refs[this.prevIndex].setNativeProps({
text: value,
style: {
borderBottomColor: colors.accent,
},
});
}
}}
returnKeyLabel="Done"
returnKeyType="done"
onSubmit={() => {
this.onSubmit();
}}
blurOnSubmit={false}
button={{
icon: this.state.editTopic ? 'check' : 'plus',
onPress: this.onSubmit,
color: topicInputFocused ? colors.accent : colors.icon,
}}
placeholder="Add a topic"
/>
2020-11-02 09:22:26 +05:00
<FlatList
data={topics}
ref={ref => (this.listRef = ref)}
keyExtractor={(item, index) => item + index.toString()}
renderItem={({item, index}) => (
<TopicItem
item={item}
onPress={(item, index) => {
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.current?.setNativeProps({
text: item,
});
this.topicInputRef.current?.focus();
this.currentInputValue = item;
this.setState({
editTopic: true,
});
2020-12-17 15:42:55 +05:00
}}
onDelete={this.onDelete}
index={index}
colors={colors}
2020-12-17 15:42:55 +05:00
/>
)}
/>
2020-11-02 09:22:26 +05:00
<DialogButtons
negativeTitle="Cancel"
positiveTitle={toEdit && toEdit.dateCreated ? 'Save' : 'Add'}
onPressPositive={this.addNewNotebook}
onPressNegative={this.close}
loading={this.state.loading}
/>
</View>
2020-11-02 09:22:26 +05:00
<Toast context="local" />
</BaseDialog>
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-23 15:57:31 +05:00
width: DDS.isTab ? 500 : '100%',
height: DDS.isTab ? 600 : '100%',
maxHeight: DDS.isTab ? 600 : '100%',
2020-12-31 18:53:05 +05:00
borderRadius: DDS.isTab ? 5 : 0,
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: {
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
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,
2021-01-03 10:38:07 +05:00
//fontFamily: "sans-serif",
2020-11-02 09:22:26 +05:00
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
});