Skip to content

Commit

Permalink
✨ 로그아웃, 레시피 삭제 시점에 컨펌 dialog로 확인 #22
Browse files Browse the repository at this point in the history
♻️ RecipeListItem의 useRef와 useRouter를 부모 컴포넌트로 이동하여 훅 호출 횟수 줄임
  • Loading branch information
kihyeoon committed Sep 8, 2024
1 parent 0de3ff0 commit 869ffd4
Show file tree
Hide file tree
Showing 8 changed files with 749 additions and 66 deletions.
473 changes: 461 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"build-storybook": "storybook build"
},
"dependencies": {
"@radix-ui/react-alert-dialog": "^1.1.1",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.1.5",
"@sanity/image-url": "^1.0.2",
"@tanstack/react-query": "^5.37.1",
Expand Down
51 changes: 51 additions & 0 deletions src/components/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";

interface Props {
trigger: React.ReactNode;
title: string;
description?: string;
handleAction?: () => void;
handleCancel?: () => void;
actionText?: string;
cancelText?: string;
}

export function ConfirmDialog({
trigger,
title,
description,
handleAction,
handleCancel = () => {},
actionText = "네",
cancelText = "아니요",
}: Props) {
return (
<AlertDialog>
<AlertDialogTrigger asChild>{trigger}</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{title}</AlertDialogTitle>
<AlertDialogDescription>{description}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={handleCancel}>
{cancelText}
</AlertDialogCancel>
<AlertDialogAction onClick={handleAction}>
{actionText}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
15 changes: 11 additions & 4 deletions src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { signIn, signOut, useSession } from "next-auth/react";
import { usePathname } from "next/navigation";

import { ConfirmDialog } from "@/components/ConfirmDialog";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Button } from "@/components/ui/button";

Expand All @@ -18,10 +19,16 @@ export default function NavBar() {
return (
<nav className="z-20 mx-auto flex h-12 w-full max-w-md items-center justify-end gap-1 bg-background px-4 pt-2">
{user && (
<Avatar className="cursor-pointer" onClick={() => signOut()}>
<AvatarImage src={user.image} />
<AvatarFallback>{user.username}</AvatarFallback>
</Avatar>
<ConfirmDialog
trigger={
<Avatar className="cursor-pointer">
<AvatarImage src={user.image} />
<AvatarFallback>{user.username}</AvatarFallback>
</Avatar>
}
title="로그아웃 하시겠습니까?"
handleAction={() => signOut()}
/>
)}
{!session && <Button onClick={() => signIn()}>로그인</Button>}
</nav>
Expand Down
141 changes: 141 additions & 0 deletions src/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"use client"

import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"

import { cn } from "@/libs/utils"
import { buttonVariants } from "@/components/ui/button"

const AlertDialog = AlertDialogPrimitive.Root

const AlertDialogTrigger = AlertDialogPrimitive.Trigger

const AlertDialogPortal = AlertDialogPrimitive.Portal

const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName

const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName

const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"

const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"

const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName

const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName

const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName

const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName

export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}
14 changes: 14 additions & 0 deletions src/features/recipe/components/list/RecipeList.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useRouter } from "next/navigation";
import { useRef } from "react";

import { ListItemMotion } from "@/features/recipe/components/list/ListItemMotion";
import { RecipeListItem } from "@/features/recipe/components/list/RecipeListItem";
import { RecipePreview } from "@/features/recipe/models/recipe";
Expand All @@ -8,14 +11,25 @@ interface Props {
}

export default function RecipeList({ recipes, deleteRecipe }: Props) {
const router = useRouter();
const { current: isTouchDevice } = useRef(
"ontouchstart" in window || navigator.maxTouchPoints > 0,
);

const navigateToRecipe = (id: string) => {
router.push(`/recipe/${id}`);
};

return (
<ul className="flex flex-col gap-4 overflow-hidden pb-5">
{recipes.map((recipe, index) => (
<ListItemMotion key={recipe.id}>
<RecipeListItem
recipe={recipe}
index={index}
isTouchDevice={isTouchDevice}
deleteRecipe={deleteRecipe}
navigateToRecipe={navigateToRecipe}
/>
</ListItemMotion>
))}
Expand Down
Loading

0 comments on commit 869ffd4

Please sign in to comment.