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

282 lines
8.1 KiB
JavaScript
Raw Normal View History

2020-01-18 11:41:42 +05:00
import React, {Component} from 'react';
2020-01-22 02:50:25 +05:00
import {
Modal,
Text,
TouchableOpacity,
View,
DeviceEventEmitter,
} from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-01-22 02:50:25 +05:00
import {db, DDS} from '../../../App';
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-18 11:41:42 +05:00
import NavigationService from '../../services/NavigationService';
2020-03-02 10:25:38 +05:00
import {getElevation, ToastEvent, editing, history} from '../../utils/utils';
2020-01-18 11:41:42 +05:00
import {dialogActions, updateEvent} from '../DialogManager';
2020-01-25 23:24:01 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-03-03 14:45:49 +05:00
import {
eCloseFullscreenEditor,
eOnLoadNote,
eOnNewTopicAdded,
} from '../../services/events';
import {exitEditorAnimation} from '../../utils/animations';
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:45:49 +05:00
console.log(history.selectedItemsList.length, 'BREAK', item);
2020-03-02 10:25:38 +05:00
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);
2020-03-03 14:45:49 +05:00
eSendEvent(eOnNewTopicAdded);
2020-03-02 10:25:38 +05:00
updateEvent({type: 'notebook'});
2020-01-31 18:22:20 +05:00
2020-03-02 10:25:38 +05:00
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);
}
});
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-01-17 11:49:31 +05:00
this.setState({
visible: false,
});
if (editing.currentlyEditing) {
2020-03-03 14:45:49 +05:00
eSendEvent(eOnLoadNote, {type: 'new'});
2020-03-02 09:39:33 +05:00
if (DDS.isTab) {
eSendEvent(eCloseFullscreenEditor);
} else {
2020-03-03 14:45:49 +05:00
exitEditorAnimation();
2020-03-02 09:39:33 +05:00
}
}
2020-01-17 11:49:31 +05:00
break;
}
case dialogActions.ACTION_EXIT: {
2020-01-17 21:05:38 +05:00
this.setState({
visible: false,
});
NavigationService.goBack();
2020-01-18 00:45:37 +05:00
break;
}
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-02-02 19:09:33 +05:00
ToastEvent.show('Trash cleared', 'error', 1000, () => {}, '');
2020-01-18 00:45:37 +05:00
this.setState({
visible: false,
});
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-01-22 02:50:25 +05:00
this.setState({
visible: false,
});
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-02-02 19:09:33 +05:00
ToastEvent.show(
item.type.slice(0, 1).toUpperCase() +
item.type.slice(1) +
' restored',
'success',
3000,
);
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-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 = () => {
2020-03-02 10:25:38 +05:00
console.log(history.selectedItemsList.length, 'length');
2020-01-17 11:49:31 +05:00
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-02-02 20:44:17 +05:00
<TouchableOpacity
activeOpacity={opacity}
onPress={this._onClose}
2020-01-17 11:49:31 +05:00
style={{
2020-02-02 20:44:17 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '48%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.nav,
2020-01-17 11:49:31 +05:00
}}>
2020-02-02 20:44:17 +05:00
<Text
style={{
fontFamily: WEIGHT.medium,
color: colors.icon,
fontSize: SIZE.sm,
}}>
{negativeText}
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={opacity}
onPress={this._onPress}
2020-01-17 11:49:31 +05:00
style={{
2020-02-02 20:44:17 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '48%',
justifyContent: 'center',
alignItems: 'center',
borderColor: colors.accent,
backgroundColor: colors.accent,
borderWidth: 1,
2020-01-17 11:49:31 +05:00
}}>
2020-02-02 20:44:17 +05:00
<Text
style={{
fontFamily: WEIGHT.medium,
color: 'white',
fontSize: SIZE.sm,
}}>
{positiveText}
</Text>
</TouchableOpacity>
</View>
)}
2019-12-04 19:38:19 +05:00
</View>
</View>
2020-01-17 11:49:31 +05:00
</Modal>
);
}
}