add client/admin pages, show info and created admin api and server actions

This commit is contained in:
2024-04-28 19:32:31 +03:00
parent db66161d81
commit d6b259d71c
33 changed files with 458 additions and 91 deletions

View File

@@ -0,0 +1,68 @@
'use client'
import { Card, CardContent, CardHeader } from '@/components/ui/card'
import { RoleGate } from '@/components/auth/role-gate'
import FormSuccess from '@/components/form-success'
import { UserRole } from '@prisma/client'
import { Button } from '@/components/ui/button'
import { toast } from 'sonner'
import { admin } from '@/actions/admin'
const AdminPage = () => {
const onServerActionClick = () => {
admin()
.then((data) => {
if (data.error) {
toast.error(data.error)
}
if (data.success) {
toast.success(data.success)
}
})
}
const onApiRouteClick = () => {
fetch('/api/admin')
.then((response) => {
if (response.ok) {
toast.success('Allow API Route')
} else {
toast.error('Forbidden API Route')
}
})
}
return (
<Card className="w-[600px]">
<CardHeader>
<p className="text-2xl font-semibold text-center">
🔑 Admin
</p>
</CardHeader>
<CardContent className="space-y-4">
<RoleGate allowedRole={UserRole.ADMIN}>
<FormSuccess message="You are allowed to see this content!"/>
</RoleGate>
<div className="flex flex-row justify-between items-center rounded-lg border p-3 shadow-md">
<p className="text-sm font-medium">
Admin-only API Route
</p>
<Button onClick={onApiRouteClick}>
Click to test
</Button>
</div>
<div className="flex flex-row justify-between items-center rounded-lg border p-3 shadow-md">
<p className="text-sm font-medium">
Admin-only Server Action
</p>
<Button onClick={onServerActionClick}>
Click to test
</Button>
</div>
</CardContent>
</Card>
)
}
export default AdminPage

View File

@@ -0,0 +1,14 @@
'use client'
import { UserInfo } from '@/components/cabinet/user-info'
import { useCurrentUser } from '@/hooks/useCurrentUser'
const ClientPage = ({ params }: any) => {
const user = useCurrentUser()
return (
<UserInfo user={user} label="💻 Client component"/>
)
}
export default ClientPage

View File

@@ -1,10 +1,9 @@
'use client'
import { logout } from '@/actions/logout'
import { useCurrentUser } from '@/hooks/useCurrentUser'
const CabinetPage = () => {
const user = useCurrentUser()
const CabinetPage = ({ params }: any) => {
const btnOnClick = () => logout()
return (

View File

@@ -0,0 +1,15 @@
'use server'
import { currentUser } from '@/lib/auth'
import { UserInfo } from '@/components/cabinet/user-info'
const ServerPage = async () => {
const user = await currentUser()
return (
<UserInfo user={user} label="🗄️ Server component"/>
)
}
export default ServerPage