Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/src/app/withdrawal/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function WithdrawalPage() {
<main className="flex items-center justify-center min-h-dvh w-full bg-surface-secondary">
<div className="bg-surface-secondary relative w-full max-w-lg h-dvh flex flex-col">
<div className="max-w-lg mx-auto w-full">
<BackHeader title="회원 탈퇴" />
<BackHeader title="회원 탈퇴" fallbackHref="/profile" />
</div>

{/* 메인 콘텐츠 - 스크롤 가능 */}
Expand Down
13 changes: 9 additions & 4 deletions apps/web/src/components/common/BackHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import { useRouter } from "next/navigation";

import { Header } from "@/components/common/Header";
import { goBackOrFallback } from "@/utils/navigation";

type BackHeaderProps = {
title: string;
/** 새 탭·링크로 바로 들어와 돌아갈 기록이 없을 때 이동할 경로 */
fallbackHref?: string;
};

/**
* 뒤로가기 버튼이 있는 헤더.
* router.back()만 클라이언트에서 필요하므로, 페이지는 서버 컴포넌트로 두고 이 헤더만 클라이언트로 분리한다.
* 뒤로가기 버튼이 있는 헤더
* 뒤로가기만 클라이언트에서 필요해 페이지는 서버 컴포넌트로 두고 이 헤더만 클라이언트로 분리함
*/
export const BackHeader = ({ title }: BackHeaderProps) => {
export const BackHeader = ({ title, fallbackHref = "/" }: BackHeaderProps) => {
const router = useRouter();

return <Header variant="navy" leftIcon="back" onLeftClick={() => router.back()} title={title} />;
return (
<Header variant="navy" leftIcon="back" onLeftClick={() => goBackOrFallback(router, fallbackHref)} title={title} />
);
};
2 changes: 1 addition & 1 deletion apps/web/src/components/profile/EditProfileBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const EditProfileBottomSheet = ({

<Button
onClick={handleSubmit(onSubmit)}
disabled={isLoading || !hasChanges || !nickname || nickname.length === 0}
disabled={isLoading || !hasChanges || !nickname?.trim()}
className={cn(
"absolute right-4 top-1/2 -translate-y-1/2",
"bg-transparent text-base font-bold px-2 py-1.5",
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/profile/ProfileClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SettingSection } from "@/components/profile/SettingSection";
import { ApiError } from "@/lib/apiClient";
import { logout } from "@/services/authService";
import { getMyProfile, ProfileNicknameUpdateError, uploadAndUpdateProfile } from "@/services/profileService";
import { goBackOrFallback } from "@/utils/navigation";

type ProfileClientProps = {
initialProfile: ProfileData | null;
Expand Down Expand Up @@ -183,7 +184,7 @@ export const ProfileClient = ({ initialProfile }: ProfileClientProps) => {
screen: "profile_main",
click_code: "menu.profile.header.back",
});
router.back();
goBackOrFallback(router, "/");
}}
title="나의 프로필"
style={{
Expand Down
29 changes: 29 additions & 0 deletions apps/web/src/utils/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { afterEach, describe, expect, it, vi } from "vitest";

import { goBackOrFallback } from "./navigation";

const createRouter = () => ({ back: vi.fn(), push: vi.fn() });

afterEach(() => vi.unstubAllGlobals());

describe("goBackOrFallback", () => {
it("방문 기록이 있으면 뒤로 감", () => {
vi.stubGlobal("window", { history: { length: 3 } });
const router = createRouter();

goBackOrFallback(router, "/profile");

expect(router.back).toHaveBeenCalledOnce();
expect(router.push).not.toHaveBeenCalled();
});

it("새 탭으로 바로 들어와 방문 기록이 없으면 기본 경로로 이동함", () => {
vi.stubGlobal("window", { history: { length: 1 } });
const router = createRouter();

goBackOrFallback(router, "/profile");

expect(router.push).toHaveBeenCalledWith("/profile");
expect(router.back).not.toHaveBeenCalled();
});
});
17 changes: 17 additions & 0 deletions apps/web/src/utils/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type BackRouter = {
back: () => void;
push: (href: string) => void;
};

/**
* 돌아갈 방문 기록이 있으면 뒤로 가고, 새 탭·링크로 바로 들어와 기록이 없으면 기본 경로로 이동함 (GLB-239)
* @param router - next/navigation의 router
* @param fallbackHref - 방문 기록이 없을 때 이동할 경로
*/
export const goBackOrFallback = (router: BackRouter, fallbackHref: string) => {
if (window.history.length > 1) {
router.back();
return;
}
router.push(fallbackHref);
};
14 changes: 14 additions & 0 deletions packages/shared/src/schemas/profile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";

import { nicknameSchema } from "./profile";

describe("nicknameSchema", () => {
it("공백만 있는 닉네임은 거부함", () => {
expect(nicknameSchema.safeParse(" ").success).toBe(false);
expect(nicknameSchema.safeParse("").success).toBe(false);
});

it("앞뒤 공백이 있어도 글자가 있으면 통과함", () => {
expect(nicknameSchema.safeParse(" 글로버 ").success).toBe(true);
});
});
2 changes: 2 additions & 0 deletions packages/shared/src/schemas/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const createNicknameSchema = (unchangedNickname?: string) =>
z
.string()
.min(1, { message: "닉네임을 입력해주세요." })
// 공백만 입력한 닉네임은 빈 값으로 봄 (저장값의 앞뒤 공백 처리는 기획 확정 전이라 그대로 둠, GLB-210)
.refine(value => value.trim().length > 0, { message: "닉네임을 입력해주세요." })
.max(PROFILE_VALIDATION.MAX_NICKNAME_LENGTH, {
message: `닉네임은 ${PROFILE_VALIDATION.MAX_NICKNAME_LENGTH}자 이하여야 합니다.`,
})
Expand Down
Loading