fix admin voting

deksripsi:
- fix table reject, review dan riwayat
This commit is contained in:
2025-02-25 10:41:45 +08:00
parent 03471c2a3a
commit 38e04ff27f
7 changed files with 365 additions and 176 deletions

View File

@@ -8,15 +8,16 @@ export async function GET(
request: Request,
{ params }: { params: { name: string } }
) {
const { name } = params;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const page = searchParams.get("page");
const takeData = 2;
const skipData = Number(page) * takeData - takeData;
try {
let fixData;
const { name } = params;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const page = searchParams.get("page");
const takeData = 10
const skipData = Number(page) * takeData - takeData;
const fixStatus = _.startCase(name);
if (!page) {

View File

@@ -0,0 +1,123 @@
import { prisma } from "@/lib";
import backendLogger from "@/util/backendLogger";
import _ from "lodash";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
try {
let fixData;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const page = searchParams.get("page");
const takeData = 2;
const skipData = Number(page) * takeData - takeData;
if (!page) {
fixData = await prisma.voting.findMany({
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: "1",
isActive: true,
akhirVote: {
lte: new Date(),
},
title: {
contains: search ? search : "",
mode: "insensitive",
},
},
include: {
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
},
},
},
},
Voting_Kontributor: true,
Voting_DaftarNamaVote: true,
},
});
} else {
const data = await prisma.voting.findMany({
skip: skipData,
take: takeData,
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: "1",
isActive: true,
akhirVote: {
lte: new Date(),
},
title: {
contains: search ? search : "",
mode: "insensitive",
},
},
include: {
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
},
},
},
},
Voting_Kontributor: true,
Voting_DaftarNamaVote: true,
},
});
const nCount = await prisma.voting.count({
where: {
voting_StatusId: "1",
isActive: true,
akhirVote: {
lte: new Date(),
},
title: {
contains: search ? search : "",
mode: "insensitive",
},
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / takeData),
};
}
return NextResponse.json(
{
success: true,
message: "Success get data voting riwayat",
data: fixData,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get data voting riwayat ", error);
return NextResponse.json(
{
success: false,
message: "Error get data voting riwayat",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}

View File

@@ -1,13 +1,9 @@
import { AdminVote_Riwayat } from "@/app_modules/admin/vote";
import { adminVote_funGetListRiwayat } from "@/app_modules/admin/vote/fun";
import { AdminVote_getListTableByStatusId } from "@/app_modules/admin/vote/fun/get/get_list_table_by_status_id";
export default async function Page() {
const dataVote = await adminVote_funGetListRiwayat({page: 1});
return (
<>
<AdminVote_Riwayat dataVote={dataVote as any} />
<AdminVote_Riwayat />
</>
);
}

View File

@@ -14,130 +14,161 @@ import {
Stack,
Table,
Text,
TextInput
TextInput,
} from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import { IconReportAnalytics, IconSearch } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
import { ComponentAdminGlobal_TitlePage } from "@/app_modules/admin/_admin_global/_component";
import { useState } from "react";
import ComponentAdminVote_DetailHasil from "../../component/detail_hasil";
import { adminVote_funGetListRiwayat } from "../../fun";
import { AdminVote_getHasilById } from "../../fun/get/get_hasil_by_id";
import { AdminVote_getListKontributorById } from "../../fun/get/get_list_kontributor_by_id";
import { AccentColor } from "@/app_modules/_global/color";
import { AdminColor } from "@/app_modules/_global/color/color_pallet";
import { ComponentAdminGlobal_TitlePage } from "@/app_modules/admin/_admin_global/_component";
import ComponentAdminGlobal_IsEmptyData from "@/app_modules/admin/_admin_global/is_empty_data";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import { clientLogger } from "@/util/clientLogger";
import _ from "lodash";
import { useState } from "react";
import ComponentAdminVote_DetailHasil from "../../component/detail_hasil";
import { AdminVote_getHasilById } from "../../fun/get/get_hasil_by_id";
import { AdminVote_getListKontributorById } from "../../fun/get/get_list_kontributor_by_id";
import { apiGetAdminVotingRiwayat } from "../../lib/api_fetch_admin_voting";
export default function AdminVote_Riwayat({
dataVote,
}: {
dataVote: MODEL_VOTING[];
}) {
export default function AdminVote_Riwayat() {
return (
<>
<Stack>
<ComponentAdminGlobal_HeaderTamplate name="Voting" />
<TableStatus listPublish={dataVote} />
<TableStatus />
</Stack>
</>
);
}
function TableStatus({ listPublish }: { listPublish: any }) {
function TableStatus() {
const router = useRouter();
const [opened, { open, close }] = useDisclosure(false);
const [data, setData] = useState<MODEL_VOTING[]>(listPublish.data);
const [hasil, setHasil] = useState<any[]>();
const [kontributor, setKontributor] = useState<any[]>();
const [voteId, setVoteId] = useState("");
const [loading, setLoading] = useState(false);
const [isNPage, setNPage] = useState(listPublish.nPage);
const [data, setData] = useState<MODEL_VOTING[] | null>(null);
const [isNPage, setNPage] = useState(1);
const [isActivePage, setActivePage] = useState(1);
const [isSearch, setSearch] = useState("");
useShallowEffect(() => {
handleLoadData();
}, [isActivePage, isSearch]);
const handleLoadData = async () => {
try {
const response = await apiGetAdminVotingRiwayat({
page: `${isActivePage}`,
search: isSearch,
});
if (response?.success && response?.data?.data) {
setData(response.data.data);
setNPage(response.data.nPage || 1);
} else {
console.error("Invalid data format received:", response);
setData([]);
}
} catch (error) {
clientLogger.error("Error get data table publish", error);
setData([]);
}
};
async function onSearch(s: string) {
setSearch(s);
const loadData = await adminVote_funGetListRiwayat({
page: 1,
search: s,
});
setData(loadData.data as any);
setNPage(loadData.nPage);
}
async function onPageClick(p: any) {
setActivePage(p);
const loadData = await adminVote_funGetListRiwayat({
search: isSearch,
page: p,
});
setData(loadData.data as any);
setNPage(loadData.nPage);
}
const TableRows = data.map((e, i) => (
<tr key={i}>
<td>
<Center>
<Button
loading={
e?.id === voteId ? (loading === true ? true : false) : false
}
radius={"xl"}
color="green"
leftIcon={<IconReportAnalytics />}
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>
<Center c={AccentColor.white}>{e?.Author?.username}</Center>
</td>
<td>
<Center c={AccentColor.white}>{e?.title}</Center>
</td>
<td>
<Center>
<Spoiler
hideLabel="sembunyikan"
maw={400}
maxHeight={50}
showLabel="tampilkan"
>
{e?.deskripsi}
</Spoiler>
</Center>
</td>
<th>
<Stack>
{e?.Voting_DaftarNamaVote.map((v) => (
<Box key={v?.id}>
<Text c={AccentColor.white}>- {v?.value}</Text>
</Box>
))}
</Stack>
</th>
<td>
<Center c={AccentColor.white}>
{e?.awalVote.toLocaleDateString("id-ID", { dateStyle: "long" })}
</Center>
</td>
<td>
<Center c={AccentColor.white}>
{e?.akhirVote.toLocaleDateString("id-ID", { dateStyle: "long" })}
</Center>
</td>
</tr>
));
const renderTableBody = () => {
if (!Array.isArray(data) || data.length === 0) {
return (
<tr>
<td colSpan={12}>
<Center>
<Text color={"gray"}>Tidak ada data</Text>
</Center>
</td>
</tr>
);
}
return data.map((e, i) => (
<tr key={i}>
<td>
<Center>
<Button
loading={
e?.id === voteId ? (loading === true ? true : false) : false
}
radius={"xl"}
color="green"
leftIcon={<IconReportAnalytics />}
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>
<Center c={AccentColor.white}>{e?.Author?.username}</Center>
</td>
<td>
<Center c={AccentColor.white}>{e?.title}</Center>
</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>
<Center c={AccentColor.white}>
{new Intl.DateTimeFormat("id-ID", {
dateStyle: "long",
}).format(new Date(e?.awalVote))}
</Center>
</td>
<td>
<Center c={AccentColor.white}>
{new Intl.DateTimeFormat("id-ID", {
dateStyle: "long",
}).format(new Date(e?.akhirVote))}
</Center>
</td>
</tr>
));
};
return (
<>
@@ -148,80 +179,69 @@ function TableStatus({ listPublish }: { listPublish: any }) {
color={AdminColor.softBlue}
component={
<TextInput
icon={<IconSearch size={20} />}
radius={"xl"}
placeholder="Masukan judul"
onChange={(val) => {
onSearch(val.currentTarget.value);
}}
/>
}
/>
{/* <Group
position="apart"
bg={"gray.4"}
p={"xs"}
style={{ borderRadius: "6px" }}
>
<Title order={4}>Riwayat</Title>
<TextInput
icon={<IconSearch size={20} />}
radius={"xl"}
placeholder="Masukan judul"
onChange={(val) => {
onSearch(val.currentTarget.value);
}}
/>
</Group> */}
<Paper p={"md"} bg={AdminColor.softBlue} shadow="lg" h={"80vh"}>
<ScrollArea w={"100%"} h={"90%"}>
<Table
verticalSpacing={"md"}
horizontalSpacing={"md"}
p={"md"}
w={1500}
>
<thead>
<tr>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
<th>
<Center c={AccentColor.white}>Username</Center>
</th>
<th>
<Center c={AccentColor.white}>Judul</Center>
</th>
<th>
<Center c={AccentColor.white}>Deskripsi</Center>
</th>
<th>
<Center c={AccentColor.white}>Pilihan</Center>
</th>
<th>
<Center c={AccentColor.white}>Mulai Vote</Center>
</th>
<th>
<Center c={AccentColor.white}>Selesai Vote</Center>
</th>
</tr>
</thead>
<tbody>{TableRows}</tbody>
</Table>
</ScrollArea>
<Center mt={"xl"}>
<Pagination
value={isActivePage}
total={isNPage}
icon={<IconSearch size={20} />}
radius={"xl"}
placeholder="Masukan judul"
onChange={(val) => {
onPageClick(val);
onSearch(val.currentTarget.value);
}}
/>
</Center>
</Paper>
}
/>
{!data ? (
<CustomSkeleton height={"80vh"} width="100%" />
) : _.isEmpty(data) ? (
<ComponentAdminGlobal_IsEmptyData />
) : (
<Paper p={"md"} bg={AdminColor.softBlue} shadow="lg" h={"80vh"}>
<ScrollArea w={"100%"} h={"90%"}>
<Table
verticalSpacing={"md"}
horizontalSpacing={"md"}
p={"md"}
w={1500}
>
<thead>
<tr>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
<th>
<Center c={AccentColor.white}>Username</Center>
</th>
<th>
<Center c={AccentColor.white}>Judul</Center>
</th>
<th>
<Center c={AccentColor.white}>Deskripsi</Center>
</th>
<th>
<Center c={AccentColor.white}>Pilihan</Center>
</th>
<th>
<Center c={AccentColor.white}>Mulai Vote</Center>
</th>
<th>
<Center c={AccentColor.white}>Selesai Vote</Center>
</th>
</tr>
</thead>
<tbody>{renderTableBody()}</tbody>
</Table>
</ScrollArea>
<Center mt={"xl"}>
<Pagination
value={isActivePage}
total={isNPage}
onChange={(val) => {
onPageClick(val);
}}
/>
</Center>
</Paper>
)}
</Stack>
<Modal

View File

@@ -152,7 +152,7 @@ function TableStatus() {
</Spoiler>
</Center>
</td>
<th>
<td>
<Stack>
{e?.Voting_DaftarNamaVote.map((v) => (
<Box key={v?.id}>
@@ -160,7 +160,8 @@ function TableStatus() {
</Box>
))}
</Stack>
</th>
</td>
<td>
<Center c={AccentColor.white}>
{new Intl.DateTimeFormat("id-ID", {
@@ -175,6 +176,7 @@ function TableStatus() {
}).format(new Date(e?.akhirVote))}
</Center>
</td>
</tr>
));
};

View File

@@ -33,7 +33,6 @@ import { IconBan, IconSearch } from "@tabler/icons-react";
import _ from "lodash";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { adminVote_funGetListReject } from "../../fun";
import { AdminVote_funEditCatatanRejectById } from "../../fun/edit/fun_edit_catatan_reject_by_id";
import { apiGetAdminVotingByStatus } from "../../lib/api_fetch_admin_voting";

View File

@@ -2,6 +2,7 @@ export {
apiGetAdminVoteStatusCountDashboard,
apiGetAdminVoteRiwayatCount,
apiGetAdminVotingByStatus,
apiGetAdminVotingRiwayat,
};
const apiGetAdminVoteStatusCountDashboard = async ({
name,
@@ -85,3 +86,50 @@ const apiGetAdminVotingByStatus = async ({
throw error;
}
};
const apiGetAdminVotingRiwayat = async ({
page,
search,
}: {
page: string;
search: string;
}) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const isPage = page ? `?page=${page}` : "";
const isSearch = search ? `&search=${search}` : "";
const response = await fetch(
`/api/admin/vote/status/riwayat${isPage}${isSearch}`,
{
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 data voting admin",
errorData?.message || "Unknown error"
);
return null;
}
return response.json();
} catch (error) {
console.log("Error get data voting admin", error);
throw error;
}
};