Fix voting, intergarsi ke API

This commit is contained in:
2024-12-30 08:19:43 +08:00
parent bbef4050e0
commit 1c162c199a
14 changed files with 773 additions and 372 deletions

View File

@@ -0,0 +1,47 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(
request: Request,
context: { params: { id: string } }
) {
try {
let fixData;
const { id } = context.params;
fixData = await prisma.voting.findFirst({
where: {
id: id,
},
include: {
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
where: {
isActive: true,
},
},
Author: {
select: {
Profile: true,
},
},
},
});
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: fixData },
{ status: 200 }
);
} catch (error) {
backendLogger.error("Gagal mendapatkan data voting by id", error);
return NextResponse.json(
{ success: false, message: "Gagal mendapatkan data" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,76 @@
import { prisma } from "@/app/lib";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import backendLogger from "@/util/backendLogger";
import _ from "lodash";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
/**
*
* @param id | votingId
* @param kategori | kontribusi
* @returns
*/
export async function GET(request: Request) {
try {
let fixData;
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
const kategori = searchParams.get("kategori");
const userLoginId = await funGetUserIdByToken();
if (!userLoginId) {
return NextResponse.json(
{ success: false, message: "Gagal mendapatkan data, coba lagi nanti " },
{ status: 500 }
);
}
if (kategori == "isKontributor") {
const cek = await prisma.voting_Kontributor.count({
where: {
authorId: userLoginId,
votingId: id,
},
});
if (cek > 0) {
fixData = true;
} else {
fixData = false;
}
} else if (kategori == "pilihan") {
const cekPilihan = await prisma.voting_Kontributor.findFirst({
where: {
authorId: userLoginId,
votingId: id,
},
select: {
Voting_DaftarNamaVote: {
select: {
value: true,
},
},
},
});
fixData = cekPilihan?.Voting_DaftarNamaVote?.value
}
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: fixData },
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get hitung voting", error);
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan data",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,30 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(request: Request) {
try {
let fixData;
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
fixData = await prisma.voting_DaftarNamaVote.findMany({
where: {
votingId: id,
},
});
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: fixData },
{ status: 200 }
);
} catch (error) {
backendLogger.error(error);
return NextResponse.json(
{ success: false, message: "Gagal mendapatkan data" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,46 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(request: Request) {
try {
let fixData;
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
const page = searchParams.get("page");
const takeData = 10;
const dataSkip = Number(page) * takeData - takeData;
fixData = await prisma.voting_Kontributor.findMany({
// take: takeData,
// skip: dataSkip,
orderBy: {
createdAt: "desc",
},
where: {
votingId: id,
},
include: {
Author: {
include: {
Profile: true,
},
},
Voting_DaftarNamaVote: {
select: {
value: true,
},
},
},
});
return NextResponse.json({ success: true, data: fixData }, { status: 200 });
} catch (error) {
backendLogger.error(error);
return NextResponse.json(
{ success: false, reason: (error as Error).message || (error as Error) },
{ status: 500 }
);
}
}

View File

@@ -1,13 +1,9 @@
import { Voting_UiDetailKontributorVoting } from "@/app_modules/vote/_ui";
import { Vote_getListKontributorById } from "@/app_modules/vote/fun/get/get_list_kontributor_by_id";
export default async function Page({ params }: { params: { id: string } }) {
const votingId = params.id;
const listKontributor = await Vote_getListKontributorById(votingId);
export default async function Page() {
return (
<>
<Voting_UiDetailKontributorVoting listKontributor={listKontributor} />
<Voting_UiDetailKontributorVoting />
</>
);
}

View File

@@ -1,29 +1,12 @@
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { Vote_MainDetail } from "@/app_modules/vote";
import { Vote_cekKontributorById } from "@/app_modules/vote/fun/get/cek_kontributor_by_id";
import { voting_funGetOneVotingbyId } from "@/app_modules/vote/fun/get/fun_get_one_by_id";
import { Vote_getHasilVoteById } from "@/app_modules/vote/fun/get/get_list_hasil_by_id";
import { Vote_getListKontributorById } from "@/app_modules/vote/fun/get/get_list_kontributor_by_id";
import { Vote_getOnePilihanVotingByUserId } from "@/app_modules/vote/fun/get/get_one_pilihan_voting_by_user_id";
export default async function Page({ params }: { params: { id: string } }) {
const voteId = params.id;
const userLoginId = await funGetUserIdByToken();
const dataVote = await voting_funGetOneVotingbyId(voteId);
const hasilVoting = await Vote_getHasilVoteById(voteId as any);
const isKontributor = await Vote_cekKontributorById(voteId);
const pilihanKontributor = await Vote_getOnePilihanVotingByUserId(voteId);
const listKontributor = await Vote_getListKontributorById(voteId);
return (
<>
<Vote_MainDetail
dataVote={dataVote as any}
hasilVoting={hasilVoting}
isKontributor={isKontributor}
pilihanKontributor={pilihanKontributor as any}
listKontributor={listKontributor as any}
userLoginId={userLoginId as string}
/>
</>

View File

@@ -14,93 +14,46 @@ import { useState } from "react";
import { DIRECTORY_ID } from "../lib";
export default function Page() {
const [filePP, setFilePP] = useState<File | null>(null);
const [imgPP, setImgPP] = useState<any | null>();
async function onSave() {
const body = {
file: filePP,
dirId: DIRECTORY_ID.profile_foto,
};
const token =
"QWERTYUIOPLKJHGFDSAZXCVBNMQAZWSXEDCRFVTGBYHNUJMIKOLPPOIUYTREWQLKJHGFDSAMNBVCXZlghvftyguhijknhbgvcfytguu8okjnhbgvfty7u8oilkjnhgvtygu7u8ojilnkhbgvhujnkhghvjhukjnhb";
const formData = new FormData();
formData.append("file", filePP as any);
const res = await fetch("/api/image/upload", {
method: "POST",
body: formData,
});
console.log(await res.json());
}
const [data, setData] = useState({
name: "bagas",
hobi: [
{
id: "1",
name: "mancing",
},
{
id: "2",
name: "game",
},
],
});
return (
<>
<Stack>
<Center>
{imgPP ? (
<Paper shadow="lg" radius={"100%"}>
<Avatar
color={"cyan"}
sx={{
borderStyle: "solid",
borderColor: "gray",
borderWidth: "0.5px",
}}
src={imgPP ? imgPP : "/aset/global/avatar.png"}
size={150}
radius={"100%"}
/>
</Paper>
) : (
<Paper shadow="lg" radius={"100%"}>
<Avatar
variant="light"
color="blue"
size={150}
radius={"100%"}
sx={{
borderStyle: "solid",
borderColor: MainColor.darkblue,
borderWidth: "0.5px",
}}
/>
</Paper>
)}
</Center>
<Stack align="center" justify="center" h={"100vh"}>
<pre>{JSON.stringify(data, null, 2)}</pre>
<FileButton
onChange={async (files: any | null) => {
try {
const buffer = URL.createObjectURL(
new Blob([new Uint8Array(await files.arrayBuffer())])
);
setImgPP(buffer);
setFilePP(files);
} catch (error) {
console.log(error);
}
<Button
onClick={() => {
const newData = [
{
id: "1",
name: "sepedah",
},
{
id: "2",
name: "berenang",
},
];
setData({
...data,
hobi: newData,
});
}}
accept="image/png,image/jpeg"
>
{(props) => (
<Button
{...props}
radius={"xl"}
leftIcon={<IconCamera />}
bg={MainColor.yellow}
color="yellow"
c={"black"}
>
Upload
</Button>
)}
</FileButton>
<Button onClick={() => onSave()}>Upload</Button>
Ganti
</Button>
</Stack>
</>
);

View File

@@ -11,40 +11,44 @@ export default function Voting_ComponentSkeletonViewPuh() {
return (
<>
<UIGlobal_LayoutTamplate
header={<UIGlobal_LayoutHeaderTamplate title="Detail Publish" />}
header={<UIGlobal_LayoutHeaderTamplate title="Skeleton Maker" />}
>
<ComponentGlobal_CardStyles marginBottom={"0"}>
<Stack spacing={"lg"}>
<Grid align="center">
<Grid.Col span={"content"}>
<Skeleton circle height={40} />
</Grid.Col>
<Grid.Col span={4}>
<Skeleton height={20} w={150} />
</Grid.Col>
</Grid>
<Stack align="center">
<Skeleton height={20} w={150} />
<Skeleton height={20} w={300} />
<Stack>
<ComponentGlobal_CardStyles marginBottom={"0"}>
<Stack spacing={"xl"}>
<Grid align="center" gutter={"md"}>
<Grid.Col span={"content"}>
<Skeleton circle height={40} />
</Grid.Col>
<Grid.Col span={3}>
<Skeleton height={20} w={150} />
</Grid.Col>
<Grid.Col span={3} offset={3}>
<Skeleton height={20} w={150} />
</Grid.Col>
</Grid>
</Stack>
</ComponentGlobal_CardStyles>
<Group position="center" spacing={100}>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
</Group>
{/* <ComponentGlobal_CardStyles>
<Stack>
<Center>
<Skeleton h={20} w={"30%"} />
</Center>
<Stack align="center">
<Skeleton height={15} w={50} /> <Skeleton height={20} w={50} />
<Group position="center" spacing={50}>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
</Group>
</Stack>
</Stack>
</ComponentGlobal_CardStyles>
</ComponentGlobal_CardStyles> */}
</Stack>
</UIGlobal_LayoutTamplate>
</>
);