Refactor DeleteBook component to update synchronization states and remove unused session updates
This commit is contained in:
@@ -9,18 +9,21 @@ import {AlertContext, AlertContextProps} from "@/context/AlertContext";
|
|||||||
import AlertBox from "@/components/AlertBox";
|
import AlertBox from "@/components/AlertBox";
|
||||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||||
import {BookContext} from "@/context/BookContext";
|
import {BookContext} from "@/context/BookContext";
|
||||||
|
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||||
|
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||||
|
|
||||||
interface DeleteBookProps {
|
interface DeleteBookProps {
|
||||||
bookId: string;
|
bookId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function DeleteBook({bookId}: DeleteBookProps) {
|
export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||||
const {session, setSession} = useContext(SessionContext);
|
const {session} = useContext(SessionContext);
|
||||||
const {lang} = useContext<LangContextProps>(LangContext)
|
const {lang} = useContext<LangContextProps>(LangContext)
|
||||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||||
const {book} = useContext(BookContext);
|
const {book} = useContext(BookContext);
|
||||||
const [showConfirmBox, setShowConfirmBox] = useState<boolean>(false);
|
const [showConfirmBox, setShowConfirmBox] = useState<boolean>(false);
|
||||||
const {errorMessage} = useContext<AlertContextProps>(AlertContext)
|
const {errorMessage} = useContext<AlertContextProps>(AlertContext)
|
||||||
|
const {serverOnlyBooks,setServerOnlyBooks,localOnlyBooks,setLocalOnlyBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||||
|
|
||||||
function handleConfirmation(): void {
|
function handleConfirmation(): void {
|
||||||
setShowConfirmBox(true);
|
setShowConfirmBox(true);
|
||||||
@@ -34,7 +37,8 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
|||||||
id: bookId,
|
id: bookId,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (book?.localBook) {
|
const ifLocalBook:SyncedBook|undefined = localOnlyBooks.find((book: SyncedBook):boolean => book.id === bookId);
|
||||||
|
if (ifLocalBook) {
|
||||||
response = await window.electron.invoke<boolean>('db:book:delete', {
|
response = await window.electron.invoke<boolean>('db:book:delete', {
|
||||||
id: bookId,
|
id: bookId,
|
||||||
});
|
});
|
||||||
@@ -51,33 +55,12 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
|||||||
}
|
}
|
||||||
if (response) {
|
if (response) {
|
||||||
setShowConfirmBox(false);
|
setShowConfirmBox(false);
|
||||||
const updatedBooks: BookProps[] = (session.user?.books || []).reduce((acc: BookProps[], book: BookProps): BookProps[] => {
|
if (book?.localBook){
|
||||||
if (book.bookId !== bookId) {
|
setLocalOnlyBooks(localOnlyBooks.filter((book:SyncedBook):boolean => book.id !== bookId));
|
||||||
acc.push({...book});
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
if (!response) {
|
|
||||||
errorMessage("Une erreur est survenue lors de la suppression du livre.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const updatedUser = {
|
setServerOnlyBooks(serverOnlyBooks.filter((book:SyncedBook):boolean => book.id !== bookId));
|
||||||
...(JSON.parse(JSON.stringify(session.user))),
|
setShowConfirmBox(false);
|
||||||
books: updatedBooks
|
|
||||||
};
|
|
||||||
|
|
||||||
const newSession = {
|
|
||||||
...JSON.parse(JSON.stringify(session)),
|
|
||||||
user: updatedUser,
|
|
||||||
isConnected: true,
|
|
||||||
accessToken: session.accessToken
|
|
||||||
};
|
|
||||||
|
|
||||||
setSession(newSession);
|
|
||||||
|
|
||||||
setTimeout((): void => {
|
|
||||||
setSession({...newSession});
|
|
||||||
}, 0);
|
|
||||||
}
|
}
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
if (e instanceof Error) {
|
if (e instanceof Error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user