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} from '../../utils/utils'; import {db} from '../../../App'; import {eSendEvent} from '../../services/eventManager'; import {eOnNewTopicAdded} from '../../services/events'; 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', 3000, () => {}, ''); await db.notebooks.notebook(this.props.notebookID).topics.add(this.title); this.close(); eSendEvent(eOnNewTopicAdded); ToastEvent.show('New topic added', 'success', 3000, () => {}, ''); }; 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} /> await this.addNewTopic()} style={{ paddingVertical: pv, paddingHorizontal: ph, borderRadius: 5, width: '45%', justifyContent: 'center', alignItems: 'center', borderColor: colors.accent, backgroundColor: colors.accent, borderWidth: 1, }}> Add this.close()} style={{ paddingVertical: pv, paddingHorizontal: ph, borderRadius: 5, width: '45%', justifyContent: 'center', alignItems: 'center', backgroundColor: colors.nav, }}> Cancel ); } }