import React, {createRef} from 'react'; import {Modal, Text, TouchableOpacity, View} from 'react-native'; import {TextInput} from 'react-native-gesture-handler'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common'; import {getElevation, ToastEvent, db} from '../../utils/utils'; import {eSendEvent} from '../../services/eventManager'; import {eOnNewTopicAdded} from '../../services/events'; import {Toast} from '../Toast'; import {Button} from '../Button'; export class AddTopicDialog extends React.Component { constructor(props) { super(props); this.state = { visible: false, titleFocused: false, }; this.title; this.titleRef = createRef(); } addNewTopic = async () => { if (!this.title) return ToastEvent.show('Title is required', 'error', 'local'); if (!this.props.toEdit) { await db.notebooks.notebook(this.props.notebookID).topics.add(this.title); } else { let topic = this.props.toEdit; topic.title = this.title; await db.notebooks.notebook(this.props.notebookID).topics.add(topic); } this.close(); ToastEvent.show('New topic added', 'success'); eSendEvent(eOnNewTopicAdded); }; open() { this.setState({ visible: true, }); } close() { this.title = null; this.setState({ visible: false, }); } render() { const {visible, titleFocused} = this.state; const {colors, toEdit} = this.props; return ( { this.titleRef.current?.focus(); }} onRequestClose={() => { refs = []; this.close(); }}> this.close()} style={{ width: '100%', height: '100%', position: 'absolute', }} /> {toEdit ? 'Edit Topic' : 'Add New Topic'} { 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} />