Fix notifikasi

Deskripsi:
- Ganti get notifikasi dengan API
This commit is contained in:
2024-12-17 22:40:38 +08:00
parent 3bd2f53732
commit 2fa3f22f9c
13 changed files with 208 additions and 89 deletions

View File

@@ -0,0 +1,63 @@
import { prisma } from "@/app/lib";
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { ICategoryapp } from "@/app_modules/notifikasi/model/interface";
import backendLogger from "@/util/backendLogger";
import _ from "lodash";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(request: Request) {
if (request.method === "GET") {
try {
const { searchParams } = new URL(request.url);
const category = searchParams.get("category") as ICategoryapp;
const page = searchParams.get("page");
const userLoginId = await newFunGetUserId();
const fixPage = _.toNumber(page);
const takeData = 10;
const skipData = fixPage * takeData - takeData;
if (category === "Semua") {
const data = await prisma.notifikasi.findMany({
take: takeData,
skip: skipData,
orderBy: [
{
isRead: "asc",
},
{ createdAt: "desc" },
],
where: {
userId: userLoginId,
userRoleId: "1",
},
});
return NextResponse.json({ success: true, data });
}
const allData = await prisma.notifikasi.findMany({
take: takeData,
skip: skipData,
orderBy: [
{
isRead: "asc",
},
{ createdAt: "desc" },
],
where: {
userId: userLoginId,
userRoleId: "1",
kategoriApp: _.upperCase(category),
},
});
return NextResponse.json({ success: true, data: allData });
} catch (error) {
backendLogger.error("Error get data notifikasi: " + error);
}
} else {
return NextResponse.json({ success: false, message: "Method not allowed" });
}
}

View File

@@ -1,15 +1,11 @@
import { Notifikasi_UiEvent } from "@/app_modules/notifikasi/_ui"; import { Notifikasi_UiEvent } from "@/app_modules/notifikasi/_ui";
import notifikasi_getByUserId from "@/app_modules/notifikasi/fun/get/get_notifiaksi_by_id";
export default async function Page() { export default async function Page() {
const listNotifikasi = await notifikasi_getByUserId({ //
page: 1,
kategoriApp: "Event",
});
return ( return (
<> <>
<Notifikasi_UiEvent listNotifikasi={listNotifikasi} /> <Notifikasi_UiEvent />
</> </>
); );
} }

View File

@@ -1,15 +1,9 @@
import { Notifikasi_UiAll } from "@/app_modules/notifikasi/_ui"; import { Notifikasi_UiAll } from "@/app_modules/notifikasi/_ui";
import notifikasi_getByUserId from "@/app_modules/notifikasi/fun/get/get_notifiaksi_by_id";
export default async function Page() { export default async function Page() {
const listNotifikasi = await notifikasi_getByUserId({
page: 1,
kategoriApp: "Semua",
});
return ( return (
<> <>
<Notifikasi_UiAll listNotifikasi={listNotifikasi} /> <Notifikasi_UiAll />
</> </>
); );
} }

View File

@@ -0,0 +1,11 @@
import { ICategoryapp } from "@/app_modules/notifikasi/model/interface";
export const API_RouteNotifikasi = {
get_all_by_category: ({
category,
page,
}: {
category: ICategoryapp;
page: number;
}) => `/api/notifikasi/get-all-by-category?category=${category}&page=${page}`,
};

View File

@@ -17,6 +17,7 @@ import BodyHome from "./component/body_home";
import FooterHome from "./component/footer_home"; import FooterHome from "./component/footer_home";
import { apiGetDataHome } from "./fun/get/api_home"; import { apiGetDataHome } from "./fun/get/api_home";
import { RouterProfile } from "@/app/lib/router_hipmi/router_katalog"; import { RouterProfile } from "@/app/lib/router_hipmi/router_katalog";
import { gs_notifikasi_kategori_app } from "../notifikasi/lib";
export default function HomeViewNew({ export default function HomeViewNew({
countNotifikasi, countNotifikasi,
@@ -27,6 +28,7 @@ export default function HomeViewNew({
const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf);
const [countLoadNtf, setCountLoadNtf] = useAtom(gs_count_ntf); const [countLoadNtf, setCountLoadNtf] = useAtom(gs_count_ntf);
const [dataUser, setDataUser] = useState<any>({}); const [dataUser, setDataUser] = useState<any>({});
const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app);
const router = useRouter(); const router = useRouter();
useShallowEffect(() => { useShallowEffect(() => {
@@ -98,12 +100,14 @@ export default function HomeViewNew({
) { ) {
router.push(RouterProfile.create, { scroll: false }); router.push(RouterProfile.create, { scroll: false });
} else { } else {
setCategoryPage("Semua")
router.push( router.push(
RouterNotifikasi.categoryApp({ name: "semua" }), RouterNotifikasi.categoryApp({ name: "semua" }),
{ {
scroll: false, scroll: false,
} }
); );
} }
}} }}
> >

View File

@@ -2,46 +2,57 @@
import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data"; import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data";
import ComponentGlobal_Loader from "@/app_modules/_global/component/loader"; import ComponentGlobal_Loader from "@/app_modules/_global/component/loader";
import { Box, Center } from "@mantine/core"; import {
Box,
Center,
Divider,
Grid,
Group,
Loader,
Skeleton,
Stack,
} from "@mantine/core";
import _ from "lodash"; import _ from "lodash";
import { ScrollOnly } from "next-scroll-loader"; import { ScrollOnly } from "next-scroll-loader";
import { useState } from "react"; import { useState } from "react";
import { ComponentNotifiaksi_CardView } from "../component"; import {
ComponentNotifiaksi_CardView,
Notifikasi_ComponentSkeletonView,
} from "../component";
import notifikasi_getByUserId from "../fun/get/get_notifiaksi_by_id"; import notifikasi_getByUserId from "../fun/get/get_notifiaksi_by_id";
import { MODEL_NOTIFIKASI } from "../model/interface"; import { ICategoryapp, MODEL_NOTIFIKASI } from "../model/interface";
import { useAtom } from "jotai"; import { useAtom } from "jotai";
import { gs_notifikasi_kategori_app } from "../lib"; import { gs_notifikasi_kategori_app } from "../lib";
import { useShallowEffect } from "@mantine/hooks"; import { useShallowEffect } from "@mantine/hooks";
import { API_RouteNotifikasi } from "@/app/lib/api_user_router/route_api_notifikasi";
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
export default function Notifikasi_UiAll({ export default function Notifikasi_UiAll() {
listNotifikasi, const [data, setData] = useState<MODEL_NOTIFIKASI[] | null>(null);
}: {
listNotifikasi: any[];
}) {
const [data, setData] = useState<MODEL_NOTIFIKASI[]>(listNotifikasi);
const [activePage, setActivePage] = useState(1); const [activePage, setActivePage] = useState(1);
const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app); const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app);
useShallowEffect(() => { useShallowEffect(() => {
onLoadData({ onLoadData();
onSetData(val: any) { }, []);
setData(val);
},
});
}, [setData]);
async function onLoadData({ onSetData }: { onSetData: any }) { async function onLoadData() {
const loadData = await notifikasi_getByUserId({ const loadData = await fetch(
API_RouteNotifikasi.get_all_by_category({
category: categoryPage as any,
page: 1, page: 1,
kategoriApp: "Semua", })
}); );
onSetData(loadData); const data = await loadData.json().then((res) => res.data as any);
setData(data);
} }
return ( return (
<> <>
<Box> <Box>
{_.isEmpty(data) ? ( {_.isNull(data) ? (
<Notifikasi_ComponentSkeletonView />
) : _.isEmpty(data) ? (
<ComponentGlobal_IsEmptyData text="Tidak ada pemberitahuan" /> <ComponentGlobal_IsEmptyData text="Tidak ada pemberitahuan" />
) : ( ) : (
<ScrollOnly <ScrollOnly
@@ -52,22 +63,25 @@ export default function Notifikasi_UiAll({
</Center> </Center>
)} )}
data={data} data={data}
setData={setData} setData={setData as any}
moreData={async () => { moreData={async () => {
const loadData = await notifikasi_getByUserId({ const loadData = await fetch(
API_RouteNotifikasi.get_all_by_category({
category: categoryPage as any,
page: activePage + 1, page: activePage + 1,
kategoriApp: categoryPage as any, })
}); );
const data = await loadData.json().then((res) => res.data as any);
setActivePage((val) => val + 1); setActivePage((val) => val + 1);
return loadData; return data;
}} }}
> >
{(item) => ( {(item) => (
<ComponentNotifiaksi_CardView <ComponentNotifiaksi_CardView
data={item} data={item}
onLoadData={setData} onLoadData={setData}
categoryPage={categoryPage} categoryPage={categoryPage as any}
/> />
)} )}
</ScrollOnly> </ScrollOnly>

View File

@@ -7,37 +7,39 @@ import { useAtom } from "jotai";
import _ from "lodash"; import _ from "lodash";
import { ScrollOnly } from "next-scroll-loader"; import { ScrollOnly } from "next-scroll-loader";
import { useState } from "react"; import { useState } from "react";
import { ComponentNotifiaksi_CardView } from "../component"; import {
ComponentNotifiaksi_CardView,
Notifikasi_ComponentSkeletonView,
} from "../component";
import notifikasi_getByUserId from "../fun/get/get_notifiaksi_by_id"; import notifikasi_getByUserId from "../fun/get/get_notifiaksi_by_id";
import { gs_notifikasi_kategori_app } from "../lib"; import { gs_notifikasi_kategori_app } from "../lib";
import { MODEL_NOTIFIKASI } from "../model/interface"; import { MODEL_NOTIFIKASI } from "../model/interface";
import { useShallowEffect } from "@mantine/hooks"; import { useShallowEffect } from "@mantine/hooks";
import { API_RouteNotifikasi } from "@/app/lib/api_user_router/route_api_notifikasi";
export default function Notifikasi_UiEvent({ export default function Notifikasi_UiEvent() {
listNotifikasi, const [data, setData] = useState<MODEL_NOTIFIKASI[] | null>(null);
}: {
listNotifikasi: any[];
}) {
const [data, setData] = useState<MODEL_NOTIFIKASI[]>(listNotifikasi);
const [activePage, setActivePage] = useState(1); const [activePage, setActivePage] = useState(1);
const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app); const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app);
useShallowEffect(() => { useShallowEffect(() => {
onLoadData(setData); onLoadData();
}, [setData]); }, []);
async function onLoadData(setData: any) { async function onLoadData() {
const listNotifikasi = await notifikasi_getByUserId({ const loadData = await fetch(
page: 1, API_RouteNotifikasi.get_all_by_category({ category: categoryPage as any, page: 1 })
kategoriApp: "Event", );
}); const data = await loadData.json().then((res) => res.data as any);
setData(listNotifikasi); setData(data);
} }
return ( return (
<> <>
<Box> <Box>
{_.isEmpty(data) ? ( {_.isNull(data) ? (
<Notifikasi_ComponentSkeletonView />
) : _.isEmpty(data) ? (
<ComponentGlobal_IsEmptyData text="Tidak ada pemberitahuan" /> <ComponentGlobal_IsEmptyData text="Tidak ada pemberitahuan" />
) : ( ) : (
<ScrollOnly <ScrollOnly
@@ -48,22 +50,25 @@ export default function Notifikasi_UiEvent({
</Center> </Center>
)} )}
data={data} data={data}
setData={setData} setData={setData as any}
moreData={async () => { moreData={async () => {
const loadData = await notifikasi_getByUserId({ const loadData = await fetch(
API_RouteNotifikasi.get_all_by_category({
category: categoryPage as any,
page: activePage + 1, page: activePage + 1,
kategoriApp: categoryPage as any, })
}); );
const data = await loadData.json().then((res) => res.data as any);
setActivePage((val) => val + 1); setActivePage((val) => val + 1);
return loadData; return data;
}} }}
> >
{(item) => ( {(item) => (
<ComponentNotifiaksi_CardView <ComponentNotifiaksi_CardView
data={item} data={item}
onLoadData={setData} onLoadData={setData}
categoryPage={categoryPage} categoryPage={categoryPage as any}
/> />
)} )}
</ScrollOnly> </ScrollOnly>

View File

@@ -108,9 +108,7 @@ function UIChildren({
style={{ style={{
transition: "0.3s", transition: "0.3s",
backgroundColor: backgroundColor:
categoryPage === e.name categoryPage === e.name ? MainColor.yellow : "GrayText",
? MainColor.yellow
: "GrayText",
}} }}
onClick={() => { onClick={() => {
router.replace( router.replace(
@@ -120,7 +118,6 @@ function UIChildren({
} }
); );
setCategoryPage(e.name); setCategoryPage(e.name);
// onLoadDataNotifikasi(e.name);
}} }}
> >
{e.name} {e.name}

View File

@@ -13,13 +13,15 @@ import { IconCheck, IconChecks } from "@tabler/icons-react";
import { useAtom } from "jotai"; import { useAtom } from "jotai";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useState } from "react"; import { useState } from "react";
import { MODEL_NOTIFIKASI } from "../model/interface"; import { ICategoryapp, MODEL_NOTIFIKASI } from "../model/interface";
import { notifikasi_eventCheckStatus } from "./path/event"; import { notifikasi_eventCheckStatus } from "./path/event";
import { notifikasi_jobCheckStatus } from "./path/job"; import { notifikasi_jobCheckStatus } from "./path/job";
import { gs_vote_hotMenu } from "@/app_modules/vote/global_state"; import { gs_vote_hotMenu } from "@/app_modules/vote/global_state";
import { notifikasi_votingCheckStatus } from "./path/voting"; import { notifikasi_votingCheckStatus } from "./path/voting";
import { redirectDonasiPage } from "./path/donasi"; import { redirectDonasiPage } from "./path/donasi";
import { gs_donasi_hot_menu } from "@/app_modules/donasi/global_state"; import { gs_donasi_hot_menu } from "@/app_modules/donasi/global_state";
import moment from "moment";
import "moment/locale/id";
export function ComponentNotifiaksi_CardView({ export function ComponentNotifiaksi_CardView({
data, data,
@@ -28,7 +30,7 @@ export function ComponentNotifiaksi_CardView({
}: { }: {
data: MODEL_NOTIFIKASI; data: MODEL_NOTIFIKASI;
onLoadData: (val: any) => void; onLoadData: (val: any) => void;
categoryPage: any; categoryPage: string;
}) { }) {
const router = useRouter(); const router = useRouter();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
@@ -220,16 +222,7 @@ export function ComponentNotifiaksi_CardView({
<Card.Section p={"sm"}> <Card.Section p={"sm"}>
<Group position="apart"> <Group position="apart">
<Text fz={10} color="gray"> <Text fz={10} color="gray">
{new Intl.DateTimeFormat("id-ID", { {moment(data.createdAt).format("LLL")}
dateStyle: "long",
}).format(data?.createdAt)}
<Text span inherit fz={10} color="gray">
{", "}
{new Intl.DateTimeFormat("id-ID", {
timeStyle: "short",
}).format(data?.createdAt)}
</Text>
</Text> </Text>
{data?.isRead ? ( {data?.isRead ? (
<Group spacing={5}> <Group spacing={5}>

View File

@@ -1,5 +1,7 @@
import { ComponentNotifikasi_CardSkeleton } from "./card_skeleton"; import { ComponentNotifikasi_CardSkeleton } from "./card_skeleton";
import { ComponentNotifiaksi_CardView } from "./card_view"; import { ComponentNotifiaksi_CardView } from "./card_view";
import Notifikasi_ComponentSkeletonView from "./skeleton_view";
export { ComponentNotifiaksi_CardView }; export { ComponentNotifiaksi_CardView };
export { ComponentNotifikasi_CardSkeleton }; export { ComponentNotifikasi_CardSkeleton };
export { Notifikasi_ComponentSkeletonView };

View File

@@ -0,0 +1,29 @@
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
import { Stack, Group, Skeleton, Divider } from "@mantine/core";
export default function Notifikasi_ComponentSkeletonView() {
return (
<>
{Array.from({ length: 10 }).map((_, i) => (
<ComponentGlobal_CardStyles key={i}>
<Stack>
<Group position="apart">
<Skeleton h={15} w={70} radius={"xl"} />
<Skeleton h={15} w={100} radius={"xl"} />
</Group>
<Divider color="gray" />
<Skeleton h={15} w={50} radius={"xl"} />
<Skeleton h={15} w={"100%"} radius={"xl"} />
<Skeleton h={15} w={"100%"} radius={"xl"} />
<Group position="apart">
<Skeleton h={15} w={100} radius={"xl"} />
<Skeleton h={15} w={50} radius={"xl"} />
</Group>
</Stack>
</ComponentGlobal_CardStyles>
))}
</>
);
}

View File

@@ -33,8 +33,18 @@ export interface MODEL_NOTIFIKASI {
| "Gagal" | "Gagal"
| "Donatur Baru" | "Donatur Baru"
| "Kabar Donasi" | "Kabar Donasi"
| "Pencairan Dana" | "Pencairan Dana";
Role: MODEL_NEW_DEFAULT_MASTER; Role: MODEL_NEW_DEFAULT_MASTER;
userRoleId: String; userRoleId: String;
} }
export type ICategoryapp =
| "Semua"
| "Event"
| "Job"
| "Voting"
| "Donasi"
| "Investasi"
| "Forum"
| "Collaboration";

View File

@@ -19,6 +19,7 @@ const middlewareConfig: MiddlewareConfig = {
userPath: "/dev/home", userPath: "/dev/home",
publicRoutes: [ publicRoutes: [
"/", "/",
"/api/notifikasi/*",
"/api/logs/*", "/api/logs/*",
"/api/image/*", "/api/image/*",
"/api/job/*", "/api/job/*",
@@ -35,7 +36,7 @@ const middlewareConfig: MiddlewareConfig = {
"/auth/api/login", "/auth/api/login",
"/aset/global/main_background.png", "/aset/global/main_background.png",
"/aset/logo/logo-hipmi.png", "/aset/logo/logo-hipmi.png",
"/api/new/*" "/api/new/*",
], ],
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!, sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,