added game version feature to users

This commit is contained in:
Polaris 2024-08-20 17:24:05 -04:00
parent d7a2d92bb0
commit 43daea7994
8 changed files with 249 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import { getAuth } from "@/auth/queries/getauth";
import { daphnis } from "@/lib/prisma";
import { GameVersion } from "@/prisma/schemas/daphnis/generated/daphnis";
export async function getCurrentGame() {
export async function getGameList() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
@ -51,18 +51,3 @@ export async function updatePlayerGameVersionChuni(gameVersion?: GameVersion) {
throw error;
}
}
export async function getAllGameVersionsChuni() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const AllGameVersions = await daphnis.user.findMany({
select: {
gameVersion: true,
},
});
return AllGameVersions;
}

View File

@ -3,10 +3,10 @@
import React from "react";
import { Card, CardHeader, CardTitle } from "@/components/ui/card";
import { ChunithmGameVersionSelection } from "./gameSelection";
import { getAllGameVersionsChuni } from "./actions";
import { getGameList } from "./actions";
const getAllGamesChunithm = async () => {
const gameVersions = await getAllGameVersionsChuni();
const gameVersions = await getGameList();
return { gameVersions };
};
@ -14,7 +14,7 @@ const Page = async () => {
const AllChunithmVersions = await getAllGamesChunithm();
return (
<Card x-chunk="gameverion">
<Card x-chunk="gameversions">
<CardHeader>
<CardTitle className="text-2xl">Update Game Versions</CardTitle>
</CardHeader>

View File

@ -1,9 +1,6 @@
import { getAuth } from "@/auth/queries/getauth";
import { GeneralSettings } from "./home";
const ProtectedDashboardPage = async () => {
const { user } = await getAuth();
return (
<div>
<GeneralSettings />

View File

@ -0,0 +1,54 @@
"use server";
import { getAuth } from "@/auth/queries/getauth";
import { daphnis } from "@/lib/prisma";
import { GameVersion } from "@/prisma/schemas/daphnis/generated/daphnis";
export async function getGameList() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const currentGameVersion = await daphnis.user.findMany({
where: {
UserId: user.UserId,
},
select: {
gameVersion: true,
},
});
return currentGameVersion;
}
export async function updatePlayerGameVersionChuni(gameVersion?: GameVersion) {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
if (gameVersion === undefined) {
throw new Error("gameVersion is required");
}
try {
const updatedUser = await daphnis.user.update({
where: {
UserId: user.UserId,
},
data: {
gameVersion,
},
select: {
gameVersion: true,
},
});
return { gameVersion: updatedUser.gameVersion };
} catch (error) {
console.error("Error updating game version:", error);
throw error;
}
}

View File

@ -0,0 +1,162 @@
"use client";
import React, { FC, useState } from "react";
import { Check, ChevronsUpDown } from "lucide-react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { toast } from "@/components/ui/use-toast";
import { z } from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { GameVersion, User } from "@/prisma/schemas/daphnis/generated/daphnis";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { updatePlayerGameVersionChuni } from "./actions";
type ChunithmGameVersionSelectionProps = {
chunithmGameVersionNumber: {
gameVersions: { gameVersion: GameVersion }[];
};
};
export function PlayerChangableChunithmGameVersionSelection({
chunithmGameVersionNumber,
}: ChunithmGameVersionSelectionProps) {
const FormSchema = z.object({
mapIconId: z.string({
required_error: "Please select a Game Version.",
}),
});
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
});
const [selectedGameVersion, setSelectedGameVersion] = useState<
string | undefined
>(Object.values(GameVersion)[0]);
function onSubmit(data: z.infer<typeof FormSchema>) {
const newGameVersion = data.mapIconId ?? selectedGameVersion;
// console.log("Submitted Game Version:", newGameVersion);
updatePlayerGameVersionChuni(newGameVersion as GameVersion)
.then((result) => {
toast({
title: "Game version updated successfully!",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">
{JSON.stringify(result, null, 2)}
</code>
</pre>
),
});
setSelectedGameVersion(newGameVersion);
form.reset({
mapIconId: undefined,
});
})
.catch((error) => {
toast({
title: "Error updating game version",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{error.message}</code>
</pre>
),
variant: "destructive",
});
});
}
return (
<main className="flex flex-col items-center space-y-6 p-4">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="mapIconId"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="pb-2">Chunithm</FormLabel>
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground",
)}
>
{field.value
? GameVersion[field.value as keyof typeof GameVersion]
: "Select Game Version"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0">
<Command>
<CommandList>
<CommandEmpty>No game version found.</CommandEmpty>
<CommandGroup>
{Object.values(GameVersion).map((version) => (
<CommandItem
value={version}
key={version}
onSelect={() => {
form.setValue("mapIconId", version);
setSelectedGameVersion(version);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
version === field.value
? "opacity-100"
: "opacity-0",
)}
/>
{version}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end">
<Button type="submit">Submit</Button>
</div>
</form>
</Form>
</main>
);
}

View File

@ -0,0 +1,25 @@
import { PlayerChangableChunithmGameVersionSelection } from "./gameSelection";
import { getGameList } from "./actions";
import { Card, CardHeader, CardTitle } from "@/components/ui/card";
const getAllGamesChunithm = async () => {
const gameVersions = await getGameList();
return { gameVersions };
};
const Versions = async () => {
const AllChunithmVersions = await getAllGamesChunithm();
return (
<Card x-chunk="gameversions">
<CardHeader>
<CardTitle className="text-2xl">Update Game Versions</CardTitle>
</CardHeader>
<PlayerChangableChunithmGameVersionSelection
chunithmGameVersionNumber={AllChunithmVersions}
/>
</Card>
);
};
export default Versions;

View File

@ -96,7 +96,7 @@ const Page = async () => {
<TabsTrigger value="customize">Customize</TabsTrigger>
<TabsTrigger value="TopPlays">Top Plays</TabsTrigger>
<TabsTrigger value="HotPlays">Hot Plays</TabsTrigger>
<TabsTrigger value="Settings">Settings</TabsTrigger>
{/* <TabsTrigger value="Settings">Settings</TabsTrigger> */}
<TabsTrigger value="Patcher" className="hidden md:block">
Patcher
</TabsTrigger>
@ -143,9 +143,9 @@ const Page = async () => {
<TabsContent className="" value="Patcher">
<Patcher />
</TabsContent>
<TabsContent className="" value="Settings">
{/* <TabsContent className="" value="Settings">
<Patcher />
</TabsContent>
</TabsContent> */}
</Tabs>
</div>
);

View File

@ -6,6 +6,7 @@ import { usePathname } from "next/navigation";
const NAV_ITEMS = [
{ href: "/settings/home", label: "General" },
{ href: "/settings/security", label: "Security" },
{ href: "/settings/versions", label: "Edit Game Version" },
];
const SettingsSubMenuNavigation = () => {