feat: pref for default filter showUnread
This commit is contained in:
@@ -14,6 +14,8 @@ interface PaginatedSeriesGridProps {
|
||||
totalPages: number;
|
||||
totalElements: number;
|
||||
pageSize: number;
|
||||
defaultShowOnlyUnread: boolean;
|
||||
showOnlyUnread: boolean;
|
||||
}
|
||||
|
||||
export function PaginatedSeriesGrid({
|
||||
@@ -22,26 +24,43 @@ export function PaginatedSeriesGrid({
|
||||
totalPages,
|
||||
totalElements,
|
||||
pageSize,
|
||||
defaultShowOnlyUnread,
|
||||
showOnlyUnread: initialShowOnlyUnread,
|
||||
}: PaginatedSeriesGridProps) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [isChangingPage, setIsChangingPage] = useState(false);
|
||||
const [showOnlyUnread, setShowOnlyUnread] = useState(searchParams.get("unread") === "true");
|
||||
const [showOnlyUnread, setShowOnlyUnread] = useState(initialShowOnlyUnread);
|
||||
|
||||
// Réinitialiser l'état de chargement quand les séries changent
|
||||
useEffect(() => {
|
||||
setIsChangingPage(false);
|
||||
}, [series]);
|
||||
|
||||
// Mettre à jour l'état local quand la prop change
|
||||
useEffect(() => {
|
||||
setShowOnlyUnread(initialShowOnlyUnread);
|
||||
}, [initialShowOnlyUnread]);
|
||||
|
||||
// Appliquer le filtre par défaut au chargement initial
|
||||
useEffect(() => {
|
||||
if (defaultShowOnlyUnread && !searchParams.has("unread")) {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", "1");
|
||||
params.set("unread", "true");
|
||||
router.push(`${pathname}?${params.toString()}`);
|
||||
}
|
||||
}, [defaultShowOnlyUnread, pathname, router, searchParams]);
|
||||
|
||||
const handlePageChange = async (page: number) => {
|
||||
setIsChangingPage(true);
|
||||
// Créer un nouvel objet URLSearchParams pour manipuler les paramètres
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", page.toString());
|
||||
if (showOnlyUnread) {
|
||||
params.set("unread", "true");
|
||||
}
|
||||
|
||||
// Conserver l'état du filtre unread
|
||||
params.set("unread", showOnlyUnread.toString());
|
||||
|
||||
// Rediriger vers la nouvelle URL avec les paramètres mis à jour
|
||||
await router.push(`${pathname}?${params.toString()}`);
|
||||
@@ -52,13 +71,12 @@ export function PaginatedSeriesGrid({
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", "1"); // Retourner à la première page lors du changement de filtre
|
||||
|
||||
if (!showOnlyUnread) {
|
||||
params.set("unread", "true");
|
||||
} else {
|
||||
params.delete("unread");
|
||||
}
|
||||
const newUnreadState = !showOnlyUnread;
|
||||
setShowOnlyUnread(newUnreadState);
|
||||
|
||||
// Toujours définir explicitement le paramètre unread
|
||||
params.set("unread", newUnreadState.toString());
|
||||
|
||||
setShowOnlyUnread(!showOnlyUnread);
|
||||
await router.push(`${pathname}?${params.toString()}`);
|
||||
};
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@ interface PaginatedBookGridProps {
|
||||
totalPages: number;
|
||||
totalElements: number;
|
||||
pageSize: number;
|
||||
defaultShowOnlyUnread: boolean;
|
||||
showOnlyUnread: boolean;
|
||||
}
|
||||
|
||||
export function PaginatedBookGrid({
|
||||
@@ -22,26 +24,43 @@ export function PaginatedBookGrid({
|
||||
totalPages,
|
||||
totalElements,
|
||||
pageSize,
|
||||
defaultShowOnlyUnread,
|
||||
showOnlyUnread: initialShowOnlyUnread,
|
||||
}: PaginatedBookGridProps) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [isChangingPage, setIsChangingPage] = useState(false);
|
||||
const [showOnlyUnread, setShowOnlyUnread] = useState(searchParams.get("unread") === "true");
|
||||
const [showOnlyUnread, setShowOnlyUnread] = useState(initialShowOnlyUnread);
|
||||
|
||||
// Réinitialiser l'état de chargement quand les tomes changent
|
||||
useEffect(() => {
|
||||
setIsChangingPage(false);
|
||||
}, [books]);
|
||||
|
||||
// Mettre à jour l'état local quand la prop change
|
||||
useEffect(() => {
|
||||
setShowOnlyUnread(initialShowOnlyUnread);
|
||||
}, [initialShowOnlyUnread]);
|
||||
|
||||
// Appliquer le filtre par défaut au chargement initial
|
||||
useEffect(() => {
|
||||
if (defaultShowOnlyUnread && !searchParams.has("unread")) {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", "1");
|
||||
params.set("unread", "true");
|
||||
router.push(`${pathname}?${params.toString()}`);
|
||||
}
|
||||
}, [defaultShowOnlyUnread, pathname, router, searchParams]);
|
||||
|
||||
const handlePageChange = async (page: number) => {
|
||||
setIsChangingPage(true);
|
||||
// Créer un nouvel objet URLSearchParams pour manipuler les paramètres
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", page.toString());
|
||||
if (showOnlyUnread) {
|
||||
params.set("unread", "true");
|
||||
}
|
||||
|
||||
// Conserver l'état du filtre unread
|
||||
params.set("unread", showOnlyUnread.toString());
|
||||
|
||||
// Rediriger vers la nouvelle URL avec les paramètres mis à jour
|
||||
await router.push(`${pathname}?${params.toString()}`);
|
||||
@@ -52,13 +71,12 @@ export function PaginatedBookGrid({
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("page", "1"); // Retourner à la première page lors du changement de filtre
|
||||
|
||||
if (!showOnlyUnread) {
|
||||
params.set("unread", "true");
|
||||
} else {
|
||||
params.delete("unread");
|
||||
}
|
||||
const newUnreadState = !showOnlyUnread;
|
||||
setShowOnlyUnread(newUnreadState);
|
||||
|
||||
// Toujours définir explicitement le paramètre unread
|
||||
params.set("unread", newUnreadState.toString());
|
||||
|
||||
setShowOnlyUnread(!showOnlyUnread);
|
||||
await router.push(`${pathname}?${params.toString()}`);
|
||||
};
|
||||
|
||||
|
||||
@@ -306,6 +306,39 @@ export function ClientSettings({ initialConfig, initialTTLConfig }: ClientSettin
|
||||
onCheckedChange={handleToggleThumbnails}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label htmlFor="unread-filter">Filtre "À lire" par défaut</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Afficher uniquement les séries non lues par défaut
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="unread-filter"
|
||||
checked={preferences.showOnlyUnread}
|
||||
onCheckedChange={async (checked) => {
|
||||
try {
|
||||
await updatePreferences({ showOnlyUnread: checked });
|
||||
toast({
|
||||
title: "Préférences sauvegardées",
|
||||
description: `Le filtre "À lire" par défaut est maintenant ${
|
||||
checked ? "activé" : "désactivé"
|
||||
}`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur détaillée:", error);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Erreur",
|
||||
description:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Une erreur est survenue lors de la mise à jour des préférences",
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user