Skip to content

Commit 14e342a

Browse files
refactor: Update CommandDialogComponent to fetch courses based on user role
This commit refactors the CommandDialogComponent in the components/dashboards/Common/CommandDialogComponent.tsx file. It adds functionality to fetch courses based on the user's role, ensuring that the displayed courses are relevant to the user. This enhancement improves the user experience by providing personalized course suggestions and improving the overall usability of the CommandDialogComponent.
1 parent 97bc9ed commit 14e342a

File tree

1 file changed

+91
-20
lines changed

1 file changed

+91
-20
lines changed

components/dashboards/Common/CommandDialogComponent.tsx

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use client'
22

33
import {
4+
Bell,
45
Calculator,
5-
Calendar,
66
CreditCard,
7-
Settings,
87
Smile,
98
User
109
} from 'lucide-react'
11-
import * as React from 'react'
10+
import Link from 'next/link'
11+
import { useEffect, useState } from 'react'
1212

1313
import {
1414
CommandDialog,
@@ -17,14 +17,21 @@ import {
1717
CommandInput,
1818
CommandItem,
1919
CommandList,
20-
CommandSeparator,
21-
CommandShortcut
20+
CommandSeparator
2221
} from '@/components/ui/command'
22+
import { useToast } from '@/components/ui/use-toast'
23+
import { createClient } from '@/utils/supabase/client'
24+
import { getClientUserRole } from '@/utils/supabase/getClientUserRole'
2325

2426
export function CommandDialogComponent () {
25-
const [open, setOpen] = React.useState(false)
27+
const [open, setOpen] = useState(false)
28+
const [userRole, setUserRole] = useState('' as string)
29+
const [loading, setLoading] = useState(false)
30+
const { toast } = useToast()
31+
const [courses, setCourses] = useState([])
32+
const [lessons, setLessons] = useState([])
2633

27-
React.useEffect(() => {
34+
useEffect(() => {
2835
const down = (e: KeyboardEvent) => {
2936
if (e.key === 'j' && (e.metaKey || e.ctrlKey)) {
3037
e.preventDefault()
@@ -36,6 +43,45 @@ export function CommandDialogComponent () {
3643
return () => document.removeEventListener('keydown', down)
3744
}, [])
3845

46+
useEffect(() => {
47+
async function fetchUserRole () {
48+
const response = await getClientUserRole()
49+
setUserRole(response)
50+
}
51+
52+
fetchUserRole()
53+
}, [])
54+
55+
useEffect(() => {
56+
async function fetchCourses () {
57+
setLoading(true)
58+
try {
59+
const supabase = createClient()
60+
const { data: courses, error } = await supabase.from('courses').select('*').limit(5)
61+
62+
if (error) {
63+
console.error(error)
64+
return null
65+
}
66+
67+
setCourses(courses)
68+
} catch (error) {
69+
toast({
70+
title: 'Error fetching courses',
71+
description: error.message,
72+
variant: 'destructive'
73+
})
74+
} finally {
75+
setLoading(false)
76+
}
77+
}
78+
79+
fetchCourses()
80+
}
81+
, [userRole])
82+
83+
console.log(courses)
84+
3985
return (
4086
<>
4187
<div className="text-sm text-muted-foreground">
@@ -49,8 +95,13 @@ export function CommandDialogComponent () {
4995
<CommandEmpty>No results found.</CommandEmpty>
5096
<CommandGroup heading="Suggestions">
5197
<CommandItem>
52-
<Calendar className="mr-2 h-4 w-4" />
53-
<span>Calendar</span>
98+
<Link
99+
className='flex items-center gap-2'
100+
href="/dashboard/notifications"
101+
>
102+
<Bell className="mr-2 h-4 w-4" />
103+
<span>Notifications</span>
104+
</Link>
54105
</CommandItem>
55106
<CommandItem>
56107
<Smile className="mr-2 h-4 w-4" />
@@ -64,21 +115,41 @@ export function CommandDialogComponent () {
64115
<CommandSeparator />
65116
<CommandGroup heading="Settings">
66117
<CommandItem>
67-
<User className="mr-2 h-4 w-4" />
68-
<span>Profile</span>
69-
<CommandShortcut>⌘P</CommandShortcut>
70-
</CommandItem>
71-
<CommandItem>
72-
<CreditCard className="mr-2 h-4 w-4" />
73-
<span>Billing</span>
74-
<CommandShortcut>⌘B</CommandShortcut>
118+
<Link
119+
className='flex items-center gap-2'
120+
href={`/dashboard/${userRole}/profile`}
121+
>
122+
<User className="mr-2 h-4 w-4" />
123+
<span>Profile</span>
124+
</Link>
75125
</CommandItem>
76126
<CommandItem>
77-
<Settings className="mr-2 h-4 w-4" />
78-
<span>Settings</span>
79-
<CommandShortcut>⌘S</CommandShortcut>
127+
<Link
128+
className='flex items-center gap-2'
129+
href={`/dashboard/${userRole}/profile`}
130+
>
131+
<CreditCard className="mr-2 h-4 w-4" />
132+
<span>Billing</span>
133+
</Link>
80134
</CommandItem>
81135
</CommandGroup>
136+
<CommandGroup heading="Courses">
137+
{loading
138+
? <>Loading...</>
139+
: courses.map((course: any) => (
140+
<CommandItem key={course.course_id}>
141+
<Link
142+
className='flex items-center gap-2'
143+
href={`/dashboard/${userRole}/courses/${course.course_id}`}
144+
>
145+
<span>{course.title}</span>
146+
</Link>
147+
</CommandItem>
148+
))
149+
}
150+
151+
</CommandGroup>
152+
82153
</CommandList>
83154
</CommandDialog>
84155
</>

0 commit comments

Comments
 (0)