import React, {Component} from 'react'; import { Modal, Text, TouchableOpacity, View, DeviceEventEmitter, } from 'react-native'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import {db, DDS} from '../../../App'; import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common'; import {ACTIONS} from '../../provider/actions'; import NavigationService from '../../services/NavigationService'; import {getElevation, ToastEvent, editing, history} from '../../utils/utils'; import {dialogActions, updateEvent} from '../DialogManager'; import {eSendEvent} from '../../services/eventManager'; import {eCloseFullscreenEditor, eOnLoadNote} from '../../services/events'; export class Dialog extends Component { constructor(props) { super(props); this.state = { visible: false, selecteItemsLength: 0, }; } _onPress = async () => { let {template, item} = this.props; switch (template.action) { case dialogActions.ACTION_DELETE: { if (item.id && history.selectedItemsList.length === 0) { history.selectedItemsList = []; history.selectedItemsList.push(item); } history.selectedItemsList.forEach(async i => { if (i.type === 'note') { await db.notes.delete(i.id); ToastEvent.show('Notes moved to trash', 'error', 3000); updateEvent({type: i.type}); } else if (i.type === 'topic') { await db.notebooks.notebook(i.notebookId).topics.delete(i.title); updateEvent({type: 'notebook'}); ToastEvent.show('Topics deleted', 'error', 3000); } else if (i.type === 'notebook') { await db.notebooks.delete(i.id); updateEvent({type: i.type}); ToastEvent.show('Notebooks moved to trash', 'error', 3000); } }); updateEvent({type: ACTIONS.CLEAR_SELECTION}); updateEvent({type: ACTIONS.SELECTION_MODE, enabled: false}); this.setState({ visible: false, }); if (editing.currentlyEditing) { if (DDS.isTab) { eSendEvent(eCloseFullscreenEditor); eSendEvent(eOnLoadNote, {type: 'new'}); } else { NavigationService.goBack(); } } break; } case dialogActions.ACTION_EXIT: { this.setState({ visible: false, }); NavigationService.goBack(); break; } case dialogActions.ACTION_EMPTY_TRASH: { await db.trash.clear(); updateEvent({type: ACTIONS.TRASH}); ToastEvent.show('Trash cleared', 'error', 1000, () => {}, ''); this.setState({ visible: false, }); break; } case dialogActions.ACTION_EXIT_FULLSCREEN: { updateEvent({type: ACTIONS.NOTES}); eSendEvent(eCloseFullscreenEditor); this.setState({ visible: false, }); break; } case dialogActions.ACTION_TRASH: { await db.trash.restore(i.id); ToastEvent.show( item.type.slice(0, 1).toUpperCase() + item.type.slice(1) + ' restored', 'success', 3000, ); updateEvent({type: ACTIONS.TRASH}); this.hide(); break; } } }; _onClose = () => { let {template, item} = this.props; if (dialogActions.ACTION_TRASH === template.action) { // delete item forever. db.trash.delete(item.id); } this.setState({ visible: false, }); }; show = () => { console.log(history.selectedItemsList.length, 'length'); this.setState({ visible: true, selectedItemsLength: history.selectedItemsList.length, }); }; hide = () => { this.setState({ visible: false, }); }; render() { const {template, colors} = this.props; const {title, paragraph, positiveText, negativeText, icon} = template; const {visible} = this.state; return ( this.setState({visible: false})}> {icon ? ( ) : null} {template.noTitle ? null : ( {title} )} {paragraph ? ( {this.state.selectedItemsLength > 0 ? 'Delete ' + this.state.selectedItemsLength + ' selected items?' : paragraph} ) : null} {template.noButtons ? null : ( {negativeText} {positiveText} )} ); } }