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

170 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-03-02 12:12:23 +05:00
import React, {createRef} from 'react';
2020-01-18 01:04:33 +05:00
import {Modal, Text, TouchableOpacity, View} from 'react-native';
import {TextInput} from 'react-native-gesture-handler';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-01-18 01:04:33 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-03-14 13:54:16 +05:00
import {getElevation, ToastEvent, db} from '../../utils/utils';
2020-02-03 00:18:40 +05:00
import {eSendEvent} from '../../services/eventManager';
import {eOnNewTopicAdded} from '../../services/events';
2020-03-17 10:46:21 +05:00
import {Toast} from '../Toast';
2020-09-09 11:10:35 +05:00
import {Button} from '../Button';
2019-12-09 13:17:40 +05:00
2020-01-18 18:14:17 +05:00
export class AddTopicDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false,
titleFocused: false,
};
this.title;
2020-03-02 12:12:23 +05:00
this.titleRef = createRef();
2020-01-18 18:14:17 +05:00
}
2020-01-08 22:47:49 +05:00
2020-01-18 18:14:17 +05:00
addNewTopic = async () => {
if (!this.title)
2020-03-17 10:46:21 +05:00
return ToastEvent.show('Title is required', 'error', 'local');
2020-01-18 18:14:17 +05:00
2020-05-12 02:56:22 +05:00
if (!this.props.toEdit) {
await db.notebooks.notebook(this.props.notebookID).topics.add(this.title);
} else {
2020-05-12 14:09:56 +05:00
let topic = this.props.toEdit;
topic.title = this.title;
await db.notebooks.notebook(this.props.notebookID).topics.add(topic);
2020-05-12 02:56:22 +05:00
}
2020-02-03 00:18:40 +05:00
this.close();
2020-03-17 10:46:21 +05:00
ToastEvent.show('New topic added', 'success');
2020-05-12 02:56:22 +05:00
eSendEvent(eOnNewTopicAdded);
2019-12-09 13:17:40 +05:00
};
2020-01-18 18:14:17 +05:00
open() {
this.setState({
visible: true,
});
}
close() {
this.title = null;
this.setState({
visible: false,
});
}
render() {
2020-01-19 21:31:26 +05:00
const {visible, titleFocused} = this.state;
2020-01-18 18:14:17 +05:00
const {colors, toEdit} = this.props;
2020-01-08 22:47:49 +05:00
2020-01-18 18:14:17 +05:00
return (
<Modal
visible={visible}
animated
animationType="fade"
transparent={true}
2020-03-02 12:12:23 +05:00
onShow={() => {
this.titleRef.current?.focus();
}}
2020-02-03 00:18:40 +05:00
onRequestClose={() => {
refs = [];
this.close();
}}>
2019-12-09 13:17:40 +05:00
<View
style={{
2020-01-18 18:14:17 +05:00
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<TouchableOpacity
2020-02-03 00:18:40 +05:00
onPress={() => this.close()}
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
width: '100%',
height: '100%',
position: 'absolute',
2019-12-09 13:17:40 +05:00
}}
/>
<View
style={{
2020-01-18 18:14:17 +05:00
...getElevation(5),
width: '80%',
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<View
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
flexDirection: 'row',
2019-12-09 13:17:40 +05:00
justifyContent: 'center',
alignItems: 'center',
}}>
2020-02-11 20:33:36 +05:00
<Icon name="book-outline" color={colors.accent} size={SIZE.lg} />
2019-12-09 13:17:40 +05:00
<Text
style={{
2020-01-18 18:14:17 +05:00
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
{toEdit ? 'Edit Topic' : 'Add New Topic'}
2019-12-09 13:17:40 +05:00
</Text>
2020-01-18 18:14:17 +05:00
</View>
2019-12-09 13:17:40 +05:00
2020-01-18 18:14:17 +05:00
<TextInput
2020-03-02 12:12:23 +05:00
ref={this.titleRef}
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
padding: pv,
borderWidth: 1.5,
borderColor: titleFocused ? colors.accent : colors.nav,
2019-12-09 13:17:40 +05:00
paddingHorizontal: ph,
borderRadius: 5,
2020-01-18 18:14:17 +05:00
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
marginTop: 20,
}}
onFocus={() => {
this.setState({
titleFocused: true,
});
}}
onBlur={() => {
this.setState({
titleFocused: true,
});
}}
defaultValue={toEdit ? toEdit.title : null}
2020-09-09 11:10:35 +05:00
onChangeText={(value) => {
2020-01-18 18:14:17 +05:00
this.title = value;
}}
placeholder="Enter title of topic"
placeholderTextColor={colors.icon}
/>
<View
style={{
justifyContent: 'space-around',
2019-12-09 13:17:40 +05:00
alignItems: 'center',
2020-01-18 18:14:17 +05:00
flexDirection: 'row',
marginTop: 20,
2019-12-09 13:17:40 +05:00
}}>
2020-09-09 11:10:35 +05:00
<Button
2020-01-18 18:14:17 +05:00
activeOpacity={opacity}
onPress={async () => await this.addNewTopic()}
2020-09-09 11:10:35 +05:00
title={toEdit ? 'Save' : 'Add'}
/>
<Button
2020-01-18 18:14:17 +05:00
activeOpacity={opacity}
onPress={() => this.close()}
2020-09-09 11:10:35 +05:00
title="Cancel"
/>
2020-01-18 18:14:17 +05:00
</View>
2019-12-09 13:17:40 +05:00
</View>
</View>
2020-03-17 10:46:21 +05:00
<Toast context="local" />
2020-01-18 18:14:17 +05:00
</Modal>
);
}
}