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; : item.title;
const isPublished = item.type === 'note' && db.monographs.isPublished(item.id); 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(() => { useEffect(() => {
if (item.id === null) return; 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 => { const onUpdate = async type => {
if (type === 'unpin') { if (type === 'unpin') {
await sleep(1000); await sleep(1000);
@@ -145,6 +148,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function pinToNotifications() { async function pinToNotifications() {
if (!checkNoteSynced()) return;
console.log('pinToNotifications'); console.log('pinToNotifications');
if (Platform.OS === 'ios') return; if (Platform.OS === 'ios') return;
@@ -173,6 +177,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function restoreTrashItem() { async function restoreTrashItem() {
if (!checkNoteSynced()) return;
close(); close();
await db.trash.restore(item.id); await db.trash.restore(item.id);
Navigation.setRoutesToUpdate([ Navigation.setRoutesToUpdate([
@@ -191,6 +196,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function copyContent() { async function copyContent() {
if (!checkNoteSynced()) return;
if (item.locked) { if (item.locked) {
close(); close();
await sleep(300); await sleep(300);
@@ -213,6 +219,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function publishNote() { async function publishNote() {
if (!checkNoteSynced()) return;
if (!user) { if (!user) {
ToastEvent.show({ ToastEvent.show({
heading: 'Login required', heading: 'Login required',
@@ -247,8 +254,22 @@ export const useActions = ({ close = () => {}, item }) => {
eSendEvent(eOpenPublishNoteDialog, 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() { async function addToVault() {
if (!item.id) return; if (!item.id) return;
if (!checkNoteSynced()) return;
if (item.locked) { if (item.locked) {
close(); close();
await sleep(300); await sleep(300);
@@ -343,6 +364,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function shareNote() { async function shareNote() {
if (!checkNoteSynced()) return;
if (item.locked) { if (item.locked) {
close(); close();
await sleep(300); await sleep(300);
@@ -364,6 +386,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function deleteItem() { async function deleteItem() {
if (!checkNoteSynced()) return;
close(); close();
if (item.type === 'tag') { if (item.type === 'tag') {
await sleep(300); await sleep(300);
@@ -417,6 +440,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function deleteTrashItem() { async function deleteTrashItem() {
if (!checkNoteSynced()) return;
close(); close();
await sleep(300); await sleep(300);
presentDialog({ presentDialog({
@@ -459,6 +483,7 @@ export const useActions = ({ close = () => {}, item }) => {
} }
async function toggleLocalOnly() { async function toggleLocalOnly() {
if (!checkNoteSynced()) return;
db.notes.note(item.id).localOnly(); db.notes.note(item.id).localOnly();
Navigation.setRoutesToUpdate([ Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage, Navigation.routeNames.NotesPage,
@@ -484,6 +509,7 @@ export const useActions = ({ close = () => {}, item }) => {
}; };
const duplicateNote = async () => { const duplicateNote = async () => {
if (!checkNoteSynced()) return;
await db.notes.note(item.id).duplicate(); await db.notes.note(item.id).duplicate();
Navigation.setRoutesToUpdate([ Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage, Navigation.routeNames.NotesPage,
@@ -631,7 +657,7 @@ export const useActions = ({ close = () => {}, item }) => {
{ {
name: 'RemoveTopic', name: 'RemoveTopic',
title: 'Remove from topic', title: 'Remove from topic',
hidden: !noteInTopic, hidden: !isNoteInTopic(),
icon: 'minus-circle-outline', icon: 'minus-circle-outline',
func: removeNoteFromTopic func: removeNoteFromTopic
}, },