Files
notesnook/apps/mobile/src/utils/functions.js

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-11-14 12:25:57 +05:00
import {history} from '.';
import {updateEvent} from '../components/DialogManager/recievers';
import {Actions} from '../provider/Actions';
import {eSendEvent, ToastEvent} from '../services/EventManager';
import {db} from './DB';
2020-12-16 12:29:36 +05:00
import {eClearEditor, eOnNewTopicAdded, refreshNotesPage} from './Events';
2020-11-14 10:02:38 +05:00
2020-12-31 14:18:38 +05:00
export const deleteItems = async (item) => {
2020-12-31 19:54:08 +05:00
if (item && item.id && history.selectedItemsList.length === 0) {
2020-11-14 12:25:57 +05:00
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
2020-11-14 10:02:38 +05:00
2020-12-16 12:29:36 +05:00
let notes = history.selectedItemsList.filter((i) => i.type === 'note');
let notebooks = history.selectedItemsList.filter(
(i) => i.type === 'notebook',
);
let topics = history.selectedItemsList.filter((i) => i.type === 'topic');
if (notes?.length > 0) {
let ids = notes.map((i) => i.id);
2020-12-31 14:18:38 +05:00
await db.notes.delete(...ids);
2020-12-16 12:29:36 +05:00
updateEvent({type: Actions.NOTES});
eSendEvent(eClearEditor);
eSendEvent(refreshNotesPage);
}
if (topics?.length > 0) {
for (var i = 0; i < topics.length; i++) {
let it = topics[i];
await db.notebooks.notebook(it.notebookId).topics.delete(it.id);
2020-11-14 12:25:57 +05:00
}
2020-12-16 12:29:36 +05:00
updateEvent({type: Actions.NOTEBOOKS});
eSendEvent(eOnNewTopicAdded);
ToastEvent.show('Topics deleted', 'success');
}
if (notebooks?.length > 0) {
let ids = notebooks.map((i) => i.id);
2020-12-31 14:18:38 +05:00
await db.notebooks.delete(...ids);
2020-12-16 12:29:36 +05:00
updateEvent({type: Actions.NOTEBOOKS});
updateEvent({type: Actions.NOTES});
2020-11-14 12:25:57 +05:00
}
2020-11-14 10:02:38 +05:00
2020-11-14 12:25:57 +05:00
let msgPart = history.selectedItemsList.length === 1 ? ' item' : ' items';
let message = history.selectedItemsList.length + msgPart + ' moved to trash.';
2020-11-14 10:02:38 +05:00
2020-11-14 12:25:57 +05:00
let itemsCopy = [...history.selectedItemsList];
if (history.selectedItemsList[0].type !== 'topic') {
ToastEvent.show(
message,
2020-12-16 11:35:40 +05:00
'error',
2020-11-14 12:25:57 +05:00
'global',
6000,
async () => {
let trash = db.trash;
2020-12-16 12:29:36 +05:00
let ids = [];
2020-11-14 12:25:57 +05:00
for (var i = 0; i < itemsCopy.length; i++) {
let it = itemsCopy[i];
let trashItem = trash.all.find((item) => item.itemId === it.id);
2020-12-16 12:29:36 +05:00
ids.push(trashItem.id);
2020-11-14 12:25:57 +05:00
}
2020-12-31 14:18:58 +05:00
await db.trash.restore(...ids);
2020-12-16 12:29:36 +05:00
updateEvent({type: Actions.NOTEBOOKS});
updateEvent({type: Actions.NOTES});
2020-11-14 12:25:57 +05:00
updateEvent({type: Actions.TRASH});
ToastEvent.hide();
},
'Undo',
);
}
2020-12-16 12:29:36 +05:00
2020-12-16 11:35:40 +05:00
updateEvent({type: Actions.TRASH});
2020-11-14 12:25:57 +05:00
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
}