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

125 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-12-15 14:11:45 +05:00
import React, {createRef} from 'react';
import {Actions} from '../../provider/Actions';
2020-12-17 10:31:35 +05:00
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent,
} from '../../services/EventManager';
2020-12-15 14:11:45 +05:00
import {db} from '../../utils/DB';
2020-12-17 10:31:35 +05:00
import {
eCloseAddTopicDialog,
eOnNewTopicAdded,
eOpenAddTopicDialog,
} from '../../utils/Events';
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-17 15:42:55 +05:00
import Input from '../Input';
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,
loading:false
2020-01-18 18:14:17 +05:00
};
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 () => {
this.setState({loading:true})
2020-01-18 18:14:17 +05:00
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) {
2020-12-31 12:28:31 +05:00
await db.notebooks.notebook(this.notebook.id).topics.add(this.title);
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-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-17 10:31:35 +05:00
componentDidMount() {
eSubscribeEvent(eOpenAddTopicDialog, this.open);
eSubscribeEvent(eCloseAddTopicDialog, this.close);
}
componentWillUnmount() {
eUnSubscribeEvent(eOpenAddTopicDialog, this.open);
eUnSubscribeEvent(eCloseAddTopicDialog, this.close);
}
open = async (notebookId) => {
let id = notebookId || this.props.notebookID;
2021-01-08 13:10:25 +05:00
this.notebook = await db.notebooks.notebook(id).data;
if (this.props.toEdit) {
this.title = this.props.toEdit.title;
}
2020-01-18 18:14:17 +05:00
this.setState({
visible: true,
});
2020-12-17 10:31:35 +05:00
};
2020-12-07 11:00:10 +05:00
close = () => {
2021-01-08 13:10:25 +05:00
this.props.close();
2020-01-18 18:14:17 +05:00
this.title = null;
2021-01-08 13:10:25 +05:00
this.notebook = null;
2020-01-18 18:14:17 +05:00
this.setState({
visible: false,
});
2020-12-07 11:10:20 +05:00
};
2020-01-18 18:14:17 +05:00
render() {
2020-12-17 15:42:55 +05:00
const {visible} = 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-12-17 15:42:55 +05:00
<Input
fwdRef={this.titleRef}
2020-09-27 13:05:26 +05:00
onChangeText={(value) => {
this.title = value;
}}
2020-12-17 15:42:55 +05:00
blurOnSubmit={false}
defaultValue={toEdit ? toEdit.title : null}
2020-09-27 13:05:26 +05:00
placeholder="Enter title of topic"
onSubmit={() => this.addNewTopic()}
2020-09-27 13:05:26 +05:00
/>
2020-01-18 18:14:17 +05:00
2020-09-27 13:05:26 +05:00
<DialogButtons
positiveTitle={toEdit ? 'Save' : 'Add'}
2021-01-08 13:10:25 +05:00
onPressNegative={() => this.close()}
onPressPositive={() => this.addNewTopic()}
loading={this.state.loading}
2020-09-27 13:05:26 +05:00
/>
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
);
}
}