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

128 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-12-15 14:11:45 +05:00
import React, {createRef} from 'react';
import {View} from 'react-native';
import {TextInput} from 'react-native-gesture-handler';
import {Actions} from '../../provider/Actions';
import {DDS} from '../../services/DeviceDetection';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import {getElevation} from '../../utils';
import {db} from '../../utils/DB';
import {eOnNewTopicAdded} from '../../utils/Events';
import {ph, pv, SIZE, WEIGHT} from '../../utils/SizeUtils';
2020-09-27 13:05:26 +05:00
import BaseDialog from '../Dialog/base-dialog';
import DialogButtons from '../Dialog/dialog-buttons';
2020-12-15 14:11:45 +05:00
import DialogContainer from '../Dialog/dialog-container';
2020-12-07 11:10:20 +05:00
import DialogHeader from '../Dialog/dialog-header';
2020-12-15 14:11:45 +05:00
import {updateEvent} from '../DialogManager/recievers';
2020-12-07 11:10:20 +05:00
import Seperator from '../Seperator';
2020-12-15 14:11:45 +05:00
import {Toast} from '../Toast';
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-12-07 11:10:20 +05:00
this.notebook = null;
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;
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-12-07 11:10:20 +05:00
updateEvent({type: Actions.NOTEBOOKS});
2020-05-12 02:56:22 +05:00
eSendEvent(eOnNewTopicAdded);
2019-12-09 13:17:40 +05:00
};
2020-12-07 11:10:20 +05:00
async open() {
2020-12-15 14:11:45 +05:00
console.log(this.props.notebookID);
2020-12-07 11:10:20 +05:00
this.notebook = await db.notebooks.notebook(this.props.notebookID);
2020-01-18 18:14:17 +05:00
this.setState({
visible: true,
});
}
2020-12-07 11:00:10 +05:00
close = () => {
2020-01-18 18:14:17 +05:00
this.title = null;
this.setState({
visible: false,
});
2020-12-07 11:10:20 +05:00
};
2020-01-18 18:14:17 +05:00
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}>
2020-12-15 14:11:45 +05:00
<DialogContainer>
2020-09-27 13:05:26 +05:00
<DialogHeader
icon="book-outline"
2020-12-07 11:10:20 +05:00
title={toEdit ? 'Edit Topic' : 'New Topic'}
paragraph={'Add a new topic to ' + this.notebook.title}
2019-12-09 13:17:40 +05:00
/>
2020-09-27 13:05:26 +05:00
<TextInput
ref={this.titleRef}
2019-12-09 13:17:40 +05:00
style={{
2020-12-15 14:11:45 +05:00
paddingTop: 10,
paddingBottom: 5,
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-12-15 14:11:45 +05:00
paddingHorizontal: 0,
2020-09-27 13:05:26 +05:00
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
}}
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}
/>
2020-12-15 14:11:45 +05:00
</DialogContainer>
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
);
}
}