Integrate offline logic for book creation and enhance synchronization
- Add offline handling to `AddNewBookForm` by updating `BooksSyncContext` with server-only and local-only book management. - Refactor `guideTourDone` to check offline completion states via `localStorage`. - Update and lock dependencies, including `@esbuild` and `@next`, to latest versions. - Clean up unused session state updates in book creation logic.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
'use client'
|
||||
import {ChangeEvent, Dispatch, SetStateAction, useContext, useEffect, useRef, useState} from "react";
|
||||
import {ChangeEvent, Dispatch, RefObject, SetStateAction, useContext, useEffect, useRef, useState} from "react";
|
||||
import {AlertContext} from "@/context/AlertContext";
|
||||
import System from "@/lib/models/System";
|
||||
import {SessionContext} from "@/context/SessionContext";
|
||||
@@ -28,6 +28,8 @@ import {UserProps} from "@/lib/models/User";
|
||||
import {useTranslations} from "next-intl";
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||
|
||||
interface MinMax {
|
||||
min: number;
|
||||
@@ -37,10 +39,11 @@ interface MinMax {
|
||||
export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<SetStateAction<boolean>> }) {
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {session, setSession} = useContext(SessionContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {errorMessage} = useContext(AlertContext);
|
||||
const {setServerOnlyBooks, setLocalOnlyBooks} = useContext<BooksSyncContextProps>(BooksSyncContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const modalRef: React.RefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
|
||||
const modalRef: RefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [title, setTitle] = useState<string>('');
|
||||
const [subtitle, setSubtitle] = useState<string>('');
|
||||
@@ -159,14 +162,45 @@ export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<
|
||||
bookId: bookId,
|
||||
...bookData
|
||||
};
|
||||
|
||||
setSession({
|
||||
...session,
|
||||
user: {
|
||||
...session.user as UserProps,
|
||||
books: [...((session.user as UserProps)?.books ?? []), book]
|
||||
}
|
||||
});
|
||||
if (isCurrentlyOffline()){
|
||||
setLocalOnlyBooks((prevBooks: SyncedBook[]): SyncedBook[] => [...prevBooks, {
|
||||
id: book.bookId,
|
||||
type: selectedBookType,
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
lastUpdate: new Date().getTime()/1000,
|
||||
chapters: [],
|
||||
characters: [],
|
||||
locations: [],
|
||||
worlds: [],
|
||||
incidents: [],
|
||||
plotPoints: [],
|
||||
issues: [],
|
||||
actSummaries: [],
|
||||
guideLine: null,
|
||||
aiGuideLine: null
|
||||
}]);
|
||||
}
|
||||
else {
|
||||
setServerOnlyBooks((prevBooks: SyncedBook[]): SyncedBook[] => [...prevBooks, {
|
||||
id: book.bookId,
|
||||
type: selectedBookType,
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
lastUpdate: new Date().getTime()/1000,
|
||||
chapters: [],
|
||||
characters: [],
|
||||
locations: [],
|
||||
worlds: [],
|
||||
incidents: [],
|
||||
plotPoints: [],
|
||||
issues: [],
|
||||
actSummaries: [],
|
||||
guideLine: null,
|
||||
aiGuideLine: null
|
||||
}]);
|
||||
}
|
||||
|
||||
setIsAddingBook(false);
|
||||
setCloseForm(false)
|
||||
} catch (e: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user