Skip to content

Commit cb45a65

Browse files
committed
Submit Notification on Success or Error
1 parent 67dede1 commit cb45a65

File tree

7 files changed

+640
-2
lines changed

7 files changed

+640
-2
lines changed

app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SavedProvider } from './saved/SavedContext'
44
import Navbar from '@/components/Navbar';
55
import Footer from '@/components/Footer';
66
import { Analytics } from "@vercel/analytics/react"
7+
import { Toaster } from "@/components/ui/toaster"
78

89
export const metadata: Metadata = {
910
title: 'Code Gems',
@@ -40,6 +41,7 @@ export default function RootLayout({
4041
{children}
4142
</SavedProvider>
4243
<Footer />
44+
<Toaster />
4345
<Analytics />
4446
</body>
4547
</html>

components/GithubProjectsPage.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button';
77
import { Star, GitFork, Search, Tag, ChevronLeft, ChevronRight } from 'lucide-react';
88
import { Textarea } from '@/components/ui/textarea';
99

10+
1011
interface Project {
1112
name: string;
1213
description: string;
@@ -31,6 +32,7 @@ function getRandomGradient() {
3132
}
3233

3334
export default function GithubProjectsPage() {
35+
const [SubmitButton, setSubmitButton] = useState("Submit")
3436
const [searchTerm, setSearchTerm] = useState('');
3537
const [projects, setProjects] = useState<Project[]>([]);
3638
const [savedProjects, setSavedProjects] = useState<string[]>(() => {
@@ -84,6 +86,7 @@ export default function GithubProjectsPage() {
8486
!projectRequest.githubLink.trim() ||
8587
!projectRequest.description.trim() ||
8688
!projectRequest.reason.trim()) {
89+
setSubmitButton("Please Fill every Text-Area")
8790
return;
8891
}
8992

@@ -95,16 +98,20 @@ export default function GithubProjectsPage() {
9598
});
9699

97100
if (!response.ok) {
101+
setSubmitButton("Failed to submit request")
98102
throw new Error('Failed to submit request');
99103
}
100104

105+
setSubmitButton("Successfully Sent")
106+
101107
setProjectRequest({
102108
title: '',
103109
githubLink: '',
104110
description: '',
105111
reason: ''
106112
});
107113
} catch (error) {
114+
setSubmitButton("Failed to submit request")
108115
console.error('Failed to submit project request:', error);
109116
}
110117
};
@@ -264,7 +271,7 @@ export default function GithubProjectsPage() {
264271
onClick={handleProjectRequest}
265272
className="bg-purple-500 hover:bg-purple-600 text-white w-full"
266273
>
267-
Submit
274+
{SubmitButton}
268275
</Button>
269276
</div>
270277
</div>

components/ui/toast.tsx

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"use client"
2+
3+
import * as React from "react"
4+
import * as ToastPrimitives from "@radix-ui/react-toast"
5+
import { cva, type VariantProps } from "class-variance-authority"
6+
import { X } from "lucide-react"
7+
8+
import { cn } from "@/lib/utils"
9+
10+
const ToastProvider = ToastPrimitives.Provider
11+
12+
const ToastViewport = React.forwardRef<
13+
React.ElementRef<typeof ToastPrimitives.Viewport>,
14+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
15+
>(({ className, ...props }, ref) => (
16+
<ToastPrimitives.Viewport
17+
ref={ref}
18+
className={cn(
19+
"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
20+
className
21+
)}
22+
{...props}
23+
/>
24+
))
25+
ToastViewport.displayName = ToastPrimitives.Viewport.displayName
26+
27+
const toastVariants = cva(
28+
"group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
29+
{
30+
variants: {
31+
variant: {
32+
default: "border bg-background text-foreground",
33+
destructive:
34+
"destructive group border-destructive bg-destructive text-destructive-foreground",
35+
},
36+
},
37+
defaultVariants: {
38+
variant: "default",
39+
},
40+
}
41+
)
42+
43+
const Toast = React.forwardRef<
44+
React.ElementRef<typeof ToastPrimitives.Root>,
45+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
46+
VariantProps<typeof toastVariants>
47+
>(({ className, variant, ...props }, ref) => {
48+
return (
49+
<ToastPrimitives.Root
50+
ref={ref}
51+
className={cn(toastVariants({ variant }), className)}
52+
{...props}
53+
/>
54+
)
55+
})
56+
Toast.displayName = ToastPrimitives.Root.displayName
57+
58+
const ToastAction = React.forwardRef<
59+
React.ElementRef<typeof ToastPrimitives.Action>,
60+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
61+
>(({ className, ...props }, ref) => (
62+
<ToastPrimitives.Action
63+
ref={ref}
64+
className={cn(
65+
"inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-tranrsparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive",
66+
className
67+
)}
68+
{...props}
69+
/>
70+
))
71+
ToastAction.displayName = ToastPrimitives.Action.displayName
72+
73+
const ToastClose = React.forwardRef<
74+
React.ElementRef<typeof ToastPrimitives.Close>,
75+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
76+
>(({ className, ...props }, ref) => (
77+
<ToastPrimitives.Close
78+
ref={ref}
79+
className={cn(
80+
"absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
81+
className
82+
)}
83+
toast-close=""
84+
{...props}
85+
>
86+
<X className="h-4 w-4" />
87+
</ToastPrimitives.Close>
88+
))
89+
ToastClose.displayName = ToastPrimitives.Close.displayName
90+
91+
const ToastTitle = React.forwardRef<
92+
React.ElementRef<typeof ToastPrimitives.Title>,
93+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
94+
>(({ className, ...props }, ref) => (
95+
<ToastPrimitives.Title
96+
ref={ref}
97+
className={cn("text-sm font-semibold", className)}
98+
{...props}
99+
/>
100+
))
101+
ToastTitle.displayName = ToastPrimitives.Title.displayName
102+
103+
const ToastDescription = React.forwardRef<
104+
React.ElementRef<typeof ToastPrimitives.Description>,
105+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
106+
>(({ className, ...props }, ref) => (
107+
<ToastPrimitives.Description
108+
ref={ref}
109+
className={cn("text-sm opacity-90", className)}
110+
{...props}
111+
/>
112+
))
113+
ToastDescription.displayName = ToastPrimitives.Description.displayName
114+
115+
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
116+
117+
type ToastActionElement = React.ReactElement<typeof ToastAction>
118+
119+
export {
120+
type ToastProps,
121+
type ToastActionElement,
122+
ToastProvider,
123+
ToastViewport,
124+
Toast,
125+
ToastTitle,
126+
ToastDescription,
127+
ToastClose,
128+
ToastAction,
129+
}

components/ui/toaster.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use client"
2+
3+
import { useToast } from "@/hooks/use-toast"
4+
import {
5+
Toast,
6+
ToastClose,
7+
ToastDescription,
8+
ToastProvider,
9+
ToastTitle,
10+
ToastViewport,
11+
} from "@/components/ui/toast"
12+
13+
export function Toaster() {
14+
const { toasts } = useToast()
15+
16+
return (
17+
<ToastProvider>
18+
{toasts.map(function ({ id, title, description, action, ...props }) {
19+
return (
20+
<Toast key={id} {...props}>
21+
<div className="grid gap-1">
22+
{title && <ToastTitle>{title}</ToastTitle>}
23+
{description && (
24+
<ToastDescription>{description}</ToastDescription>
25+
)}
26+
</div>
27+
{action}
28+
<ToastClose />
29+
</Toast>
30+
)
31+
})}
32+
<ToastViewport />
33+
</ToastProvider>
34+
)
35+
}

0 commit comments

Comments
 (0)