Certainly! Below is an example of a page.tsx file for a Next.js dashboard using ShadCN UI components. This dashboard allows users to view a list of servers and add a new server by specifying the number of coins.
Features Implemented:
- Server List: Displays a list of servers using the
DataTable component.
- Add Server: A button triggers a
Dialog where users can input the number of coins to add a new server.
- Notifications: Uses
Toast to display success messages upon adding a server.
Prerequisites:
- Ensure you have ShadCN UI set up in your Next.js project.
- Install necessary dependencies if not already installed:
npm install @shadcn/ui
page.tsx Code:
// app/dashboard/page.tsx
"use client";
import { useState } from "react";
import {
Button,
Card,
Dialog,
DialogTrigger,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
Input,
Label,
Toast,
ToastProvider,
useToast,
DataTable,
Table,
TableHeader,
TableRow,
TableHead,
TableBody,
TableCell,
} from "@/components/ui"; // Adjust the import paths based on your project structure
// Define the Server type
type Server = {
id: number;
name: string;
coins: number;
status: string;
};
// Sample data
const initialServers: Server[] = [
{ id: 1, name: "Server One", coins: 100, status: "Active" },
{ id: 2, name: "Server Two", coins: 200, status: "Inactive" },
// Add more servers as needed
];
const DashboardPage = () => {
const [servers, setServers] = useState<Server[]>(initialServers);
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [newServerName, setNewServerName] = useState("");
const [newServerCoins, setNewServerCoins] = useState<number>(0);
const { toast } = useToast();
const handleAddServer = () => {
if (newServerName && newServerCoins > 0) {
const newServer: Server = {
id: servers.length + 1,
name: newServerName,
coins: newServerCoins,
status: "Active",
};
setServers([...servers, newServer]);
setIsDialogOpen(false);
setNewServerName("");
setNewServerCoins(0);
toast({
title: "Server Added",
description: `Server "${newServer.name}" has been added successfully.`,
});
} else {
toast({
title: "Invalid Input",
description: "Please provide a valid server name and coin amount.",
variant: "destructive",
});
}
};
return (
<ToastProvider>
<div className="p-6">
<h1 className="text-2xl font-bold mb-4">Dashboard</h1>
{/* Add Server Button */}
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<DialogTrigger asChild>
<Button variant="primary" className="mb-4">
Add Server
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Add a New Server</DialogTitle>
<DialogDescription>
Enter the details to add a new server.
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div>
<Label htmlFor="server-name">Server Name</Label>
<Input
id="server-name"
value={newServerName}
onChange={(e) => setNewServerName(e.target.value)}
placeholder="Enter server name"
/>
</div>
<div>
<Label htmlFor="server-coins">Coins</Label>
<Input
id="server-coins"
type="number"
value={newServerCoins}
onChange={(e) => setNewServerCoins(Number(e.target.value))}
placeholder="Enter number of coins"
/>
</div>
</div>
<DialogFooter>
<Button variant="secondary" onClick={() => setIsDialogOpen(false)}>
Cancel
</Button>
<Button variant="primary" onClick={handleAddServer}>
Add Server
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* Server List */}
<Card>
<DataTable>
<Table>
<TableHeader>
<TableRow>
<TableHead>ID</TableHead>
<TableHead>Name</TableHead>
<TableHead>Coins</TableHead>
<TableHead>Status</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{servers.map((server) => (
<TableRow key={server.id}>
<TableCell>{server.id}</TableCell>
<TableCell>{server.name}</TableCell>
<TableCell>{server.coins}</TableCell>
<TableCell>{server.status}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</DataTable>
</Card>
{/* Toast Notifications */}
<Toast />
</div>
</ToastProvider>
);
};
export default DashboardPage;
Explanation of Components Used:
- Button: Triggers the dialog to add a new server.
- Dialog: A modal dialog that contains the form to add a new server.
- Input: For entering server name and coin amount.
- Label: Labels for the input fields.
- Card: Wraps the server list table.
- DataTable, Table, TableHeader, TableRow, TableHead, TableBody, TableCell: Structures the server list in a tabular format.
- ToastProvider & Toast: Provides and displays toast notifications for user feedback.
Styling and Layout:
- The
p-6 class adds padding around the dashboard content.
- The
text-2xl and font-bold classes style the dashboard title.
- Margins (
mb-4, space-y-4) are used for spacing between elements.
Notes:
- State Management: The component uses React's
useState hook to manage the list of servers and the state of the dialog.
- Validation: Before adding a new server, the code checks if the server name is provided and the coin amount is greater than zero.
- Toast Notifications: Provides feedback to the user upon successful addition or if there's an input error.
- Accessibility: The dialog and form elements include labels and descriptions to enhance accessibility.
Feel free to customize the components and styles further to match your project's requirements and design guidelines.