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 CharacterDetail from "@/components/book/settings/characters/CharacterDeta
|
||||
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 CharacterDetailProps {
|
||||
selectedCharacter: CharacterProps | null;
|
||||
@@ -48,6 +51,8 @@ export function CharacterComponent(props: any, ref: any) {
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {book} = useContext(BookContext);
|
||||
const {errorMessage, successMessage} = useContext(AlertContext);
|
||||
@@ -120,22 +125,23 @@ export function CharacterComponent(props: any, ref: any) {
|
||||
}
|
||||
try {
|
||||
let characterId: string;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
characterId = await window.electron.invoke<string>('db:character:create', {
|
||||
bookId: book?.bookId,
|
||||
character: updatedCharacter,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
characterId = await window.electron.invoke<string>('db:character:create', {
|
||||
characterId = await System.authPostToServer<string>(`character/add`, {
|
||||
bookId: book?.bookId,
|
||||
character: updatedCharacter,
|
||||
}, session.accessToken, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
|
||||
addToQueue('db:character:create', {
|
||||
bookId: book?.bookId,
|
||||
characterId,
|
||||
character: updatedCharacter,
|
||||
});
|
||||
} else {
|
||||
characterId = await System.authPostToServer<string>(`character/add`, {
|
||||
bookId: book?.bookId,
|
||||
character: updatedCharacter,
|
||||
}, session.accessToken, lang);
|
||||
}
|
||||
}
|
||||
if (!characterId) {
|
||||
@@ -157,19 +163,19 @@ export function CharacterComponent(props: any, ref: any) {
|
||||
async function updateCharacter(updatedCharacter: CharacterProps,): Promise<void> {
|
||||
try {
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:character:update', {
|
||||
character: updatedCharacter,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:character:update', {
|
||||
response = await System.authPostToServer<boolean>(`character/update`, {
|
||||
character: updatedCharacter,
|
||||
}, session.accessToken, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
|
||||
addToQueue('db:character:update', {
|
||||
character: updatedCharacter,
|
||||
});
|
||||
} else {
|
||||
response = await System.authPostToServer<boolean>(`character/update`, {
|
||||
character: updatedCharacter,
|
||||
}, session.accessToken, lang);
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
@@ -215,25 +221,26 @@ export function CharacterComponent(props: any, ref: any) {
|
||||
} else {
|
||||
try {
|
||||
let attributeId: string;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
attributeId = await window.electron.invoke<string>('db:character:attribute:add', {
|
||||
characterId: selectedCharacter.id,
|
||||
type: section,
|
||||
name: value.name,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
attributeId = await window.electron.invoke<string>('db:character:attribute:add', {
|
||||
attributeId = await System.authPostToServer<string>(`character/attribute/add`, {
|
||||
characterId: selectedCharacter.id,
|
||||
type: section,
|
||||
name: value.name,
|
||||
}, session.accessToken, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
|
||||
addToQueue('db:character:attribute:add', {
|
||||
characterId: selectedCharacter.id,
|
||||
attributeId,
|
||||
type: section,
|
||||
name: value.name,
|
||||
});
|
||||
} else {
|
||||
attributeId = await System.authPostToServer<string>(`character/attribute/add`, {
|
||||
characterId: selectedCharacter.id,
|
||||
type: section,
|
||||
name: value.name,
|
||||
}, session.accessToken, lang);
|
||||
}
|
||||
}
|
||||
if (!attributeId) {
|
||||
@@ -271,19 +278,19 @@ export function CharacterComponent(props: any, ref: any) {
|
||||
} else {
|
||||
try {
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline()) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:character:attribute:delete', {
|
||||
attributeId: attrId,
|
||||
});
|
||||
} else {
|
||||
if (book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:character:attribute:delete', {
|
||||
response = await System.authDeleteToServer<boolean>(`character/attribute/delete`, {
|
||||
attributeId: attrId,
|
||||
}, session.accessToken, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
|
||||
addToQueue('db:character:attribute:delete', {
|
||||
attributeId: attrId,
|
||||
});
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(`character/attribute/delete`, {
|
||||
attributeId: attrId,
|
||||
}, session.accessToken, lang);
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
|
||||
Reference in New Issue
Block a user