added update functions
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
"use server";
|
||||
|
||||
import { getAuth } from "@/auth/queries/getauth";
|
||||
import { artemis, daphnis } from "@/lib/prisma";
|
||||
import type * as Prisma from "@prisma/client";
|
||||
import { supportedVersionNumber } from "@/lib/helpers";
|
||||
import { artemis } from "@/lib/prisma";
|
||||
|
||||
export async function getCurrentTrophies() {
|
||||
const { user } = await getAuth();
|
||||
@ -14,6 +14,7 @@ export async function getCurrentTrophies() {
|
||||
const CurrentTrophy = await artemis.chuni_profile_data.findMany({
|
||||
where: {
|
||||
user: user.UserId,
|
||||
version: supportedVersionNumber,
|
||||
},
|
||||
select: {
|
||||
trophyId: true,
|
||||
@ -22,6 +23,39 @@ export async function getCurrentTrophies() {
|
||||
return CurrentTrophy;
|
||||
}
|
||||
|
||||
export async function updatePlayerTrophy(trophyId: number) {
|
||||
const { user } = await getAuth();
|
||||
|
||||
if (!user || !user.accessCode) {
|
||||
throw new Error("User is not authenticated or accessCode is missing");
|
||||
}
|
||||
|
||||
if (trophyId === undefined) {
|
||||
throw new Error("nameplateId is required");
|
||||
}
|
||||
|
||||
try {
|
||||
const updatePlayerNameplate = await artemis.chuni_profile_data.update({
|
||||
where: {
|
||||
user_version: {
|
||||
user: user.UserId,
|
||||
version: supportedVersionNumber,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
trophyId,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(updatePlayerNameplate);
|
||||
|
||||
return updatePlayerNameplate;
|
||||
} catch (error) {
|
||||
console.error("Error updating nameplate:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTrophies() {
|
||||
const { user } = await getAuth();
|
||||
|
||||
|
@ -30,7 +30,7 @@ import { z } from "zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { toast } from "../../ui/use-toast";
|
||||
import { getCurrentTrophies } from "./actions";
|
||||
import { getCurrentTrophies, updatePlayerTrophy } from "./actions";
|
||||
import { cozynet_chuni_static_trophies } from "@/prisma/schemas/artemis/generated/artemis";
|
||||
const getAvatarTextureSrc = (id: number | undefined) => {
|
||||
if (id === undefined) return "";
|
||||
@ -76,6 +76,14 @@ export const TrophyCustomization: FC<AvatarSelectionProps> = ({
|
||||
}, []);
|
||||
|
||||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||||
const unchangedNamePlateId = trophyID;
|
||||
const newNamePlateId = data.trophies ?? unchangedNamePlateId;
|
||||
|
||||
updatePlayerTrophy(newNamePlateId).then(() => {
|
||||
setTrophyId(newNamePlateId);
|
||||
});
|
||||
resetFormValues();
|
||||
|
||||
toast({
|
||||
title: "You submitted the following values:",
|
||||
description: (
|
||||
@ -84,6 +92,12 @@ export const TrophyCustomization: FC<AvatarSelectionProps> = ({
|
||||
</pre>
|
||||
),
|
||||
});
|
||||
|
||||
function resetFormValues() {
|
||||
form.reset({
|
||||
trophies: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
Reference in New Issue
Block a user