Enhance synchronization logic and offline handling
- Refactor components to support conditional offline and online CRUD operations. - Introduce `addToQueue` mechanism for syncing offline changes to the server. - Add `isChapterContentExist` method and related existence checks in repositories. - Consolidate data structures and streamline book, chapter, character, and guideline synchronization workflows. - Encrypt additional character fields and adjust repository inserts for offline data.
This commit is contained in:
@@ -10,6 +10,9 @@ import CollapsableArea from "@/components/CollapsableArea";
|
||||
import {useTranslations} from "next-intl";
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from "@/context/SyncQueueContext";
|
||||
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||
|
||||
interface IssuesProps {
|
||||
issues: Issue[];
|
||||
@@ -20,6 +23,8 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const {book} = useContext(BookContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {errorMessage} = useContext(AlertContext);
|
||||
@@ -36,22 +41,23 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
}
|
||||
try {
|
||||
let issueId: string;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
issueId = await window.electron.invoke<string>('db:book:issue:add', {
|
||||
bookId,
|
||||
name: newIssueName,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
issueId = await window.electron.invoke<string>('db:book:issue:add', {
|
||||
issueId = await System.authPostToServer<string>('book/issue/add', {
|
||||
bookId,
|
||||
name: newIssueName,
|
||||
}, token, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
|
||||
addToQueue('db:book:issue:add', {
|
||||
bookId,
|
||||
issueId,
|
||||
name: newIssueName,
|
||||
});
|
||||
} else {
|
||||
issueId = await System.authPostToServer<string>('book/issue/add', {
|
||||
bookId,
|
||||
name: newIssueName,
|
||||
}, token, lang);
|
||||
}
|
||||
}
|
||||
if (!issueId) {
|
||||
@@ -62,7 +68,7 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
name: newIssueName,
|
||||
id: issueId,
|
||||
};
|
||||
|
||||
|
||||
setIssues([...issues, newIssue]);
|
||||
setNewIssueName('');
|
||||
} catch (e: unknown) {
|
||||
@@ -77,32 +83,32 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
async function deleteIssue(issueId: string): Promise<void> {
|
||||
if (issueId === undefined) {
|
||||
errorMessage(t("issues.errorInvalidId"));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:book:issue:remove', {
|
||||
bookId,
|
||||
issueId,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:book:issue:remove', {
|
||||
response = await System.authDeleteToServer<boolean>(
|
||||
'book/issue/remove',
|
||||
{
|
||||
bookId,
|
||||
issueId,
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
|
||||
addToQueue('db:book:issue:remove', {
|
||||
bookId,
|
||||
issueId,
|
||||
});
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(
|
||||
'book/issue/remove',
|
||||
{
|
||||
bookId,
|
||||
issueId,
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
}
|
||||
}
|
||||
if (response) {
|
||||
|
||||
Reference in New Issue
Block a user