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

194 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-01-16 19:52:45 +05:00
import React, {Component} from 'react';
import {DeviceEventEmitter} from 'react-native';
import {ActionSheetComponent} from '../ActionSheetComponent';
import ActionSheet from '../ActionSheet';
2020-01-17 11:49:31 +05:00
import {Dialog} from '../Dialog';
2020-01-16 19:52:45 +05:00
export const ActionSheetEvent = (item, colors, tags, rowItems, columnItems) => {
DeviceEventEmitter.emit('ActionSheetEvent', {
item,
colors,
tags,
rowItems,
columnItems,
});
};
export const ActionSheetHideEvent = () => {
DeviceEventEmitter.emit('ActionSheetHideEvent');
};
2020-01-17 11:49:31 +05:00
export const simpleDialogEvent = data => {
DeviceEventEmitter.emit('simpleDialogEvent', data);
};
export const simpleDialogHideEvent = () => {
DeviceEventEmitter.emit('simpleDialogHideEvent');
};
export const updateEvent = data => {
DeviceEventEmitter.emit('updateEvent', data);
};
2020-01-16 19:52:45 +05:00
export const _recieveEvent = (eventName, action) => {
DeviceEventEmitter.addListener(eventName, action);
};
export const _unSubscribeEvent = (eventName, action) => {
DeviceEventEmitter.removeListener(eventName, action);
};
2020-01-17 11:49:31 +05:00
export const TEMPLATE_DELETE = type => {
return {
title: `Delete ${type}`,
paragraph: `Are you sure you want to delete this ${type}`,
positiveText: 'Delete',
negativeText: 'Cancel',
action: dialogActions.ACTION_DELETE,
icon: 'trash',
};
};
export const dialogActions = {
ACTION_DELETE: 511,
ACTION_EXIT: 512,
};
2020-01-16 19:52:45 +05:00
export class DialogManager extends Component {
constructor(props) {
super(props);
this.actionSheet;
this.state = {
item: {},
actionSheetData: {
colors: false,
tags: false,
rowItems: [],
columnItems: [],
},
2020-01-17 11:49:31 +05:00
simpleDialog: {
title: '',
paragraph: '',
positiveText: '',
negativeText: '',
action: 0,
icon: '',
},
2020-01-16 19:52:45 +05:00
};
}
_showActionSheet = data => {
this.setState(
{
actionSheetData: data,
item: data.item,
},
() => {
this.actionSheet._setModalVisible();
},
);
};
_hideActionSheet = () => {
this.actionSheet._setModalVisible();
};
componentDidMount() {
_recieveEvent('ActionSheetEvent', this._showActionSheet);
_recieveEvent('ActionSheetHideEvent', this._hideActionSheet);
2020-01-17 11:49:31 +05:00
_recieveEvent('simpleDialogEvent', this._showSimpleDialog);
_recieveEvent('simpleDialogHideEvent', this._hideActionSheet);
2020-01-16 19:52:45 +05:00
}
componentWillUnmount() {
_unSubscribeEvent('ActionSheetEvent', this._showActionSheet);
2020-01-17 11:49:31 +05:00
_unSubscribeEvent('ActionSheetHideEvent', this._hideSimpleDialog);
_unSubscribeEvent('simpleDialogEvent', this._showSimpleDialog);
_unSubscribeEvent('simpleDialogHideEvent', this._hideSimpleDialog);
2020-01-16 19:52:45 +05:00
}
2020-01-17 11:49:31 +05:00
_showSimpleDialog = data => {
this.setState(
{
simpleDialog: data,
},
() => {
this.simpleDialog.show();
},
);
};
_hideSimpleDialog = data => {
this.simpleDialog.hide();
};
onActionSheetHide = () => {
if (this.show) {
if (this.show === 'delete') {
this._showSimpleDialog(TEMPLATE_DELETE(this.state.item.type));
this.show = null;
} else if (this.show == 'lock') {
this.setState({
vaultDialog: true,
});
this.show = null;
} else if (this.show == 'unlock') {
this.setState({
unlock: true,
isPerm: true,
vaultDialog: true,
});
this.show = null;
}
}
this.show = null;
};
2020-01-16 19:52:45 +05:00
render() {
let {colors, update} = this.props;
2020-01-17 11:49:31 +05:00
let {actionSheetData, item, simpleDialog} = this.state;
2020-01-16 19:52:45 +05:00
return (
<>
<ActionSheet
ref={ref => (this.actionSheet = ref)}
customStyles={{
backgroundColor: colors.bg,
}}
indicatorColor={colors.shade}
initialOffsetFromBottom={0.5}
bounceOnOpen={true}
gestureEnabled={true}
onClose={() => {
2020-01-17 11:49:31 +05:00
this.onActionSheetHide();
2020-01-16 19:52:45 +05:00
}}>
<ActionSheetComponent
item={item}
setWillRefresh={value => {
this.willRefresh = true;
}}
hasColors={actionSheetData.colors}
hasTags={actionSheetData.colors}
overlayColor={
colors.night ? 'rgba(225,225,225,0.1)' : 'rgba(0,0,0,0.3)'
}
rowItems={actionSheetData.rowItems}
columnItems={actionSheetData.columnItems}
close={value => {
if (value) {
this.show = value;
}
this.actionSheet._setModalVisible();
}}
/>
</ActionSheet>
2020-01-17 11:49:31 +05:00
<Dialog
ref={ref => (this.simpleDialog = ref)}
item={item}
colors={colors}
template={simpleDialog}
/>
2020-01-16 19:52:45 +05:00
</>
);
}
}