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

133 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-06-16 08:16:31 +05:00
import React, { Component } from 'react';
import { View } from 'react-native';
2021-06-05 21:10:20 +05:00
import { useSelectionStore, useTrashStore } from '../../provider/stores';
2021-06-16 08:16:31 +05:00
import { DDS } from '../../services/DeviceDetection';
import { ToastEvent } from '../../services/EventManager';
import { getElevation, history } from '../../utils';
import { db } from '../../utils/DB';
2020-12-16 11:40:32 +05:00
import { deleteItems } from '../../utils/functions';
2021-06-16 08:16:31 +05:00
import { ph, pv } from '../../utils/SizeUtils';
import { dialogActions } from '../DialogManager/DialogActions';
2020-11-02 20:50:07 +05:00
import Seperator from '../Seperator';
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-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) {
2020-12-16 11:40:32 +05:00
case dialogActions.ACTION_DELETE:
deleteItems();
this.hide();
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);
2021-06-05 21:10:20 +05:00
useTrashStore.getState().setTrash();
useSelectionStore.getState().clearSelection();
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: 'Permanantly deleted items',
type: 'success',
context: 'local',
});
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();
2021-06-05 21:10:20 +05:00
useTrashStore.getState().setTrash();
useSelectionStore.getState().clearSelection();
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: 'Trash cleared',
message:"All notes and notebooks in the trash have been removed permanantly.",
type: 'success',
context: 'local',
});
2020-03-17 13:55:40 +05:00
this.hide();
2020-03-26 13:39:04 +05:00
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;
2020-11-23 12:32:33 +05:00
if (!visible) return null;
2020-01-17 11:49:31 +05:00
return (
2020-11-23 12:32:33 +05:00
<BaseDialog visible={true} 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-12-22 10:27:51 +05:00
width: DDS.isTab ? 350 : '85%',
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-11-02 20:50:07 +05:00
<Seperator />
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
);
}
}