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

298 lines
9.0 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-01-18 11:41:42 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-01-24 23:13:09 +05:00
import {ACTIONS} from '../../provider/actions';
2020-01-25 23:24:01 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-03-03 14:45:49 +05:00
import {
2020-03-21 10:03:11 +05:00
eClearEditor,
2020-03-03 14:45:49 +05:00
eCloseFullscreenEditor,
eOnNewTopicAdded,
2020-09-15 11:24:43 +05:00
eApplyChanges, eOnLoadNote
2020-03-03 14:45:49 +05:00
} from '../../services/events';
2020-03-04 12:11:16 +05:00
import NavigationService from '../../services/NavigationService';
2020-03-21 10:03:11 +05:00
import {db, DDS, getElevation, history, ToastEvent} from '../../utils/utils';
2020-03-14 13:37:07 +05:00
import {dialogActions} from '../DialogManager/dialogActions';
2020-03-21 10:03:11 +05:00
import {updateEvent} from '../DialogManager/recievers';
2020-09-10 13:16:30 +05:00
import {Button} from '../Button';
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
}
updateEvent({type: ACTIONS.PINNED});
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});
}
updateEvent({type: ACTIONS.TRASH});
updateEvent({type: ACTIONS.PINNED});
ToastEvent.hide();
},
'Undo',
);
2020-03-02 10:25:38 +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-03-04 11:58:44 +05:00
updateEvent({type: ACTIONS.TRASH});
updateEvent({type: ACTIONS.CLEAR_SELECTION});
updateEvent({type: ACTIONS.SELECTION_MODE, enabled: false});
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: {
eSendEvent(eOnLoadNote, {type:"new"});
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-01-18 00:45:37 +05:00
updateEvent({type: ACTIONS.TRASH});
2020-03-04 11:58:44 +05:00
updateEvent({type: ACTIONS.CLEAR_SELECTION});
updateEvent({type: ACTIONS.SELECTION_MODE, enabled: false});
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: {
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-02-02 16:18:52 +05:00
updateEvent({type: ACTIONS.TRASH});
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 (
<Modal
visible={visible}
transparent={true}
animated
animationType="fade"
onRequestClose={() => this.setState({visible: false})}>
<View
2020-01-08 22:47:49 +05:00
style={{
width: '100%',
height: '100%',
2020-01-17 11:49:31 +05:00
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
2019-12-04 19:38:19 +05:00
}}>
2020-01-17 11:49:31 +05:00
<TouchableOpacity
2020-01-22 02:50:25 +05:00
onPress={this.hide}
2019-12-04 19:38:19 +05:00
style={{
2020-01-17 11:49:31 +05:00
width: '100%',
height: '100%',
position: 'absolute',
}}
/>
2019-12-04 19:38:19 +05:00
<View
style={{
2020-01-17 11:49:31 +05:00
...getElevation(5),
2020-02-22 17:36:18 +05:00
width: DDS.isTab ? '40%' : '80%',
2020-01-17 11:49:31 +05:00
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-04 19:38:19 +05:00
}}>
2020-01-17 11:49:31 +05:00
<View
2019-12-04 19:38:19 +05:00
style={{
2020-01-17 11:49:31 +05:00
flexDirection: 'row',
2019-12-04 19:38:19 +05:00
justifyContent: 'center',
alignItems: 'center',
}}>
2020-01-17 11:49:31 +05:00
{icon ? (
<Icon name={icon} color={colors.accent} size={SIZE.lg} />
) : null}
2020-02-02 20:44:17 +05:00
{template.noTitle ? null : (
<Text
style={{
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
}}>
{title}
</Text>
)}
2020-01-17 11:49:31 +05:00
</View>
2019-12-04 19:38:19 +05:00
2020-01-17 11:49:31 +05:00
{paragraph ? (
2019-12-04 19:38:19 +05:00
<Text
style={{
color: colors.icon,
2020-01-17 11:49:31 +05:00
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
textAlign: 'center',
marginTop: 10,
2019-12-04 19:38:19 +05:00
}}>
2020-03-02 10:25:38 +05:00
{this.state.selectedItemsLength > 0
? 'Delete ' +
this.state.selectedItemsLength +
' selected items?'
: paragraph}
2019-12-04 19:38:19 +05:00
</Text>
2020-01-17 11:49:31 +05:00
) : null}
2020-02-02 20:44:17 +05:00
{template.noButtons ? null : (
<View
2020-01-17 11:49:31 +05:00
style={{
2020-02-02 20:44:17 +05:00
justifyContent: 'space-between',
2020-01-17 11:49:31 +05:00
alignItems: 'center',
2020-02-02 20:44:17 +05:00
flexDirection: 'row',
marginTop: 20,
2020-01-17 11:49:31 +05:00
}}>
2020-09-10 13:16:30 +05:00
<Button onPress={this._onClose} title={negativeText} grayed />
<Button onPress={this._onPress} title={positiveText} />
2020-02-02 20:44:17 +05:00
</View>
)}
2019-12-04 19:38:19 +05:00
</View>
</View>
2020-01-17 11:49:31 +05:00
</Modal>
);
}
}