Skip to content

Commit

Permalink
fix: should sync added to remove sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Palanikannan1437 committed Nov 26, 2024
1 parent 1a66f3c commit 5958a82
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const DocumentCollaborativeEvents = {
Lock: { client: "locked", server: "Lock" },
Unlock: { client: "unlocked", server: "Unlock" },
Archive: { client: "archived", server: "Archive" },
Unarchive: { client: "unarchived", server: "Unarchive" },
lock: { client: "locked", server: "lock" },
unlock: { client: "unlocked", server: "unlock" },
archive: { client: "archived", server: "archive" },
unarchive: { client: "unarchived", server: "unarchive" },
} as const;
8 changes: 4 additions & 4 deletions web/core/components/pages/editor/header/options-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ export const PageOptionsDropdown: React.FC<Props> = observer((props) => {
{
key: "lock-unlock-page",
action: is_locked
? () => executeCollaborativeAction({ type: "sendMessageToServer", message: "Unlock" })
: () => executeCollaborativeAction({ type: "sendMessageToServer", message: "Lock" }),
? () => executeCollaborativeAction({ type: "sendMessageToServer", message: "unlock" })
: () => executeCollaborativeAction({ type: "sendMessageToServer", message: "lock" }),
label: is_locked ? "Unlock page" : "Lock page",
icon: is_locked ? LockOpen : Lock,
shouldRender: canCurrentUserLockPage,
},
{
key: "archive-restore-page",
action: archived_at
? () => executeCollaborativeAction({ type: "sendMessageToServer", message: "Unarchive" })
: () => executeCollaborativeAction({ type: "sendMessageToServer", message: "Archive" }),
? () => executeCollaborativeAction({ type: "sendMessageToServer", message: "unarchive" })
: () => executeCollaborativeAction({ type: "sendMessageToServer", message: "archive" }),
label: archived_at ? "Restore page" : "Archive page",
icon: archived_at ? ArchiveRestoreIcon : ArchiveIcon,
shouldRender: canCurrentUserArchivePage,
Expand Down
22 changes: 11 additions & 11 deletions web/core/hooks/use-collaborative-page-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IPage } from "@/store/pages/page";

// Better type naming and structure
type CollaborativeAction = {
execute: () => Promise<void>;
execute: (shouldSync?: boolean) => Promise<void>;
errorMessage: string;
};

Expand All @@ -21,24 +21,24 @@ export const useCollaborativePageActions = (editorRef: EditorRefApi | EditorRead

const actionHandlerMap: Record<TDocumentEventsClient, CollaborativeAction> = useMemo(
() => ({
[DocumentCollaborativeEvents.Lock.client]: {
execute: page.lock,
[DocumentCollaborativeEvents.lock.client]: {
execute: (shouldSync) => page.lock(shouldSync),
errorMessage: "Page could not be locked. Please try again later.",
},
[DocumentCollaborativeEvents.Unlock.client]: {
execute: page.unlock,
[DocumentCollaborativeEvents.unlock.client]: {
execute: (shouldSync) => page.unlock(shouldSync),
errorMessage: "Page could not be unlocked. Please try again later.",
},
[DocumentCollaborativeEvents.Archive.client]: {
execute: page.archive,
[DocumentCollaborativeEvents.archive.client]: {
execute: (shouldSync) => page.archive(shouldSync),
errorMessage: "Page could not be archived. Please try again later.",
},
[DocumentCollaborativeEvents.Unarchive.client]: {
execute: page.restore,
[DocumentCollaborativeEvents.unarchive.client]: {
execute: (shouldSync) => page.restore(shouldSync),
errorMessage: "Page could not be restored. Please try again later.",
},
}),
[page.lock, page.unlock, page.archive, page.restore]
[page]
);

const executeCollaborativeAction = useCallback(
Expand All @@ -48,7 +48,7 @@ export const useCollaborativePageActions = (editorRef: EditorRefApi | EditorRead
const actionDetails = actionHandlerMap[clientAction];

try {
await actionDetails.execute();
await actionDetails.execute(isPerformedByCurrentUser);
if (isPerformedByCurrentUser) {
setCurrentActionBeingProcessed(clientAction);
}
Expand Down
86 changes: 59 additions & 27 deletions web/core/store/pages/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export interface IPage extends TPage {
updateDescription: (document: TDocumentPayload) => Promise<void>;
makePublic: () => Promise<void>;
makePrivate: () => Promise<void>;
lock: () => Promise<void>;
unlock: () => Promise<void>;
archive: () => Promise<void>;
restore: () => Promise<void>;
lock: (shouldSync?: boolean) => Promise<void>;
unlock: (shouldSync?: boolean) => Promise<void>;
archive: (shouldSync?: boolean) => Promise<void>;
restore: (shouldSync?: boolean) => Promise<void>;
updatePageLogo: (logo_props: TLogoProps) => Promise<void>;
addToFavorites: () => Promise<void>;
removePageFromFavorites: () => Promise<void>;
Expand Down Expand Up @@ -435,62 +435,94 @@ export class Page implements IPage {
/**
* @description lock the page
*/
lock = async () => {
lock = async (shouldSync: boolean = true) => {
const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !this.id) return undefined;

const pageIsLocked = this.is_locked;
runInAction(() => (this.is_locked = true));

await this.pageService.lock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => {
this.is_locked = pageIsLocked;
if (shouldSync) {
await this.pageService.lock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => {
this.is_locked = pageIsLocked;
});
throw error;
});
throw error;
});
}
};

/**
* @description unlock the page
*/
unlock = async () => {
unlock = async (shouldSync: boolean = true) => {
const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !this.id) return undefined;

const pageIsLocked = this.is_locked;
runInAction(() => (this.is_locked = false));

await this.pageService.unlock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => {
this.is_locked = pageIsLocked;
if (shouldSync) {
await this.pageService.unlock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => {
this.is_locked = pageIsLocked;
});
throw error;
});
throw error;
});
}
};

/**
* @description archive the page
*/
archive = async () => {
archive = async (shouldSync: boolean = true) => {
const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !this.id) return undefined;
const response = await this.pageService.archive(workspaceSlug, projectId, this.id);
runInAction(() => {
this.archived_at = response.archived_at;
});
if (this.rootStore.favorite.entityMap[this.id]) this.rootStore.favorite.removeFavoriteFromStore(this.id);

try {
runInAction(() => {
this.archived_at = new Date().toISOString();
});

if (this.rootStore.favorite.entityMap[this.id]) this.rootStore.favorite.removeFavoriteFromStore(this.id);

if (shouldSync) {
const response = await this.pageService.archive(workspaceSlug, projectId, this.id);
runInAction(() => {
this.archived_at = response.archived_at;
});
}
} catch (error) {
console.error(error);
runInAction(() => {
this.archived_at = null;
});
}
};

/**
* @description restore the page
*/
restore = async () => {
restore = async (shouldSync: boolean = true) => {
const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !this.id) return undefined;
await this.pageService.restore(workspaceSlug, projectId, this.id);
runInAction(() => {
this.archived_at = null;
});

const archivedAtBeforeRestore = this.archived_at;

try {
runInAction(() => {
this.archived_at = null;
});

if (shouldSync) {
await this.pageService.restore(workspaceSlug, projectId, this.id);
}
} catch (error) {
console.error(error);
runInAction(() => {
this.archived_at = archivedAtBeforeRestore;
});
}
};

updatePageLogo = async (logo_props: TLogoProps) => {
Expand Down

0 comments on commit 5958a82

Please sign in to comment.