Skip to content

Commit

Permalink
feat: implement toast on ticker delete
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-jy-park committed Jan 20, 2024
1 parent fdb073e commit 247b5bd
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 7 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
"dependencies": {
"@lukemorales/query-key-factory": "^1.3.2",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-slot": "^1.0.2",
"@tanstack/react-query": "^5.17.9",
"@tanstack/react-query-devtools": "^5.17.9",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"eslint-plugin-tailwindcss": "^3.13.1",
"lucide-react": "^0.312.0",
"next": "14.0.4",
"next-themes": "^0.2.1",
"react": "^18",
"react-dom": "^18",
"sonner": "^1.3.1",
"tailwind-merge": "^2.2.0",
"vaul": "^0.8.0",
"zustand": "^4.4.7"
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { Inter, Poppins, DM_Sans } from "next/font/google";
import "./globals.css";
import React from "react";
import { GlobalProvider } from "./global-provider";
import { cn } from "@/utils/cn";
import NavigationBar from "@/components/common/NavigationBar";
import NavigationBar from "@/components/common/navigation-bar";
import { Toaster } from "@/components/ui/sonner";

const inter = Inter({ subsets: ["latin"] });
const poppins = Poppins({ subsets: ["latin"], weight: ["400", "500", "600", "700"] });
const dm_sans = DM_Sans({ subsets: ["latin"], weight: ["400", "500", "600", "700"] });

export const metadata: Metadata = {
title: "Create Next App",
Expand All @@ -16,11 +19,12 @@ export const metadata: Metadata = {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className={cn(inter.className)}>
<body className={cn(dm_sans.className)}>
<GlobalProvider>
<div className="flex h-full w-full flex-col items-center justify-center">
<NavigationBar />
<div className="h-full w-full max-w-[--max-width] flex-1 px-4">{children}</div>
<main className="h-full w-full max-w-[--max-width] flex-1 px-4">{children}</main>
<Toaster position="top-center" />
</div>
</GlobalProvider>
</body>
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions src/app/ticker/_components/ticker-list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use client";

import Toast from "@/components/common/toast/toast";
import { Button } from "@/components/ui/button";
import React from "react";
import { toast } from "sonner";

const TickerList = React.memo(() => {
const onTickerDelete = React.useCallback(() => {
toast.custom((t) => <Toast t={t} title={`You deleted ${"GOOGLE"}`} isRevertable={true} />);
}, []);

return (
<div className="flex h-full w-full flex-1 flex-col items-start">
<div className="flex w-full flex-1 flex-col items-start gap-4">
<div className="flex w-full items-center justify-between ">
<p className="text-2xl font-bold">GOOGLE</p>
<Button onClick={onTickerDelete}>Delete</Button>
</div>
<div className="flex w-full items-center justify-between ">
<p className="text-2xl font-bold">GOOGLE</p>
<Button onClick={onTickerDelete}>Delete</Button>
</div>
<div className="flex w-full items-center justify-between ">
<p className="text-2xl font-bold">GOOGLE</p>
<Button onClick={onTickerDelete}>Delete</Button>
</div>
<div className="flex w-full items-center justify-between ">
<p className="text-2xl font-bold">GOOGLE</p>
<Button onClick={onTickerDelete}>Delete</Button>
</div>
<div className="flex w-full items-center justify-between ">
<p className="text-2xl font-bold">GOOGLE</p>
<Button onClick={onTickerDelete}>Delete</Button>
</div>
</div>
</div>
);
});

export default TickerList;
8 changes: 5 additions & 3 deletions src/app/ticker/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Plus } from "lucide-react";
import { Drawer as DrawerPrimitive, DrawerTrigger } from "@/components/ui/drawer";
import { TickerDrawer } from "./_components/drawer";
import { TickerDrawer } from "./_components/ticker-drawer";
import TickerList from "./_components/ticker-list";

const TickerPage = () => {
return (
<DrawerPrimitive nested shouldScaleBackground>
<DrawerPrimitive>
<div className="mx-auto flex h-full w-full max-w-[--max-width] flex-col py-6 text-2xl font-bold">
<p>You Added 0 Tickers</p>
<div className="flex flex-1 flex-col justify-start py-10 text-lg">
<div className="flex flex-col justify-start py-10 text-lg">
<DrawerTrigger asChild>
<div className="flex w-full cursor-pointer items-center justify-start gap-4">
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-gray-200 text-[#8B95A1]">
Expand All @@ -17,6 +18,7 @@ const TickerPage = () => {
</div>
</DrawerTrigger>
</div>
<TickerList />
</div>
<TickerDrawer />
</DrawerPrimitive>
Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions src/components/common/toast/toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Button } from "@/components/ui/button";
import { cn } from "@/utils/cn";
import React from "react";
import { toast } from "sonner";

export interface ToastProps extends React.HTMLAttributes<HTMLDivElement> {
t?: string | number;
title: string;
isRevertable?: boolean;
}

const Toast = React.memo(({ t, title, isRevertable, ...props }: ToastProps) => {
const onUndo = React.useCallback(() => {
toast.dismiss(t);
}, [t]);

return (
<div
className={cn(
"flex items-center justify-between rounded-lg bg-[#8B95A1] px-5 py-4 text-white shadow",
props.className
)}
{...props}
>
<p>{title}</p>
{isRevertable && <UndoButton onClick={onUndo} />}
</div>
);
});

const UndoButton = React.memo(({ onClick }: { onClick: () => void }) => {
return (
<Button onClick={onClick} variant={"default"}>
<p>Undo</p>
</Button>
);
});

export default Toast;
46 changes: 46 additions & 0 deletions src/components/ui/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/utils/cn";

const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
);

export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return <Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props} />;
}
);
Button.displayName = "Button";

export { Button, buttonVariants };
31 changes: 31 additions & 0 deletions src/components/ui/sonner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client"

import { useTheme } from "next-themes"
import { Toaster as Sonner } from "sonner"

type ToasterProps = React.ComponentProps<typeof Sonner>

const Toaster = ({ ...props }: ToasterProps) => {
const { theme = "system" } = useTheme()

return (
<Sonner
theme={theme as ToasterProps["theme"]}
className="toaster group"
toastOptions={{
classNames: {
toast:
"group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg",
description: "group-[.toast]:text-muted-foreground",
actionButton:
"group-[.toast]:bg-primary group-[.toast]:text-primary-foreground",
cancelButton:
"group-[.toast]:bg-muted group-[.toast]:text-muted-foreground",
},
}}
{...props}
/>
)
}

export { Toaster }

0 comments on commit 247b5bd

Please sign in to comment.