import React, {createRef} from 'react'; import {Actions} from '../../provider/Actions'; import { eSendEvent, eSubscribeEvent, eUnSubscribeEvent, ToastEvent, } from '../../services/EventManager'; import {db} from '../../utils/DB'; import { eCloseAddTopicDialog, eOnNewTopicAdded, eOpenAddTopicDialog, } from '../../utils/Events'; import BaseDialog from '../Dialog/base-dialog'; import DialogButtons from '../Dialog/dialog-buttons'; import DialogContainer from '../Dialog/dialog-container'; import DialogHeader from '../Dialog/dialog-header'; import {updateEvent} from '../DialogManager/recievers'; import Input from '../Input'; import {Toast} from '../Toast'; export class AddTopicDialog extends React.Component { constructor(props) { super(props); this.state = { visible: false, titleFocused: false, loading:false }; this.title; this.titleRef = createRef(); this.notebook = null; } addNewTopic = async () => { this.setState({loading:true}) if (!this.title) return ToastEvent.show('Title is required', 'error', 'local'); if (!this.props.toEdit) { await db.notebooks.notebook(this.notebook.id).topics.add(this.title); } else { let topic = this.props.toEdit; topic.title = this.title; await db.notebooks.notebook(topic.notebookId).topics.add(topic); } this.close(); updateEvent({type: Actions.NOTEBOOKS}); eSendEvent(eOnNewTopicAdded); }; 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; this.notebook = await db.notebooks.notebook(id).data; if (this.props.toEdit) { this.title = this.props.toEdit.title; } this.setState({ visible: true, }); }; close = () => { this.props.close(); this.title = null; this.notebook = null; this.setState({ visible: false, }); }; render() { const {visible} = this.state; const {colors, toEdit} = this.props; if (!visible) return null; return ( { this.titleRef.current?.focus(); }} statusBarTranslucent={false} visible={true} onRequestClose={this.close}> { this.title = value; }} blurOnSubmit={false} defaultValue={toEdit ? toEdit.title : null} placeholder="Enter title of topic" onSubmit={() => this.addNewTopic()} /> this.close()} onPressPositive={() => this.addNewTopic()} loading={this.state.loading} /> ); } }