Refactor imports, streamline database IPC handlers, and improve offline support
- Replace absolute import paths with relative paths for consistency across files. - Transition database operations in Electron main process to modular handlers in `ipc/book.ipc.ts` using the `createHandler` pattern. - Update database sync service to use `window.electron.invoke()` for improved reliability and structure. - Refactor `AddNewBookForm` to handle both online and offline book creation seamlessly.
This commit is contained in:
@@ -27,7 +27,7 @@ import GuideTour, {GuideStep} from "@/components/GuideTour";
|
||||
import {UserProps} from "@/lib/models/User";
|
||||
import {useTranslations} from "next-intl";
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
// TODO: Refactor to use window.electron.invoke() instead of OfflineDataService
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
|
||||
interface MinMax {
|
||||
min: number;
|
||||
@@ -39,6 +39,7 @@ export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {session, setSession} = useContext(SessionContext);
|
||||
const {errorMessage} = useContext(AlertContext);
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const modalRef: React.RefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [title, setTitle] = useState<string>('');
|
||||
@@ -123,7 +124,6 @@ export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<
|
||||
}
|
||||
setIsAddingBook(true);
|
||||
try {
|
||||
const offlineDataService = getOfflineDataService();
|
||||
const bookData = {
|
||||
title,
|
||||
subTitle: subtitle,
|
||||
@@ -134,26 +134,25 @@ export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<
|
||||
desiredWordCount: wordCount
|
||||
};
|
||||
|
||||
const bookId: string = await offlineDataService.createBook(
|
||||
bookData,
|
||||
session.user?.id || '',
|
||||
async () => {
|
||||
// Only called if online
|
||||
const id = await System.authPostToServer<string>('book/add', {
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
type: selectedBookType,
|
||||
summary: summary,
|
||||
serie: 0,
|
||||
publicationDate: publicationDate,
|
||||
desiredWordCount: wordCount,
|
||||
}, token, lang);
|
||||
if (!id) {
|
||||
throw new Error(t('addNewBookForm.error.addingBook'));
|
||||
}
|
||||
return id;
|
||||
let bookId: string;
|
||||
if (!isCurrentlyOffline()) {
|
||||
// Online - call API server
|
||||
bookId = await System.authPostToServer<string>('book/add', {
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
type: selectedBookType,
|
||||
summary: summary,
|
||||
serie: 0,
|
||||
publicationDate: publicationDate,
|
||||
desiredWordCount: wordCount,
|
||||
}, token, lang);
|
||||
if (!bookId) {
|
||||
throw new Error(t('addNewBookForm.error.addingBook'));
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Offline - call local database
|
||||
bookId = await window.electron.invoke<string>('db:book:create', bookData);
|
||||
}
|
||||
|
||||
const book: BookProps = {
|
||||
bookId: bookId,
|
||||
|
||||
Reference in New Issue
Block a user