fix voting admin:

deskripsi:
- fix voting table review
This commit is contained in:
2025-02-24 16:55:34 +08:00
parent 8f80419eb2
commit f2d3930bd1
6 changed files with 771 additions and 724 deletions

View File

@@ -4,152 +4,215 @@ import _ from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";
export async function GET(request: Request,
{ params }: { params: { name: string } }
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;
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;
try {
let fixData;
const fixStatus = _.startCase(name);
try {
let fixData;
const fixStatus = _.startCase(name);
if (!page) {
fixData = await prisma.voting.findMany({
orderBy: {
createdAt: "desc",
},
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
include: {
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
},
},
},
},
Voting_Kontributor: true,
Voting_DaftarNamaVote: true,
},
});
} else {
if (fixStatus === "Publish") {
const getAllData = await prisma.voting.findMany({
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
isArsip: false,
akhirVote: {
gte: new Date(),
},
},
});
if (!page) {
fixData = await prisma.voting.findMany({
orderBy: {
createdAt: "desc",
},
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
include: {
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
},
},
},
},
Voting_Kontributor: true,
Voting_DaftarNamaVote: true,
},
for (let i of getAllData) {
if (moment(i.akhirVote).diff(moment(), "minutes") < 0) {
await prisma.event.update({
where: {
id: i.id,
},
data: {
isArsip: true,
},
});
} else {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
include: {
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
},
},
},
},
Voting_Kontributor: true,
Voting_DaftarNamaVote: true,
},
});
if (fixStatus === "Publish") {
const data = await prisma.voting.findMany({
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
});
for (let i of data) {
if (moment(i.akhirVote).diff(moment(), "minutes") < 0) {
await prisma.event.update({
where: {
id: i.id,
},
data: {
isArsip: true,
},
});
}
}
const nCount = await prisma.voting.count({
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
},
});
fixData = {
data: data,
count: _.ceil(nCount / takeData)
}
}
}
}
return NextResponse.json({
success: true,
message: "Success get data voting dashboard",
data: fixData
},
{ status: 200 }
)
} catch (error) {
backendLogger.error("Error get data voting dashboard >>", error);
return NextResponse.json({
success: false,
message: "Error get data voting dashboard",
reason: (error as Error).message
},
{ status: 500 }
)
const data = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
akhirVote: {
gte: new Date(),
},
isArsip: false,
},
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_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
akhirVote: {
gte: new Date(),
},
isArsip: false,
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / takeData),
};
} else {
const data = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
Voting_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
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_Status: {
name: fixStatus,
},
isActive: true,
title: {
contains: search ? search : "",
mode: "insensitive",
},
isArsip: false,
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / takeData),
};
}
}
}
return NextResponse.json(
{
success: true,
message: "Success get data voting status",
data: fixData,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get data voting status ", error);
return NextResponse.json(
{
success: false,
message: "Error get data voting status",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}

View File

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

View File

@@ -1,12 +1,9 @@
import { AdminVote_TableReview } from "@/app_modules/admin/vote";
import { adminVote_funGetListReview } from "@/app_modules/admin/vote/fun";
export default async function Page() {
const listVote = await adminVote_funGetListReview({ page: 1 });
return (
<>
<AdminVote_TableReview listVote={listVote as any} />
<AdminVote_TableReview />
</>
);
}

View File

@@ -18,8 +18,12 @@ import {
TextInput,
Title,
} from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
import { IconCircleCheckFilled, IconEyeCheck, IconSearch } from "@tabler/icons-react";
import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import {
IconCircleCheckFilled,
IconEyeCheck,
IconSearch,
} from "@tabler/icons-react";
import { useRouter } from "next/navigation";
import { useState } from "react";
import ComponentAdminVote_DetailHasil from "../../component/detail_hasil";
@@ -28,200 +32,224 @@ import { AdminVote_getListKontributorById } from "../../fun/get/get_list_kontrib
import { adminVote_funGetListPublish } from "../../fun/get/status/get_list_publish";
import { ComponentAdminGlobal_TitlePage } from "@/app_modules/admin/_admin_global/_component";
import { MainColor } from "@/app_modules/_global/color";
import { AccentColor, AdminColor } from "@/app_modules/_global/color/color_pallet";
import {
AccentColor,
AdminColor,
} from "@/app_modules/_global/color/color_pallet";
import { clientLogger } from "@/util/clientLogger";
import { apiGetAdminVotingByStatus } from "../../lib/api_fetch_admin_voting";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import _ from "lodash";
import ComponentAdminGlobal_IsEmptyData from "@/app_modules/admin/_admin_global/is_empty_data";
export default function AdminVote_TablePublish({
dataVote,
}: {
dataVote: any;
}) {
export default function AdminVote_TablePublish() {
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 apiGetAdminVotingByStatus({
name: "Publish",
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_funGetListPublish({
page: 1,
search: s,
});
setData(loadData.data as any);
setNPage(loadData.nPage);
}
async function onPageClick(p: any) {
setActivePage(p);
const loadData = await adminVote_funGetListPublish({
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={<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>
<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={<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>
<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>
<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}>
{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 (
<>
<Stack spacing={"xs"} h={"100%"}>
{/* <pre>{JSON.stringify(listUser, null, 2)}</pre> */}
<ComponentAdminGlobal_TitlePage
name="Publish"
color={AdminColor.softBlue}
component={
<TextInput
icon={<IconSearch size={20} />}
radius={"xl"}
placeholder="Masukan judul"
onChange={(val) => {
onSearch(val.currentTarget.value);
}}
/>
}
/>
{/* <Group
position="apart"
bg={"green.4"}
p={"xs"}
style={{ borderRadius: "6px" }}
>
<Title order={4}>Publish</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

@@ -48,28 +48,33 @@ import { AdminVote_funEditStatusPublishById } from "../../fun/edit/fun_edit_stat
import { AdminEvent_funEditCatatanById } from "../../fun/edit/fun_edit_status_reject_by_id";
import { ComponentAdminGlobal_TitlePage } from "@/app_modules/admin/_admin_global/_component";
import { AdminColor } from "@/app_modules/_global/color/color_pallet";
import { clientLogger } from "@/util/clientLogger";
import { apiGetAdminVotingByStatus } from "../../lib/api_fetch_admin_voting";
import _ from "lodash";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import ComponentAdminGlobal_IsEmptyData from "@/app_modules/admin/_admin_global/is_empty_data";
export default function AdminVote_TableReview({
listVote,
}: {
listVote: MODEL_VOTING[];
}) {
export default function AdminVote_TableReview() {
return (
<>
<Stack>
<ComponentAdminGlobal_HeaderTamplate name="Voting" />
<TableStatus listData={listVote} />
<TableStatus />
</Stack>
</>
);
}
function TableStatus({ listData }: { listData: any }) {
const [openedReject, { open: openReject, close: closeReject }] = useDisclosure(false);
const [openedPublish, { open: openPublish, close: closePublish }] = useDisclosure(false);
const [data, setData] = useState<MODEL_VOTING[]>(listData.data);
const [isNPage, setNPage] = useState(listData.nPage);
const [votingId, setVotingId] = useState("");
function TableStatus() {
const [openedReject, { open: openReject, close: closeReject }] =
useDisclosure(false);
const [openedPublish, { open: openPublish, close: closePublish }] =
useDisclosure(false);
const [data, setData] = useState<MODEL_VOTING[] | null>(null);
const [nPage, setNPage] = useState(1);
const [dataId, setDataId] = useState("");
const [tanggalMulai, setTanggalMulai] = useState<Date>();
const [catatan, setCatatan] = useState("");
const [isLoadingPublish, setLoadingPublish] = useState(false);
const [isSaveLoading, setSaveLoading] = useState(false);
@@ -90,11 +95,33 @@ function TableStatus({ listData }: { listData: any }) {
}
}, [isAdminVoting_TriggerReview, setIsShowReload]);
async function onLoadData() {
const loadData = await adminVote_funGetListReview({ page: 1 });
useShallowEffect(() => {
handleLoadData();
}, [isActivePage, isSearch]);
setData(loadData.data as any);
setNPage(loadData.nPage);
const handleLoadData = async () => {
try {
const response = await apiGetAdminVotingByStatus({
name: "Review",
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 onLoadData() {
handleLoadData();
setIsLoading(false);
setIsShowReload(false);
setIsAdminVoting_TriggerReview(false);
@@ -102,114 +129,197 @@ function TableStatus({ listData }: { listData: any }) {
async function onSearch(s: string) {
setSearch(s);
const loadData = await adminVote_funGetListReview({
page: 1,
search: s,
});
setData(loadData.data as any);
setNPage(loadData.nPage);
}
async function onPageClick(p: any) {
setActivePage(p);
const loadData = await adminVote_funGetListReview({
search: isSearch,
page: p,
});
setData(loadData.data as any);
setNPage(loadData.nPage);
}
const TableRows = data.map((e, i) => (
<tr key={i}>
<td>
<Center c={AccentColor.white}>{e?.Author?.username}</Center>
</td>
<td>
<Center c={AccentColor.white}>{e.title}</Center>
</td>
<td>
<Center c={AccentColor.white}>
<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>
async function onReject() {
const data = {
id: dataId,
catatan: catatan,
};
<td>
<Stack align="center">
<Button
// loaderPosition="center"
// loading={
// e?.id === votingId ? (isLoadingPublish ? true : false) : false
// }
w={120}
color={"green"}
leftIcon={<IconCircleCheck />}
radius={"xl"}
onClick={() => {
openPublish();
setVotingId(e.id);
}
// onPublish({
// // voteId: e.id,
// // awalVote: e.awalVote,
// // setLoadingPublish: setLoadingPublish,
// // setVotingId: setVotingId,
// // setData(val) {
// // setData(val.data);
// // setNPage(val.nPage);
// // },
// })
}
>
Publish
</Button>
<Button
w={120}
color={"red"}
leftIcon={<IconBan />}
radius={"xl"}
onClick={() => {
openReject();
setVotingId(e.id);
}}
>
Reject
</Button>
</Stack>
</td>
</tr>
));
try {
setSaveLoading(true);
const res = await AdminEvent_funEditCatatanById(data as any);
if (res.status === 200) {
const dataNotifikasi: IRealtimeData = {
appId: res.data?.id as string,
status: res.data?.Voting_Status?.name as any,
userId: res.data?.authorId as any,
pesan: res.data?.title as any,
kategoriApp: "VOTING",
title: "Voting anda di tolak !",
};
const notif = await adminNotifikasi_funCreateToUser({
data: dataNotifikasi as any,
});
if (notif.status === 201) {
WibuRealtime.setData({
type: "notification",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
}
handleLoadData();
ComponentGlobal_NotifikasiBerhasil(res.message);
} else {
ComponentGlobal_NotifikasiGagal(res.message);
}
} catch (error) {
console.error("Error update voting admin", error);
} finally {
closeReject();
setSaveLoading(false);
}
}
async function onPublish() {
const hariIni = new Date();
const cekHari = moment(tanggalMulai).diff(hariIni, "days");
if (cekHari < 0)
return ComponentGlobal_NotifikasiPeringatan("Tanggal Voting Lewat");
try {
setLoadingPublish(true);
const res = await AdminVote_funEditStatusPublishById(dataId);
if (res.status === 200) {
const dataNotifikasi: IRealtimeData = {
appId: res.data?.id as string,
status: res.data?.Voting_Status?.name as any,
userId: res.data?.authorId as any,
pesan: res.data?.title as any,
kategoriApp: "VOTING",
title: "Voting publish",
};
const notif = await adminNotifikasi_funCreateToUser({
data: dataNotifikasi as any,
});
if (notif.status === 201) {
WibuRealtime.setData({
type: "notification",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
WibuRealtime.setData({
type: "trigger",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
}
handleLoadData();
ComponentGlobal_NotifikasiBerhasil(res.message);
closePublish()
} else {
ComponentGlobal_NotifikasiGagal(res.message);
}
} catch (error) {
console.log("Error get data voting", error);
} finally {
setLoadingPublish(false);
}
}
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 c={AccentColor.white}>{e?.Author?.username}</Center>
</td>
<td>
<Center c={AccentColor.white}>{e.title}</Center>
</td>
<td>
<Center c={AccentColor.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>
<td>
<Stack align="center">
<Button
w={120}
color={"green"}
leftIcon={<IconCircleCheck />}
radius={"xl"}
onClick={() => {
openPublish();
setDataId(e.id);
setTanggalMulai(e.awalVote);
}}
>
Publish
</Button>
<Button
w={120}
color={"red"}
leftIcon={<IconBan />}
radius={"xl"}
onClick={() => {
openReject();
setDataId(e.id);
}}
>
Reject
</Button>
</Stack>
</td>
</tr>
));
};
return (
<>
<Stack spacing={"xs"} h={"100%"}>
{/* <pre>{JSON.stringify(listUser, null, 2)}</pre> */}
<ComponentAdminGlobal_TitlePage
name="Review"
color={AdminColor.softBlue}
@@ -224,94 +334,82 @@ function TableStatus({ listData }: { listData: any }) {
/>
}
/>
{/* <Group
position="apart"
bg={"orange.4"}
p={"xs"}
style={{ borderRadius: "6px" }}
>
<Title order={4}>Review</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"}>
{isShowReload && (
<Affix position={{ top: rem(200) }} w={"100%"}>
<Center>
<Button
style={{
transition: "0.5s",
border: `1px solid ${AccentColor.skyblue}`,
}}
bg={AccentColor.blue}
loaderPosition="center"
loading={isLoading}
radius={"xl"}
opacity={0.8}
onClick={() => onLoadData()}
leftIcon={<IconRefresh />}
>
Update Data
</Button>
</Center>
</Affix>
)}
{!data ? (
<CustomSkeleton height={"80vh"} width="100%" />
) : _.isEmpty(data) ? (
<ComponentAdminGlobal_IsEmptyData />
) : (
<Paper p={"md"} bg={AdminColor.softBlue} shadow="lg" h={"80vh"}>
{isShowReload && (
<Affix position={{ top: rem(200) }} w={"100%"}>
<Center>
<Button
style={{
transition: "0.5s",
border: `1px solid ${AccentColor.skyblue}`,
}}
bg={AccentColor.blue}
loaderPosition="center"
loading={isLoading}
radius={"xl"}
opacity={0.8}
onClick={() => onLoadData()}
leftIcon={<IconRefresh />}
>
Update Data
</Button>
</Center>
</Affix>
)}
<ScrollArea w={"100%"} h={"90%"}>
<Table
verticalSpacing={"md"}
horizontalSpacing={"md"}
p={"md"}
w={1500}
>
<thead>
<tr>
<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>
<ScrollArea w={"100%"} h={"90%"}>
<Table
verticalSpacing={"md"}
horizontalSpacing={"md"}
p={"md"}
w={1500}
>
<thead>
<tr>
<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>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
</tr>
</thead>
<tbody>{renderTableBody()}</tbody>
</Table>
</ScrollArea>
<th>
<Center c={AccentColor.white}>Aksi</Center>
</th>
</tr>
</thead>
<tbody>{TableRows}</tbody>
</Table>
</ScrollArea>
<Center mt={"xl"}>
<Pagination
value={isActivePage}
total={isNPage}
onChange={(val) => {
onPageClick(val);
}}
/>
</Center>
</Paper>
<Center mt={"xl"}>
<Pagination
value={isActivePage}
total={nPage}
onChange={(val) => {
onPageClick(val);
}}
/>
</Center>
</Paper>
)}
</Stack>
<Modal
@@ -342,23 +440,10 @@ function TableStatus({ listData }: { listData: any }) {
loading={isSaveLoading ? true : false}
radius={"xl"}
onClick={() => {
onReject({
catatan: catatan,
voteId: votingId,
setData(val) {
setData(val.data);
setNPage(val.nPage);
},
close: () => {
closeReject();
},
setSaveLoading(val) {
setSaveLoading(val);
},
});
onReject();
}}
style={{
backgroundColor: MainColor.green
style={{
backgroundColor: MainColor.green,
}}
>
Simpan
@@ -384,23 +469,7 @@ function TableStatus({ listData }: { listData: any }) {
loading={isLoadingPublish ? true : false}
radius={"xl"}
onClick={() => {
onPublish({
awalVote: data[0].awalVote,
voteId: votingId,
setData(val) {
setData(val.data);
setNPage(val.nPage);
},
close: () => {
closePublish();
},
setLoadingPublish: (val) => {
setLoadingPublish(val);
},
setVotingId: (val) => {
setVotingId(val);
},
});
onPublish();
}}
style={{
backgroundColor: MainColor.green,
@@ -414,132 +483,3 @@ function TableStatus({ listData }: { listData: any }) {
</>
);
}
async function onPublish({
close,
voteId,
setData,
awalVote,
setLoadingPublish,
setVotingId,
}: {
close: any,
voteId: string;
setData: (val: { data: any[]; nPage: number }) => void;
awalVote: Date;
setLoadingPublish: (val: boolean) => void;
setVotingId: (val: string) => void;
}) {
const hariIni = new Date();
const cekHari = moment(awalVote).diff(hariIni, "days");
if (cekHari < 0)
return ComponentGlobal_NotifikasiPeringatan("Tanggal Voting Lewat");
setVotingId(voteId);
const res = await AdminVote_funEditStatusPublishById(voteId);
if (res.status === 200) {
setLoadingPublish(true);
const dataNotifikasi: IRealtimeData = {
appId: res.data?.id as string,
status: res.data?.Voting_Status?.name as any,
userId: res.data?.authorId as any,
pesan: res.data?.title as any,
kategoriApp: "VOTING",
title: "Voting publish",
};
const notif = await adminNotifikasi_funCreateToUser({
data: dataNotifikasi as any,
});
if (notif.status === 201) {
WibuRealtime.setData({
type: "notification",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
WibuRealtime.setData({
type: "trigger",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
}
const loadData = await adminVote_funGetListReview({ page: 1 });
setData({
data: loadData.data,
nPage: loadData.nPage,
});
ComponentGlobal_NotifikasiBerhasil(res.message);
setLoadingPublish(false);
} else {
ComponentGlobal_NotifikasiGagal(res.message);
}
}
async function onReject({
voteId,
catatan,
close,
setSaveLoading,
setData,
}: {
voteId: string;
catatan: string;
close: any;
setSaveLoading: (val: boolean) => void;
setData: (val: { data: any[]; nPage: number }) => void;
}) {
const data = {
id: voteId,
catatan: catatan,
};
const res = await AdminEvent_funEditCatatanById(data as any);
if (res.status === 200) {
setSaveLoading(true);
// const dataNotif = {
// appId: res.data?.id,
// status: res.data?.Voting_Status?.name as any,
// userId: res.data?.authorId as any,
// pesan: res.data?.title as any,
// kategoriApp: "VOTING",
// title: "Voting anda di tolak !",
// };
const dataNotifikasi: IRealtimeData = {
appId: res.data?.id as string,
status: res.data?.Voting_Status?.name as any,
userId: res.data?.authorId as any,
pesan: res.data?.title as any,
kategoriApp: "VOTING",
title: "Voting anda di tolak !",
};
const notif = await adminNotifikasi_funCreateToUser({
data: dataNotifikasi as any,
});
if (notif.status === 201) {
WibuRealtime.setData({
type: "notification",
pushNotificationTo: "USER",
dataMessage: dataNotifikasi,
});
}
const loadData = await adminVote_funGetListReview({ page: 1 });
setData({
data: loadData.data,
nPage: loadData.nPage,
});
setSaveLoading(false);
ComponentGlobal_NotifikasiBerhasil(res.message);
close();
} else {
ComponentGlobal_NotifikasiGagal(res.message);
}
}

View File

@@ -1,64 +1,87 @@
export {
apiGetAdminVoteStatusCountDashboard,
apiGetAdminVoteRiwayatCount,
apiGetAdminVotingByStatus
}
const apiGetAdminVoteStatusCountDashboard = async ({ name }: {
name: "Publish" | "Review" | "Reject";
apiGetAdminVoteStatusCountDashboard,
apiGetAdminVoteRiwayatCount,
apiGetAdminVotingByStatus,
};
const apiGetAdminVoteStatusCountDashboard = async ({
name,
}: {
name: "Publish" | "Review" | "Reject";
}) => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const response = await fetch(`/api/admin/vote/dashboard/${name}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
return await response.json().catch(() => null);
};
const response = await fetch(`/api/admin/vote/dashboard/${name}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
}
});
return await response.json().catch(() => null);
}
const apiGetAdminVoteRiwayatCount = async () => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const response = await fetch(`/api/admin/vote/dashboard/riwayat`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
}
});
return await response.json().catch(() => null);
}
const response = await fetch(`/api/admin/vote/dashboard/riwayat`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
return await response.json().catch(() => null);
};
const apiGetAdminVotingByStatus = async ({
name,
page,
search }: {
name: "Publish" | "Review" | "Reject";
page: string;
search: string;
}) => {
const apiGetAdminVotingByStatus = async ({
name,
page,
search,
}: {
name: "Publish" | "Review" | "Reject";
page: string;
search: string;
}) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
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/${name}${isPage}${isSearch}`,
{
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`
}
}
)
return await response.json().catch(() => null);
}
`/api/admin/vote/status/${name}${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;
}
};