fix admin voting

- fix detail publish voting
This commit is contained in:
2025-03-24 13:53:57 +08:00
parent 5a220528d2
commit c0f35a4c96
15 changed files with 577 additions and 97 deletions

View File

@@ -1,5 +1,6 @@
import backendLogger from "@/util/backendLogger"; import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { prisma } from "@/lib";
export async function GET( export async function GET(
request: Request, request: Request,

View File

@@ -0,0 +1,67 @@
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
import { prisma } from "@/lib";
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const { id } = params;
// Validasi ID
if (!id) {
return NextResponse.json(
{
success: false,
message: "ID is required",
},
{ status: 400 }
);
}
const data = await prisma.voting_Kontributor.findMany({
where: {
votingId: id,
},
select: {
Voting_DaftarNamaVote: true,
Author: {
select: {
username: true,
Profile: true,
},
},
},
});
// Penanganan data tidak ditemukan
if (!data) {
return NextResponse.json(
{
success: false,
message: "Voting data not found",
},
{ status: 404 }
);
}
return NextResponse.json(
{
success: true,
data: data,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error fetching voting data >>", error);
return NextResponse.json(
{
success: false,
message: "Error fetching voting data",
error: error instanceof Error ? error.message : String(error),
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,63 @@
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
import { prisma } from "@/lib";
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const { id } = params;
// Validasi ID
if (!id) {
return NextResponse.json(
{
success: false,
message: "ID is required",
},
{ status: 400 }
);
}
const fixData = await prisma.voting.findUnique({
where: {
id: id,
},
include: {
Author: true,
Voting_Status: true,
Voting_DaftarNamaVote: true,
},
});
// Penanganan data tidak ditemukan
if (!fixData) {
return NextResponse.json(
{
success: false,
message: "Voting data not found",
},
{ status: 404 }
);
}
return NextResponse.json(
{
success: true,
data: fixData,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error fetching voting data >>", error);
return NextResponse.json(
{
success: false,
message: "Error fetching voting data",
error: error instanceof Error ? error.message : String(error),
},
{ status: 500 }
);
}
}

View File

@@ -3,38 +3,60 @@ import backendLogger from "@/util/backendLogger";
import _ from "lodash"; import _ from "lodash";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export async function GET(request: Request, { params }: { export async function GET(
params: { name: string } request: Request,
}) { {
params,
}: {
params: { name: string };
}
) {
const { name } = params; const { name } = params;
try { try {
let fixData; let fixData;
const fixStatus = _.startCase(name); const fixStatus = _.startCase(name);
if (fixStatus === "Publish") {
fixData = await prisma.voting.count({ fixData = await prisma.voting.count({
where: { where: {
Voting_Status: { Voting_Status: {
name: fixStatus name: fixStatus,
}, },
isArsip: false isActive: true,
isArsip: false,
akhirVote: {
gte: new Date(),
}, },
}) },
});
} else {
fixData = await prisma.voting.count({
where: {
Voting_Status: {
name: fixStatus,
},
isArsip: false,
},
});
}
return NextResponse.json({ return NextResponse.json(
{
success: true, success: true,
message: "Success get data voting dashboard", message: "Success get data voting dashboard",
data: fixData data: fixData,
}, },
{ status: 200 } { status: 200 }
) );
} catch (error) { } catch (error) {
backendLogger.error("Error get data voting dashboard >>", error); backendLogger.error("Error get data voting dashboard >>", error);
return NextResponse.json({ return NextResponse.json(
{
success: false, success: false,
message: "Error get data voting dashboard", message: "Error get data voting dashboard",
reason: (error as Error).message reason: (error as Error).message,
}, },
{ status: 500 } { status: 500 }
) );
} }
} }

View File

@@ -11,7 +11,10 @@ export async function GET(request: Request) {
Voting_Status: { Voting_Status: {
name: "Publish", name: "Publish",
}, },
isArsip: true, isActive: true,
akhirVote: {
lte: new Date(),
},
}, },
}); });

View File

@@ -0,0 +1,11 @@
import { AdminVote_DetailVoting } from "@/app_modules/admin/vote/detail/detail_voting";
async function Page() {
return (
<>
<AdminVote_DetailVoting />
</>
);
}
export default Page;

View File

@@ -22,13 +22,10 @@ import {
Stack, Stack,
Table, Table,
Text, Text,
TextInput TextInput,
} from "@mantine/core"; } from "@mantine/core";
import { useDisclosure, useShallowEffect } from "@mantine/hooks"; import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import { import { IconCircleCheckFilled, IconSearch } from "@tabler/icons-react";
IconCircleCheckFilled,
IconSearch
} from "@tabler/icons-react";
import _ from "lodash"; import _ from "lodash";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useState } from "react"; import { useState } from "react";
@@ -36,6 +33,8 @@ import ComponentAdminVote_DetailHasil from "../../component/detail_hasil";
import { AdminVote_getHasilById } from "../../fun/get/get_hasil_by_id"; import { AdminVote_getHasilById } from "../../fun/get/get_hasil_by_id";
import { AdminVote_getListKontributorById } from "../../fun/get/get_list_kontributor_by_id"; import { AdminVote_getListKontributorById } from "../../fun/get/get_list_kontributor_by_id";
import { apiGetAdminVotingByStatus } from "../../lib/api_fetch_admin_voting"; import { apiGetAdminVotingByStatus } from "../../lib/api_fetch_admin_voting";
import Admin_DetailButton from "@/app_modules/admin/_admin_global/_component/button/detail_button";
import { RouterAdminVote } from "@/lib/router_admin/router_admin_vote";
export default function AdminVote_TablePublish() { export default function AdminVote_TablePublish() {
return ( return (
@@ -109,53 +108,12 @@ function TableStatus() {
return data?.map((e, i) => ( return data?.map((e, i) => (
<tr key={i}> <tr key={i}>
<td>
<Center>
<Button
loading={
e?.id === voteId ? (loading === true ? true : false) : false
}
radius={"xl"}
color="green"
leftIcon={<IconCircleCheckFilled />}
onClick={async () => {
setVoteId(e?.id);
setLoading(true);
await new Promise((r) => setTimeout(r, 500));
onList(e?.id, setHasil, setKontributor, setLoading, open);
}}
>
Lihat Hasil
</Button>
</Center>
</td>
<td> <td>
<Center c={AccentColor.white}>{e?.Author?.username}</Center> <Center c={AccentColor.white}>{e?.Author?.username}</Center>
</td> </td>
<td> <td>
<Center c={AccentColor.white}>{e?.title}</Center> <Center c={AccentColor.white}>{e?.title}</Center>
</td> </td>
<td>
<Center c={"white"}>
<Spoiler
hideLabel="sembunyikan"
maw={400}
maxHeight={50}
showLabel="tampilkan"
>
{e?.deskripsi}
</Spoiler>
</Center>
</td>
<td>
<Stack>
{e?.Voting_DaftarNamaVote.map((v) => (
<Box key={v?.id}>
<Text c={AccentColor.white}>- {v?.value}</Text>
</Box>
))}
</Stack>
</td>
<td> <td>
<Center c={AccentColor.white}> <Center c={AccentColor.white}>
@@ -172,6 +130,11 @@ function TableStatus() {
</Center> </Center>
</td> </td>
<td>
<Center>
<Admin_DetailButton path={RouterAdminVote.detail({id: e.id})} />
</Center>
</td>
</tr> </tr>
)); ));
}; };
@@ -201,35 +164,24 @@ function TableStatus() {
) : ( ) : (
<Paper p={"md"} bg={AdminColor.softBlue} shadow="lg" h={"80vh"}> <Paper p={"md"} bg={AdminColor.softBlue} shadow="lg" h={"80vh"}>
<ScrollArea w={"100%"} h={"90%"}> <ScrollArea w={"100%"} h={"90%"}>
<Table <Table verticalSpacing={"md"} horizontalSpacing={"md"} p={"md"}>
verticalSpacing={"md"}
horizontalSpacing={"md"}
p={"md"}
w={1500}
>
<thead> <thead>
<tr> <tr>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
<th> <th>
<Center c={AccentColor.white}>Username</Center> <Center c={AccentColor.white}>Username</Center>
</th> </th>
<th> <th>
<Center c={AccentColor.white}>Judul</Center> <Center c={AccentColor.white}>Judul</Center>
</th> </th>
<th>
<Center c={AccentColor.white}>Deskripsi</Center>
</th>
<th>
<Center c={AccentColor.white}>Pilihan</Center>
</th>
<th> <th>
<Center c={AccentColor.white}>Mulai Vote</Center> <Center c={AccentColor.white}>Mulai Vote</Center>
</th> </th>
<th> <th>
<Center c={AccentColor.white}>Selesai Vote</Center> <Center c={AccentColor.white}>Selesai Vote</Center>
</th> </th>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
</tr> </tr>
</thead> </thead>
<tbody>{renderTableBody()}</tbody> <tbody>{renderTableBody()}</tbody>

View File

@@ -0,0 +1,123 @@
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import {
MODEL_VOTE_KONTRIBUTOR,
MODEL_VOTING_DAFTAR_NAMA_VOTE,
} from "@/app_modules/vote/model/interface";
import {
Badge,
Box,
Center,
Divider,
Grid,
Paper,
ScrollArea,
Stack,
Text,
Title
} from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks";
import _ from "lodash";
import { useParams } from "next/navigation";
import { useState } from "react";
import { Admin_ComponentBoxStyle } from "../../_admin_global/_component/comp_admin_boxstyle";
import { apiGetAdminKontibutorVotingById } from "../lib/api_fetch_admin_voting";
interface Props {
dataHasil: MODEL_VOTING_DAFTAR_NAMA_VOTE[];
}
export function AdminVoting_ComponentKontributorList({ dataHasil }: Props) {
const param = useParams<{ id: string }>();
const [data, setData] = useState<MODEL_VOTE_KONTRIBUTOR[] | null>(null);
useShallowEffect(() => {
handleLoadData();
}, []);
async function handleLoadData() {
try {
const response = await apiGetAdminKontibutorVotingById({ id: param.id });
if (response && response.success) {
setData(response.data);
} else {
setData(null);
}
} catch (error) {
console.log("Error get list data kontributor voting admin", error);
}
}
return (
<>
<Admin_ComponentBoxStyle style={{ height: 700 }}>
<Stack>
<Title align="center" order={5}>
Hasil Kontributor
</Title>
<Grid justify="center">
{dataHasil?.map((e: MODEL_VOTING_DAFTAR_NAMA_VOTE, i) => (
<Grid.Col span={3} key={i}>
<Stack p={"md"} align="center">
<Paper withBorder p={"xl"}>
<Text fz={30}>{e.jumlah}</Text>
</Paper>
<Text lineClamp={2} fz={"sm"}>
{e.value}
</Text>
</Stack>
</Grid.Col>
))}
</Grid>
{!data ? (
<CustomSkeleton height={100} />
) : _.isEmpty(data) ? (
<Center>
<Text fz={"xs"} fw={"bold"}>
- Tidak ada voting -
</Text>
</Center>
) : (
<Box h={450} px={"sm"}>
<ScrollArea h={430} w={"100%"} p={"sm"}>
<Stack p={"sm"}>
{data?.map((e, i) => (
<Stack spacing={"xs"} key={i}>
<Grid>
<Grid.Col span={5}>
<Stack justify="center" h={"100%"}>
<Text truncate fz={"sm"} fw={"bold"}>
{e ? e?.Author?.username : "Nama kontributor"}
</Text>
</Stack>
</Grid.Col>
<Grid.Col span={5}>
<Center>
<Badge>
<Text
truncate
fz={
e.Voting_DaftarNamaVote.value.length > 10
? 8
: 10
}
>
{e.Voting_DaftarNamaVote.value}
</Text>
</Badge>
</Center>
</Grid.Col>
</Grid>
<Divider />
</Stack>
))}
</Stack>
</ScrollArea>
</Box>
)}
</Stack>
</Admin_ComponentBoxStyle>
</>
);
}

View File

@@ -0,0 +1,22 @@
"use client";
import { SimpleGrid, Title } from "@mantine/core";
import { Admin_ComponentBoxStyle } from "../../_admin_global/_component/comp_admin_boxstyle";
import { AdminVoting_ComponentDetail } from "./detail";
import { MODEL_VOTING } from "@/app_modules/vote/model/interface";
import { AdminVoting_ComponentKontributorList } from "./comp_kontributor";
interface Props {
data: MODEL_VOTING;
}
export function AdminVoting_ComponentDetailPublish({ data }: Props) {
return (
<SimpleGrid cols={2}>
<Admin_ComponentBoxStyle style={{height: 700}}>
<AdminVoting_ComponentDetail data={data} />
</Admin_ComponentBoxStyle>
<AdminVoting_ComponentKontributorList dataHasil={data.Voting_DaftarNamaVote}/>
</SimpleGrid>
);
}

View File

@@ -0,0 +1,74 @@
"use client";
import { MODEL_VOTING } from "@/app_modules/vote/model/interface";
import { Badge, Grid, Stack, Text, Title } from "@mantine/core";
import moment from "moment";
interface Props {
data: MODEL_VOTING;
}
export function AdminVoting_ComponentDetail({ data }: Props) {
const listData = [
{
title: "Username",
value: data.Author.username,
},
{
title: "Judul",
value: data.title,
},
{
title: "Deskripsi",
value: data.deskripsi,
},
{
title: "Awal voting",
value: moment(data.awalVote).format("DD-MM-YYYY"),
},
{
title: "Akhir voting",
value: moment(data.akhirVote).format("DD-MM-YYYY"),
},
{
title: "Status",
value: <Badge variant="light">{data.Voting_Status.name}</Badge>,
},
];
return (
<>
<Stack>
{listData.map((item, index) => (
<Grid key={index}>
<Grid.Col span={3}>
<Text>{item.title}</Text>
</Grid.Col>
<Grid.Col span={1}>
<Text>:</Text>
</Grid.Col>
<Grid.Col span={"auto"}>
<Text>{item.value}</Text>
</Grid.Col>
</Grid>
))}
<Grid>
<Grid.Col span={3}>
<Text>Daftar Pilihan</Text>
</Grid.Col>
<Grid.Col span={1}>
<Text>:</Text>
</Grid.Col>
<Grid.Col span={"auto"}>
<Stack>
{data.Voting_DaftarNamaVote.map((e, i) => (
<Text key={i}>- {e.value}</Text>
))}
</Stack>
</Grid.Col>
</Grid>
</Stack>
</>
);
}

View File

@@ -0,0 +1,64 @@
"use client";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import { MODEL_VOTING } from "@/app_modules/vote/model/interface";
import { SimpleGrid, Stack } from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks";
import { useParams } from "next/navigation";
import { useState } from "react";
import AdminGlobal_ComponentBackButton from "../../_admin_global/back_button";
import ComponentAdminGlobal_HeaderTamplate from "../../_admin_global/header_tamplate";
import { AdminVoting_ComponentDetailPublish } from "../component/comp_publish";
import { apiGetOneVotingById } from "../lib/api_fetch_admin_voting";
export function AdminVote_DetailVoting() {
const param = useParams<{ id: string }>();
const [data, setData] = useState<MODEL_VOTING | null>();
useShallowEffect(() => {
handleLoadData();
}, []);
const handleLoadData = async () => {
try {
const response = await apiGetOneVotingById({
id: param.id,
});
if (response) {
setData(response.data);
} else {
setData(null);
}
} catch (error) {
console.log("Error get one data voting admin", error);
}
};
return (
<>
<Stack>
<ComponentAdminGlobal_HeaderTamplate name="Detail voting" />
<AdminGlobal_ComponentBackButton />
{data === undefined && (
<SimpleGrid cols={2}>
<CustomSkeleton height={500} />
</SimpleGrid>
)}
{data && data.voting_StatusId === "1" ? (
<AdminVoting_ComponentDetailPublish data={data} />
) : data && data.voting_StatusId === "2" ? (
"detail review"
) : data && data.voting_StatusId === "4" ? (
"detail reject"
) : (
""
)}
{/* <pre style={{ color: "white" }}>{JSON.stringify(data, null, 2)}</pre> */}
</Stack>
</>
);
}

View File

@@ -3,6 +3,8 @@ export {
apiGetAdminVoteRiwayatCount, apiGetAdminVoteRiwayatCount,
apiGetAdminVotingByStatus, apiGetAdminVotingByStatus,
apiGetAdminVotingRiwayat, apiGetAdminVotingRiwayat,
apiGetOneAdminVotingById as apiGetOneVotingById,
apiGetAdminKontibutorVotingById,
}; };
const apiGetAdminVoteStatusCountDashboard = async ({ const apiGetAdminVoteStatusCountDashboard = async ({
name, name,
@@ -87,7 +89,6 @@ const apiGetAdminVotingByStatus = async ({
} }
}; };
const apiGetAdminVotingRiwayat = async ({ const apiGetAdminVotingRiwayat = async ({
page, page,
search, search,
@@ -133,3 +134,68 @@ const apiGetAdminVotingRiwayat = async ({
} }
}; };
const apiGetOneAdminVotingById = async ({ id }: { id: string }) => {
try {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const response = await fetch(`/api/admin/vote/${id}`, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Error get one data voting admin",
errorData?.message || "Unknown error"
);
return null;
}
return response.json();
} catch (error) {
console.log("Error get one data voting admin", error);
throw error;
}
};
const apiGetAdminKontibutorVotingById = async ({ id }: { id: string }) => {
try {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const response = await fetch(`/api/admin/vote/${id}/kontributor`, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Error get one data voting admin",
errorData?.message || "Unknown error"
);
return null;
}
return response.json();
} catch (error) {
console.log("Error get one data voting admin", error);
throw error;
}
};

View File

@@ -21,3 +21,11 @@ export interface MODEL_NEW_DEFAULT_MASTER {
updatedAt: Date; updatedAt: Date;
} }
export interface IDefaultMaster {
id: string;
name: string;
isActive: boolean;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -1,4 +1,6 @@
import { MODEL_USER } from "@/app_modules/home/model/interface"; import { MODEL_USER } from "@/app_modules/home/model/interface";
import { IDefaultMaster, MODEL_NEW_DEFAULT_MASTER } from "@/app_modules/model_global/interface";
import { MODEL_DEFAULT_MASTER_OLD } from "@/app_modules/model_global/model_default_master";
export interface MODEL_VOTING { export interface MODEL_VOTING {
id: string; id: string;
@@ -14,7 +16,8 @@ export interface MODEL_VOTING {
Author: MODEL_USER; Author: MODEL_USER;
Voting_DaftarNamaVote: MODEL_VOTING_DAFTAR_NAMA_VOTE[]; Voting_DaftarNamaVote: MODEL_VOTING_DAFTAR_NAMA_VOTE[];
isArsip: boolean; isArsip: boolean;
voting_StatusId: string voting_StatusId: string;
Voting_Status: IDefaultMaster
} }
export interface MODEL_VOTING_DAFTAR_NAMA_VOTE { export interface MODEL_VOTING_DAFTAR_NAMA_VOTE {

View File

@@ -5,4 +5,5 @@ export const RouterAdminVote = {
table_review: "/dev/admin/vote/child/table_review", table_review: "/dev/admin/vote/child/table_review",
table_reject: "/dev/admin/vote/child/table_reject", table_reject: "/dev/admin/vote/child/table_reject",
riwayat: "/dev/admin/vote/child/riwayat", riwayat: "/dev/admin/vote/child/riwayat",
detail: ({ id }: { id: string }) => `/dev/admin/vote/${id}`,
}; };