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

135 lines
3.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-10-13 17:02:14 +05:00
import {getElevation} from '../../utils';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import {eOnNewTopicAdded} from '../../utils/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';
2020-09-27 13:05:26 +05:00
import BaseDialog from '../Dialog/base-dialog';
import DialogHeader from '../Dialog/dialog-header';
import DialogButtons from '../Dialog/dialog-buttons';
2020-11-02 20:45:56 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../utils/SizeUtils';
import {db} from '../../utils/DB';
import {DDS} from '../../services/DeviceDetection';
import Seperator from '../Seperator';
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);
2020-09-14 22:59:00 +05:00
//ToastEvent.show('New topic added', 'success');
2020-05-12 02:56:22 +05:00
} else {
2020-05-12 14:09:56 +05:00
let topic = this.props.toEdit;
topic.title = this.title;
2020-09-27 13:05:26 +05:00
2020-09-14 22:59:00 +05:00
await db.notebooks.notebook(topic.notebookId).topics.add(topic);
2020-05-12 02:56:22 +05:00
}
2020-02-03 00:18:40 +05:00
this.close();
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-11-23 12:32:33 +05:00
if (!visible) return null;
2020-01-18 18:14:17 +05:00
return (
2020-09-27 13:05:26 +05:00
<BaseDialog
2020-03-02 12:12:23 +05:00
onShow={() => {
this.titleRef.current?.focus();
}}
2020-11-26 12:17:37 +05:00
statusBarTranslucent={false}
2020-11-23 12:32:33 +05:00
visible={true}
2020-09-27 13:05:26 +05:00
onRequestClose={this.close}>
2019-12-09 13:17:40 +05:00
<View
style={{
2020-09-27 13:05:26 +05:00
...getElevation(5),
2020-11-02 20:45:56 +05:00
width: DDS.isTab ? 350 : '80%',
2020-09-27 13:05:26 +05:00
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-09 13:17:40 +05:00
}}>
2020-09-27 13:05:26 +05:00
<DialogHeader
icon="book-outline"
title={toEdit ? 'Edit Topic' : 'Add New Topic'}
2019-12-09 13:17:40 +05:00
/>
2020-11-02 20:45:56 +05:00
<Seperator />
2020-09-27 13:05:26 +05:00
<TextInput
ref={this.titleRef}
2019-12-09 13:17:40 +05:00
style={{
2020-09-27 13:05:26 +05:00
padding: pv,
2020-11-02 20:45:56 +05:00
borderBottomWidth: 1,
2020-09-27 13:05:26 +05:00
borderColor: titleFocused ? colors.accent : colors.nav,
2020-01-18 18:14:17 +05:00
paddingHorizontal: ph,
2020-09-27 13:05:26 +05:00
borderRadius: 5,
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}
onChangeText={(value) => {
this.title = value;
}}
placeholder="Enter title of topic"
placeholderTextColor={colors.icon}
/>
2020-01-18 18:14:17 +05:00
2020-09-27 13:05:26 +05:00
<DialogButtons
positiveTitle={toEdit ? 'Save' : 'Add'}
2020-11-26 12:17:37 +05:00
onPressNegative={() => {
this.title = null;
this.setState({
visible: false,
});
}}
2020-09-27 13:05:26 +05:00
onPressPositive={this.addNewTopic}
/>
2019-12-09 13:17:40 +05:00
</View>
2020-03-17 10:46:21 +05:00
<Toast context="local" />
2020-09-27 13:05:26 +05:00
</BaseDialog>
2020-01-18 18:14:17 +05:00
);
}
}