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

250 lines
7.6 KiB
JavaScript
Raw Normal View History

2020-01-18 11:41:42 +05:00
import React, {Component} from 'react';
2020-03-04 12:11:16 +05:00
import {Modal, Text, TouchableOpacity, View} from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-10-13 17:02:14 +05:00
import {Actions} from '../../provider/Actions';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
2020-03-03 14:45:49 +05:00
import {
2020-09-27 13:05:26 +05:00
eApplyChanges,
2020-03-21 10:03:11 +05:00
eClearEditor,
2020-03-03 14:45:49 +05:00
eCloseFullscreenEditor,
2020-09-27 13:05:26 +05:00
eOnLoadNote,
2020-03-03 14:45:49 +05:00
eOnNewTopicAdded,
2020-10-13 17:02:14 +05:00
} from '../../utils/Events';
import NavigationService from '../../services/Navigation';
import {getElevation, history} from '../../utils';
2020-09-27 13:05:26 +05:00
import {Button} from '../Button';
2020-10-13 17:02:14 +05:00
import {dialogActions} from '../DialogManager/DialogActions';
2020-03-21 10:03:11 +05:00
import {updateEvent} from '../DialogManager/recievers';
2020-09-27 13:05:26 +05:00
import BaseDialog from './base-dialog';
2020-09-27 13:14:24 +05:00
import DialogButtons from './dialog-buttons';
import DialogHeader from './dialog-header';
2020-10-13 17:02:14 +05:00
import {ph, pv, SIZE, WEIGHT} from "../../utils/SizeUtils";
import {db} from "../../utils/DB";
import {DDS} from "../../services/DeviceDetection";
2020-01-17 11:49:31 +05:00
export class Dialog extends Component {
constructor(props) {
super(props);
this.state = {
visible: false,
2020-03-02 10:25:38 +05:00
selecteItemsLength: 0,
2020-01-17 11:49:31 +05:00
};
}
_onPress = async () => {
let {template, item} = this.props;
switch (template.action) {
case dialogActions.ACTION_DELETE: {
2020-03-03 14:45:49 +05:00
if (item.dateCreated && history.selectedItemsList.length === 0) {
2020-03-02 10:25:38 +05:00
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
2020-03-03 14:55:44 +05:00
2020-09-10 13:16:30 +05:00
for (var i = 0; i < history.selectedItemsList.length; i++) {
let it = history.selectedItemsList[i];
if (it.type === 'note') {
await db.notes.delete(it.id);
updateEvent({type: it.type});
2020-03-19 11:34:47 +05:00
eSendEvent(eClearEditor);
2020-09-10 13:16:30 +05:00
} else if (it.type === 'topic') {
await db.notebooks.notebook(it.notebookId).topics.delete(it.title);
2020-03-03 14:45:49 +05:00
eSendEvent(eOnNewTopicAdded);
2020-03-02 10:25:38 +05:00
updateEvent({type: 'notebook'});
2020-09-10 13:16:30 +05:00
ToastEvent.show('Topics deleted', 'success');
} else if (it.type === 'notebook') {
await db.notebooks.delete(it.id);
updateEvent({type: it.type});
2020-03-02 10:25:38 +05:00
}
2020-09-10 13:16:30 +05:00
}
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.PINNED});
2020-09-10 13:16:30 +05:00
let message;
let notes = history.selectedItemsList.filter((o) => o.type === 'note');
let notebooks = history.selectedItemsList.filter(
(o) => o.type === 'notebook',
);
let topics = history.selectedItemsList.filter(
(o) => o.type === 'topic',
);
if (notes.length > 0 && notebooks.length === 0 && topics.length === 0) {
let msgPart = notes.length > 1 ? ' notes' : ' note';
message = notes.length + msgPart + ' moved to trash';
} else if (
notes.length === 0 &&
notebooks.length > 0 &&
topics.length === 0
) {
let msgPart = notebooks.length > 1 ? ' notebooks' : ' notebook';
message = notebooks.length + msgPart + ' moved to trash';
} else if (
notes.length === 0 &&
notebooks.length === 0 &&
topics.length > 0
) {
let msgPart = topics.length > 1 ? ' topics' : ' topic';
message = topics.length + msgPart + ' moved to trash';
}
let itemsCopy = [...history.selectedItemsList];
if (history.selectedItemsList[0].type !== 'topic')
ToastEvent.show(
message,
'success',
'global',
6000,
async () => {
let trash = db.trash;
for (var i = 0; i < itemsCopy.length; i++) {
let it = itemsCopy[i];
let trashItem = trash.all.find((item) => item.itemId === it.id);
await db.trash.restore(trashItem.id);
updateEvent({type: it.type});
}
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.TRASH});
updateEvent({type: Actions.PINNED});
2020-09-10 13:16:30 +05:00
ToastEvent.hide();
},
'Undo',
);
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
2020-01-17 21:05:38 +05:00
2020-03-17 13:55:40 +05:00
this.hide();
2020-01-17 11:49:31 +05:00
break;
}
2020-03-04 11:58:44 +05:00
case dialogActions.ACTION_PERMANANT_DELETE: {
if (item.dateCreated && history.selectedItemsList.length === 0) {
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
let ids = [];
2020-09-10 13:16:30 +05:00
history.selectedItemsList.forEach((item) => ids.push(item.id));
2020-03-04 11:58:44 +05:00
await db.trash.delete(...ids);
2020-03-17 13:55:40 +05:00
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.TRASH});
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
2020-03-04 11:58:44 +05:00
ToastEvent.show('Item permanantly deleted');
2020-03-17 13:55:40 +05:00
this.hide();
2020-03-04 11:58:44 +05:00
break;
}
2020-01-17 11:49:31 +05:00
case dialogActions.ACTION_EXIT: {
2020-01-17 21:05:38 +05:00
this.setState({
visible: false,
});
NavigationService.goBack();
2020-03-17 13:55:40 +05:00
this.hide();
2020-01-18 00:45:37 +05:00
break;
}
2020-09-15 11:24:43 +05:00
case dialogActions.ACTION_NEW_NOTE: {
2020-09-27 13:05:26 +05:00
eSendEvent(eOnLoadNote, {type: 'new'});
2020-09-15 11:24:43 +05:00
this.hide();
break;
}
2020-01-18 00:45:37 +05:00
case dialogActions.ACTION_EMPTY_TRASH: {
2020-02-06 21:33:18 +05:00
await db.trash.clear();
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.TRASH});
2020-03-04 11:58:44 +05:00
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
2020-03-04 11:58:44 +05:00
ToastEvent.show('Trash cleared', 'error');
2020-03-17 13:55:40 +05:00
this.hide();
2020-01-18 00:45:37 +05:00
2020-01-17 11:49:31 +05:00
break;
}
2020-01-22 02:50:25 +05:00
case dialogActions.ACTION_EXIT_FULLSCREEN: {
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.NOTES});
2020-01-25 23:24:01 +05:00
eSendEvent(eCloseFullscreenEditor);
2020-03-17 13:55:40 +05:00
this.hide();
2020-02-22 17:36:18 +05:00
break;
2020-01-22 02:50:25 +05:00
}
2020-02-02 16:18:52 +05:00
case dialogActions.ACTION_TRASH: {
2020-03-02 10:25:38 +05:00
await db.trash.restore(i.id);
2020-03-17 13:55:40 +05:00
this.hide();
2020-02-02 19:09:33 +05:00
ToastEvent.show(
item.type.slice(0, 1).toUpperCase() +
item.type.slice(1) +
' restored',
'success',
);
2020-03-02 10:25:38 +05:00
2020-10-13 17:02:14 +05:00
updateEvent({type: Actions.TRASH});
2020-02-02 16:18:52 +05:00
this.hide();
2020-02-22 17:36:18 +05:00
break;
2020-02-02 16:18:52 +05:00
}
2020-03-26 13:39:04 +05:00
case dialogActions.ACTION_APPLY_CHANGES: {
eSendEvent(eApplyChanges);
this.hide();
break;
}
2020-01-17 11:49:31 +05:00
}
};
_onClose = () => {
2020-02-02 16:18:52 +05:00
let {template, item} = this.props;
if (dialogActions.ACTION_TRASH === template.action) {
// delete item forever.
2020-02-06 21:33:18 +05:00
db.trash.delete(item.id);
2020-02-02 16:18:52 +05:00
}
2020-01-17 11:49:31 +05:00
this.setState({
visible: false,
});
};
2019-12-04 19:38:19 +05:00
2020-01-17 11:49:31 +05:00
show = () => {
this.setState({
visible: true,
2020-03-02 10:25:38 +05:00
selectedItemsLength: history.selectedItemsList.length,
2020-01-17 11:49:31 +05:00
});
};
hide = () => {
this.setState({
visible: false,
});
};
2019-12-04 19:38:19 +05:00
2020-01-17 11:49:31 +05:00
render() {
const {template, colors} = this.props;
const {title, paragraph, positiveText, negativeText, icon} = template;
const {visible} = this.state;
return (
2020-09-27 13:05:26 +05:00
<BaseDialog visible={visible} onRequestClose={this.hide}>
2020-01-17 11:49:31 +05:00
<View
2020-01-08 22:47:49 +05:00
style={{
2020-09-27 13:05:26 +05:00
...getElevation(5),
2020-10-01 17:22:44 +05:00
width: DDS.isTab ? 350 : '80%',
2020-09-27 13:05:26 +05:00
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-04 19:38:19 +05:00
}}>
2020-09-27 13:14:24 +05:00
<DialogHeader
title={title}
icon={icon}
paragraph={
this.state.selectedItemsLength > 0
2020-09-27 13:05:26 +05:00
? 'Delete ' +
this.state.selectedItemsLength +
' selected items?'
2020-09-27 13:14:24 +05:00
: paragraph
}
/>
2020-09-27 13:05:26 +05:00
{template.noButtons ? null : (
2020-09-27 13:14:24 +05:00
<DialogButtons
onPressNegative={this._onClose}
onPressPositive={this._onPress}
positiveTitle={positiveText}
negativeTitle={negativeText}
/>
2020-09-27 13:05:26 +05:00
)}
2019-12-04 19:38:19 +05:00
</View>
2020-09-27 13:05:26 +05:00
</BaseDialog>
2020-01-17 11:49:31 +05:00
);
}
}