mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-03-28 11:13:16 +00:00
feat: preserve back navigation on search and like button
This commit is contained in:
parent
8f63e6c365
commit
4405aa50c3
2 changed files with 14 additions and 5 deletions
|
|
@ -1,7 +1,7 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Icon, loadIcons } from "@iconify/react";
|
||||
import { abbreviateNumber } from "@/lib/abbreviation";
|
||||
|
||||
|
|
@ -16,13 +16,18 @@ interface Props {
|
|||
}
|
||||
|
||||
export default function LikeButton({ likes, isLiked, miiId, isLoggedIn, disabled, abbreviate, big }: Props) {
|
||||
const router = useRouter();
|
||||
|
||||
const [isLikedState, setIsLikedState] = useState(isLiked);
|
||||
const [likesState, setLikesState] = useState(likes);
|
||||
const [isAnimating, setIsAnimating] = useState(false);
|
||||
|
||||
const onClick = async () => {
|
||||
if (disabled) return;
|
||||
if (!isLoggedIn) redirect("/login");
|
||||
if (!isLoggedIn) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLikedState(!isLikedState);
|
||||
setLikesState(isLikedState ? likesState - 1 : likesState + 1);
|
||||
|
|
|
|||
|
|
@ -1,24 +1,28 @@
|
|||
"use client";
|
||||
|
||||
import { redirect, useSearchParams } from "next/navigation";
|
||||
import { redirect, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { Icon } from "@iconify/react";
|
||||
import { querySchema } from "@/lib/schemas";
|
||||
|
||||
export default function SearchBar() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const [query, setQuery] = useState("");
|
||||
|
||||
const handleSearch = () => {
|
||||
const result = querySchema.safeParse(query);
|
||||
if (!result.success) redirect("/");
|
||||
if (!result.success) {
|
||||
router.push("/");
|
||||
return;
|
||||
}
|
||||
|
||||
// Clone current search params and add query param
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("q", query);
|
||||
params.set("page", "1");
|
||||
|
||||
redirect(`/?${params.toString()}`);
|
||||
router.push(`/?${params.toString()}`);
|
||||
};
|
||||
|
||||
const handleKeyDown = (event: React.KeyboardEvent) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue