2021-02-20 14:50:27 +05:00
|
|
|
import React, {createRef} from 'react';
|
2021-05-15 12:45:58 +05:00
|
|
|
import {Actions} from '../../provider/Actions';
|
2020-12-17 10:31:35 +05:00
|
|
|
import {
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
2021-02-20 14:50:27 +05:00
|
|
|
ToastEvent,
|
2020-12-17 10:31:35 +05:00
|
|
|
} from '../../services/EventManager';
|
2021-02-20 14:50:27 +05:00
|
|
|
import Navigation from '../../services/Navigation';
|
|
|
|
|
import {db} from '../../utils/DB';
|
|
|
|
|
import {eCloseAddTopicDialog, eOpenAddTopicDialog} from '../../utils/Events';
|
|
|
|
|
import {sleep} from '../../utils/TimeUtils';
|
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';
|
2021-05-15 12:45:58 +05:00
|
|
|
import {updateEvent} from '../DialogManager/recievers';
|
2020-12-17 15:42:55 +05:00
|
|
|
import Input from '../Input';
|
2021-02-20 14:50:27 +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,
|
2021-02-16 16:11:10 +05:00
|
|
|
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;
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = 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 () => {
|
2021-05-15 12:45:58 +05:00
|
|
|
try {
|
|
|
|
|
this.setState({loading: true});
|
|
|
|
|
if (!this.title || this.title?.trim() === '') {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Topic title is required',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
|
|
|
|
this.setState({loading: false});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-18 18:14:17 +05:00
|
|
|
|
2021-05-15 12:45:58 +05:00
|
|
|
if (!this.toEdit) {
|
|
|
|
|
await db.notebooks.notebook(this.notebook.id).topics.add(this.title);
|
|
|
|
|
} else {
|
|
|
|
|
let topic = this.toEdit;
|
|
|
|
|
topic.title = this.title;
|
2020-09-27 13:05:26 +05:00
|
|
|
|
2021-05-15 12:45:58 +05:00
|
|
|
await db.notebooks.notebook(topic.notebookId).topics.add(topic);
|
|
|
|
|
}
|
|
|
|
|
this.setState({loading: false});
|
|
|
|
|
this.close();
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notebooks,
|
|
|
|
|
Navigation.routeNames.Notebook,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
]);
|
|
|
|
|
updateEvent({type: Actions.MENU_PINS});
|
|
|
|
|
} catch (e) {}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 14:50:27 +05:00
|
|
|
open = async ({notebookId, toEdit}) => {
|
|
|
|
|
let id = notebookId;
|
|
|
|
|
if (id) {
|
|
|
|
|
this.notebook = await db.notebooks.notebook(id).data;
|
2021-01-08 13:14:37 +05:00
|
|
|
}
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = toEdit;
|
|
|
|
|
|
|
|
|
|
if (this.toEdit) {
|
|
|
|
|
this.title = this.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;
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = 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-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
|
2021-02-20 14:50:27 +05:00
|
|
|
onShow={async () => {
|
|
|
|
|
await sleep(300);
|
2020-03-02 12:12:23 +05:00
|
|
|
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"
|
2021-02-20 14:50:27 +05:00
|
|
|
title={this.toEdit ? 'Edit Topic' : 'New Topic'}
|
|
|
|
|
paragraph={
|
|
|
|
|
this.toEdit
|
|
|
|
|
? 'Edit title of the topic'
|
|
|
|
|
: '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}
|
2021-05-15 12:45:58 +05:00
|
|
|
onChangeText={value => {
|
2020-09-27 13:05:26 +05:00
|
|
|
this.title = value;
|
|
|
|
|
}}
|
2020-12-17 15:42:55 +05:00
|
|
|
blurOnSubmit={false}
|
2021-02-20 14:50:27 +05:00
|
|
|
defaultValue={this.toEdit ? this.toEdit.title : null}
|
2020-09-27 13:05:26 +05:00
|
|
|
placeholder="Enter title of topic"
|
2021-01-08 13:14:37 +05:00
|
|
|
onSubmit={() => this.addNewTopic()}
|
2021-04-05 10:25:05 +05:00
|
|
|
returnKeyLabel="Done"
|
|
|
|
|
returnKeyType="done"
|
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
|
2021-02-20 14:50:27 +05:00
|
|
|
positiveTitle={this.toEdit ? 'Save' : 'Add'}
|
2021-01-08 13:10:25 +05:00
|
|
|
onPressNegative={() => this.close()}
|
2021-01-08 13:14:37 +05:00
|
|
|
onPressPositive={() => this.addNewTopic()}
|
2021-02-09 16:41:34 +05:00
|
|
|
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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|