mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
move notebook dialog to dialog manager
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, {createRef, useState} from 'react';
|
import React, {useState} from 'react';
|
||||||
import {
|
import {
|
||||||
KeyboardAvoidingView,
|
KeyboardAvoidingView,
|
||||||
Modal,
|
Modal,
|
||||||
@@ -11,46 +11,83 @@ import {FlatList, TextInput} from 'react-native-gesture-handler';
|
|||||||
import Icon from 'react-native-vector-icons/Feather';
|
import Icon from 'react-native-vector-icons/Feather';
|
||||||
import {db, DDS} from '../../../App';
|
import {db, DDS} from '../../../App';
|
||||||
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
|
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
|
||||||
import {useTracked} from '../../provider';
|
import {ACTIONS} from '../../provider';
|
||||||
import {getElevation, ToastEvent} from '../../utils/utils';
|
import {getElevation, ToastEvent} from '../../utils/utils';
|
||||||
|
import {updateEvent} from '../DialogManager';
|
||||||
|
|
||||||
let refs = [];
|
let refs = [];
|
||||||
|
|
||||||
export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
export class AddNotebookDialog extends React.Component {
|
||||||
const [state, dispatch] = useTracked();
|
constructor(props) {
|
||||||
const {colors} = state;
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
visible: false,
|
||||||
|
topics: [''],
|
||||||
|
title: null,
|
||||||
|
description: null,
|
||||||
|
titleFocused: false,
|
||||||
|
descFocused: false,
|
||||||
|
};
|
||||||
|
|
||||||
const [topics, setTopics] = useState(['']);
|
this.listRef;
|
||||||
const [title, setTitle] = useState(null);
|
this.prevItem = null;
|
||||||
|
this.prevIndex = null;
|
||||||
|
this.currentSelectedInput = null;
|
||||||
|
this.timestamp = null;
|
||||||
|
this.backPressCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
const [description, setDescription] = useState(null);
|
open() {
|
||||||
const [titleFocused, setTitleFocused] = useState(false);
|
refs = [];
|
||||||
const [descFocused, setDescFocused] = useState(false);
|
let {toEdit} = this.props;
|
||||||
let listRef = createRef();
|
|
||||||
let prevItem = null;
|
|
||||||
let prevIndex = null;
|
|
||||||
let currentSelectedInput = null;
|
|
||||||
let timestamp = null;
|
|
||||||
let backPressCount = 0;
|
|
||||||
|
|
||||||
const onSubmit = (text, index, willFocus = false) => {
|
if (toEdit !== null) {
|
||||||
console.log('here');
|
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) => {
|
||||||
let prevTopics = topics;
|
let prevTopics = topics;
|
||||||
prevTopics[index] = text;
|
prevTopics[index] = text;
|
||||||
prevIndex = index;
|
this.prevIndex = index;
|
||||||
prevItem = text;
|
this.prevItem = text;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
prevTopics.length === index + 1 &&
|
prevTopics.length === index + 1 &&
|
||||||
prevIndex !== null &&
|
this.prevIndex !== null &&
|
||||||
prevItem !== null
|
this.prevItem !== null
|
||||||
) {
|
) {
|
||||||
prevTopics.push('');
|
prevTopics.push('');
|
||||||
}
|
}
|
||||||
|
|
||||||
let nextTopics = [...prevTopics];
|
let nextTopics = [...prevTopics];
|
||||||
setTopics(nextTopics);
|
this.setState({
|
||||||
currentSelectedInput = null;
|
topics: nextTopics,
|
||||||
|
});
|
||||||
|
this.currentSelectedInput = null;
|
||||||
if (!refs[index + 1]) {
|
if (!refs[index + 1]) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!refs[index + 1]) return;
|
if (!refs[index + 1]) return;
|
||||||
@@ -63,41 +100,45 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onBlur = (text, index) => {};
|
onBlur = (text, index) => {};
|
||||||
|
|
||||||
const onFocus = index => {
|
onFocus = index => {
|
||||||
currentSelectedInput = index;
|
this.currentSelectedInput = index;
|
||||||
|
|
||||||
if (currentSelectedInput) {
|
if (this.currentSelectedInput) {
|
||||||
let prevTopics = topics;
|
let prevTopics = topics;
|
||||||
|
|
||||||
prevTopics[prevIndex] = prevItem;
|
prevTopics[this.prevIndex] = this.prevItem;
|
||||||
if (prevTopics.length === prevIndex + 1) {
|
if (prevTopics.length === this.prevIndex + 1) {
|
||||||
prevTopics.push('');
|
prevTopics.push('');
|
||||||
}
|
}
|
||||||
prevIndex = null;
|
this.prevIndex = null;
|
||||||
prevItem = null;
|
this.prevItem = null;
|
||||||
|
|
||||||
let nextTopics = [...prevTopics];
|
let nextTopics = [...prevTopics];
|
||||||
setTopics(nextTopics);
|
this.setState({
|
||||||
|
topics: nextTopics,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const onChange = (text, index) => {
|
onChange = (text, index) => {
|
||||||
prevIndex = index;
|
this.prevIndex = index;
|
||||||
prevItem = text;
|
this.prevItem = text;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onDelete = index => {
|
onDelete = index => {
|
||||||
let prevTopics = topics;
|
let prevTopics = topics;
|
||||||
|
|
||||||
if (prevTopics.length === 1) return;
|
if (prevTopics.length === 1) return;
|
||||||
refs = [];
|
refs = [];
|
||||||
prevTopics.splice(index, 1);
|
prevTopics.splice(index, 1);
|
||||||
let nextTopics = [...prevTopics];
|
let nextTopics = [...prevTopics];
|
||||||
setTopics(nextTopics);
|
this.setState({
|
||||||
|
topics: nextTopics,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const addNewNotebook = async () => {
|
addNewNotebook = async () => {
|
||||||
|
let {toEdit} = this.props;
|
||||||
if (!title)
|
if (!title)
|
||||||
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
|
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
|
||||||
|
|
||||||
@@ -109,59 +150,44 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
topics,
|
topics,
|
||||||
dateCreated: dateCreated,
|
dateCreated: dateCreated,
|
||||||
});
|
});
|
||||||
|
updateEvent({type: ACTIONS.NOTEBOOKS});
|
||||||
prevIndex = null;
|
this.close();
|
||||||
prevItem = null;
|
|
||||||
currentSelectedInput = null;
|
|
||||||
refs = [];
|
|
||||||
setTopics(['']);
|
|
||||||
close(true);
|
|
||||||
ToastEvent.show('New notebook added', 'success', 3000, () => {}, '');
|
ToastEvent.show('New notebook added', 'success', 3000, () => {}, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
onKeyPress = (event, index, text) => {
|
onKeyPress = (event, index, text) => {
|
||||||
if (event.nativeEvent.key === 'Backspace') {
|
if (event.nativeEvent.key === 'Backspace') {
|
||||||
if (backPressCount === 0 && (!text || text.length == 0)) {
|
if (this.backPressCount === 0 && (!text || text.length == 0)) {
|
||||||
backPressCount = 1;
|
this.backPressCount = 1;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (this.backPressCount === 1 && (!text || text.length == 0)) {
|
||||||
if (backPressCount === 1 && (!text || text.length == 0)) {
|
this.backPressCount = 0;
|
||||||
backPressCount = 0;
|
|
||||||
if (!refs[index] == 0) {
|
if (!refs[index] == 0) {
|
||||||
refs[index - 1].focus();
|
refs[index - 1].focus();
|
||||||
}
|
}
|
||||||
onDelete(index);
|
this.onDelete(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const {colors, toEdit} = this.props;
|
||||||
|
const {
|
||||||
|
titleFocused,
|
||||||
|
descFocused,
|
||||||
|
description,
|
||||||
|
title,
|
||||||
|
topics,
|
||||||
|
visible,
|
||||||
|
} = this.state;
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
visible={visible}
|
visible={visible}
|
||||||
transparent={true}
|
transparent={true}
|
||||||
animated
|
animated
|
||||||
animationType="fade"
|
animationType="fade"
|
||||||
onShow={() => {
|
|
||||||
refs = [];
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
onRequestClose={() => (refs = [])}>
|
onRequestClose={() => (refs = [])}>
|
||||||
<KeyboardAvoidingView
|
<KeyboardAvoidingView
|
||||||
behavior={Platform.OS === 'ios' ? 'padding' : null}
|
behavior={Platform.OS === 'ios' ? 'padding' : null}
|
||||||
@@ -222,14 +248,20 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
marginBottom: 5,
|
marginBottom: 5,
|
||||||
}}
|
}}
|
||||||
onFocus={() => {
|
onFocus={() => {
|
||||||
setTitleFocused(true);
|
this.setState({
|
||||||
|
titleFocused: true,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
onBlur={() => {
|
onBlur={() => {
|
||||||
setTitleFocused(false);
|
this.setState({
|
||||||
|
titleFocused: false,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
defaultValue={toEdit ? toEdit.title : null}
|
defaultValue={toEdit ? toEdit.title : null}
|
||||||
onChangeText={value => {
|
onChangeText={value => {
|
||||||
setTitle(value);
|
this.setState({
|
||||||
|
title: value,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
placeholder="Title of notebook"
|
placeholder="Title of notebook"
|
||||||
placeholderTextColor={colors.icon}
|
placeholderTextColor={colors.icon}
|
||||||
@@ -248,14 +280,20 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
marginBottom: 10,
|
marginBottom: 10,
|
||||||
}}
|
}}
|
||||||
onFocus={() => {
|
onFocus={() => {
|
||||||
setDescFocused(true);
|
this.setState({
|
||||||
|
descFocused: true,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
onBlur={() => {
|
onBlur={() => {
|
||||||
setDescFocused(false);
|
this.setState({
|
||||||
|
descFocused: false,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
defaultValue={toEdit ? toEdit.description : null}
|
defaultValue={toEdit ? toEdit.description : null}
|
||||||
onChangeText={value => {
|
onChangeText={value => {
|
||||||
setDescription(value);
|
this.setState({
|
||||||
|
description: value,
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
placeholder="write a description"
|
placeholder="write a description"
|
||||||
placeholderTextColor={colors.icon}
|
placeholderTextColor={colors.icon}
|
||||||
@@ -272,7 +310,7 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
|
|
||||||
<FlatList
|
<FlatList
|
||||||
data={topics}
|
data={topics}
|
||||||
ref={listRef}
|
ref={ref => (this.listRef = ref)}
|
||||||
removeClippedSubviews={false}
|
removeClippedSubviews={false}
|
||||||
enableEmptySections={false}
|
enableEmptySections={false}
|
||||||
getItemLayout={(data, index) => ({
|
getItemLayout={(data, index) => ({
|
||||||
@@ -287,12 +325,12 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
toEdit={toEdit ? true : false}
|
toEdit={toEdit ? true : false}
|
||||||
index={index}
|
index={index}
|
||||||
colors={colors}
|
colors={colors}
|
||||||
onSubmit={onSubmit}
|
onSubmit={this.onSubmit}
|
||||||
onChange={onChange}
|
onChange={this.onChange}
|
||||||
onFocus={onFocus}
|
onFocus={this.onFocus}
|
||||||
onDelete={onDelete}
|
onDelete={this.onDelete}
|
||||||
onKeyPress={onKeyPress}
|
onKeyPress={this.onKeyPress}
|
||||||
onBlur={onBlur}
|
onBlur={this.onBlur}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
@@ -309,7 +347,7 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
activeOpacity={opacity}
|
activeOpacity={opacity}
|
||||||
onPress={async () => {
|
onPress={async () => {
|
||||||
await addNewNotebook();
|
await this.addNewNotebook();
|
||||||
}}
|
}}
|
||||||
style={{
|
style={{
|
||||||
paddingVertical: pv,
|
paddingVertical: pv,
|
||||||
@@ -335,12 +373,7 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
activeOpacity={opacity}
|
activeOpacity={opacity}
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
setTopics(['']);
|
this.close();
|
||||||
refs = [];
|
|
||||||
prevIndex = null;
|
|
||||||
prevItem = null;
|
|
||||||
currentSelectedInput = null;
|
|
||||||
close();
|
|
||||||
}}
|
}}
|
||||||
style={{
|
style={{
|
||||||
paddingVertical: pv,
|
paddingVertical: pv,
|
||||||
@@ -365,7 +398,8 @@ export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
|
|||||||
</KeyboardAvoidingView>
|
</KeyboardAvoidingView>
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const TopicItem = ({
|
const TopicItem = ({
|
||||||
item,
|
item,
|
||||||
|
|||||||
Reference in New Issue
Block a user