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

147 lines
3.9 KiB
JavaScript
Raw Normal View History

import React, {createRef} from 'react';
import { Actions } from '../../provider/Actions';
2020-12-17 10:31:35 +05:00
import {
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent,
2020-12-17 10:31:35 +05:00
} from '../../services/EventManager';
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';
import { updateEvent } from '../DialogManager/recievers';
2020-12-17 15:42:55 +05:00
import Input from '../Input';
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;
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-02-16 16:11:10 +05:00
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
if (!this.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 {
let topic = this.toEdit;
2020-05-12 14:09:56 +05:00
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
}
this.setState({loading: false});
2020-02-03 00:18:40 +05:00
this.close();
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notebook,
Navigation.routeNames.NotesPage,
2021-02-16 16:11:10 +05:00
]);
updateEvent({type:Actions.MENU_PINS});
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, toEdit}) => {
let id = notebookId;
if (id) {
this.notebook = await db.notebooks.notebook(id).data;
}
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;
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
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"
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}
2020-09-27 13:05:26 +05:00
onChangeText={(value) => {
this.title = value;
}}
2020-12-17 15:42:55 +05:00
blurOnSubmit={false}
defaultValue={this.toEdit ? this.toEdit.title : null}
2020-09-27 13:05:26 +05:00
placeholder="Enter title of topic"
onSubmit={() => this.addNewTopic()}
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
positiveTitle={this.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
);
}
}