Refactor and extend offline synchronization logic across components and services
- Integrated sync queue mechanisms with `LocalSyncQueueContext` for offline data handling. - Updated key sync-related services (e.g., book, chapter, series) to support offline-first functionality. - Removed redundant database fetch methods to optimize repository logic and improve maintainability. - Enhanced Tauri IPC usage for sync operations and removed legacy methods in Rust services.
This commit is contained in:
@@ -6,6 +6,9 @@ import {apiDelete, apiPost} from '@/lib/api/client';
|
||||
import {isDesktop} from '@/lib/configs';
|
||||
import * as tauri from '@/lib/tauri';
|
||||
import OfflineContext, {OfflineContextType} from '@/context/OfflineContext';
|
||||
import {BooksSyncContext, BooksSyncContextProps} from '@/context/BooksSyncContext';
|
||||
import {SyncedBook} from '@/lib/types/synced-book';
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from '@/context/SyncQueueContext';
|
||||
import {BookContext, BookContextProps} from '@/context/BookContext';
|
||||
import {SessionContext, SessionContextProps} from '@/context/SessionContext';
|
||||
import {AlertContext, AlertContextProps} from '@/context/AlertContext';
|
||||
@@ -27,6 +30,8 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
|
||||
const {errorMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {isCurrentlyOffline}: OfflineContextType = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
|
||||
const bookId: string | undefined = book?.bookId;
|
||||
const token: string = session.accessToken;
|
||||
@@ -43,10 +48,12 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
issueId = await tauri.addIssue(bookId ?? '', newIssueName);
|
||||
} else {
|
||||
issueId = await apiPost<string>('book/issue/add', {
|
||||
bookId,
|
||||
name: newIssueName,
|
||||
}, token, lang);
|
||||
const addData = {bookId, name: newIssueName};
|
||||
issueId = await apiPost<string>('book/issue/add', addData, token, lang);
|
||||
|
||||
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
|
||||
addToQueue('add_issue', addData);
|
||||
}
|
||||
}
|
||||
if (!issueId) {
|
||||
errorMessage(t("issues.errorAdd"));
|
||||
@@ -79,15 +86,12 @@ export default function Issues({issues, setIssues}: IssuesProps) {
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.removeIssue(bookId ?? '', issueId, Date.now());
|
||||
} else {
|
||||
response = await apiDelete<boolean>(
|
||||
'book/issue/remove',
|
||||
{
|
||||
bookId,
|
||||
issueId,
|
||||
},
|
||||
token,
|
||||
lang
|
||||
);
|
||||
const deleteData = {bookId, issueId};
|
||||
response = await apiDelete<boolean>('book/issue/remove', deleteData, token, lang);
|
||||
|
||||
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
|
||||
addToQueue('remove_issue', {...deleteData, deletedAt: Date.now()});
|
||||
}
|
||||
}
|
||||
if (response) {
|
||||
const updatedIssues: Issue[] = issues.filter((issue: Issue): boolean => issue.id !== issueId,);
|
||||
|
||||
Reference in New Issue
Block a user