check if note content is synced before taking action

This commit is contained in:
ammarahm-ed
2022-04-01 01:00:27 +05:00
parent fb86b1f4a9
commit 9814f17d6b

View File

@@ -58,13 +58,6 @@ export const useActions = ({ close = () => {}, item }) => {
: item.title;
const isPublished = item.type === 'note' && db.monographs.isPublished(item.id);
const noteInTopic =
item.type === 'note' &&
editing.actionAfterFirstSave.type === 'topic' &&
db.notebooks
.notebook(editing.actionAfterFirstSave.notebook)
.topics.topic(editing.actionAfterFirstSave.id)
.has(item.id);
useEffect(() => {
if (item.id === null) return;
@@ -89,6 +82,16 @@ export const useActions = ({ close = () => {}, item }) => {
}
}
const isNoteInTopic = () => {
if (item.type !== 'note' || editing.actionAfterFirstSave?.type !== 'topic') return;
console.log(editing.actionAfterFirstSave, 'save item');
return db.notebooks
.notebook(editing.actionAfterFirstSave.notebook)
.topics.topic(editing.actionAfterFirstSave.id)
.has(item.id);
};
const onUpdate = async type => {
if (type === 'unpin') {
await sleep(1000);
@@ -145,6 +148,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function pinToNotifications() {
if (!checkNoteSynced()) return;
console.log('pinToNotifications');
if (Platform.OS === 'ios') return;
@@ -173,6 +177,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function restoreTrashItem() {
if (!checkNoteSynced()) return;
close();
await db.trash.restore(item.id);
Navigation.setRoutesToUpdate([
@@ -191,6 +196,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function copyContent() {
if (!checkNoteSynced()) return;
if (item.locked) {
close();
await sleep(300);
@@ -213,6 +219,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function publishNote() {
if (!checkNoteSynced()) return;
if (!user) {
ToastEvent.show({
heading: 'Login required',
@@ -247,8 +254,22 @@ export const useActions = ({ close = () => {}, item }) => {
eSendEvent(eOpenPublishNoteDialog, item);
}
const checkNoteSynced = () => {
if (!db.notes.note(item.id).synced()) {
ToastEvent.show({
context: 'local',
heading: 'Note not synced',
message: 'Please run sync before making changes',
type: 'error'
});
return false;
}
return true;
};
async function addToVault() {
if (!item.id) return;
if (!checkNoteSynced()) return;
if (item.locked) {
close();
await sleep(300);
@@ -343,6 +364,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function shareNote() {
if (!checkNoteSynced()) return;
if (item.locked) {
close();
await sleep(300);
@@ -364,6 +386,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function deleteItem() {
if (!checkNoteSynced()) return;
close();
if (item.type === 'tag') {
await sleep(300);
@@ -417,6 +440,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function deleteTrashItem() {
if (!checkNoteSynced()) return;
close();
await sleep(300);
presentDialog({
@@ -459,6 +483,7 @@ export const useActions = ({ close = () => {}, item }) => {
}
async function toggleLocalOnly() {
if (!checkNoteSynced()) return;
db.notes.note(item.id).localOnly();
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
@@ -484,6 +509,7 @@ export const useActions = ({ close = () => {}, item }) => {
};
const duplicateNote = async () => {
if (!checkNoteSynced()) return;
await db.notes.note(item.id).duplicate();
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
@@ -631,7 +657,7 @@ export const useActions = ({ close = () => {}, item }) => {
{
name: 'RemoveTopic',
title: 'Remove from topic',
hidden: !noteInTopic,
hidden: !isNoteInTopic(),
icon: 'minus-circle-outline',
func: removeNoteFromTopic
},