feature: notifikasi pengumuman setelah login

Deskripsi:
- ui notification announcement
- api findmany notification announcement
- nb : masih setiap reload home tampil (seharusnya setelah login aja)

No Issues
This commit is contained in:
amel
2025-04-07 17:16:01 +08:00
parent ad0736e7ca
commit def1dfe660
5 changed files with 120 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash";
import moment from "moment";
import "moment/locale/id";
import { NextResponse } from "next/server";
// GET ONE NOTIFIKASI PENGUMUMAN
export async function GET(request: Request) {
try {
const user = await funGetUserByCookies();
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const announcements = await prisma.notifications.findMany({
skip: 0,
take: 1,
where: {
isActive: true,
idUserTo: user.id,
isRead: false,
category: 'announcement'
},
orderBy: [
{
createdAt: 'desc'
}
]
});
const allData = announcements.map((v: any) => ({
..._.omit(v, ["createdAt"]),
createdAt: moment(v.createdAt).format("ll")
}))
return NextResponse.json({ success: true, message: "Berhasil mendapatkan notifikasi", data: allData, }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}

View File

@@ -1,5 +1,6 @@
import { funGetHome } from "./lib/api_home";
import { globalParamJumlahNotif } from "./lib/val_home";
import NotificationAnnouncement from "./ui/notification_announcement";
import ViewDetailFeature from "./ui/view_detail_feature";
import ViewHome from "./ui/view_home";
import ViewNotification from "./ui/view_notification";
@@ -10,4 +11,5 @@ export { ViewDetailFeature }
export { ViewSearch }
export { ViewNotification }
export { funGetHome }
export { globalParamJumlahNotif }
export { globalParamJumlahNotif }
export { NotificationAnnouncement }

View File

@@ -10,4 +10,9 @@ export const funReadNotification = async (data: { id: string }) => {
body: JSON.stringify(data),
});
return await response.json().catch(() => null);
}
export const funGetOneNotificationAnnouncement = async (path?: string) => {
const response = await fetch(`/api/notification${(path) ? path : ''}`, { next: { tags: ['notification'] } });
return await response.json().catch(() => null);
}

View File

@@ -0,0 +1,65 @@
'use client'
import { NotificationCustome } from "@/module/_global";
import { useShallowEffect } from "@mantine/hooks";
import { useRouter } from "next/navigation";
import { useState } from "react";
import toast from "react-hot-toast";
import { funGetOneNotificationAnnouncement, funReadNotification } from "../lib/api_notification";
import { IListNotification } from "../lib/type_notification";
export default function NotificationAnnouncement() {
const router = useRouter()
const [tampilNotif, setTampilNotif] = useState(false)
const [isData, setData] = useState<IListNotification[]>([]);
async function fetchData() {
try {
const res = await funGetOneNotificationAnnouncement();
if (res.success) {
setTampilNotif(true)
setData(res.data);
} else {
toast.error(res.message);
}
} catch (error) {
console.error(error);
toast.error("Gagal memuat data, coba lagi nanti");
} finally {
setTimeout(() => {
setTampilNotif(false);
}, 6000);
}
}
useShallowEffect(() => {
fetchData();
}, []);
async function onClickNotif(category: string, idContent: string, idData: string) {
try {
const response = await funReadNotification({ id: idData });
if (response.success) {
router.push(`/${category}/${idContent}`);
} else {
toast.error(response.message);
}
} catch (error) {
console.error(error);
toast.error("Gagal memuat data, coba lagi nanti");
}
}
return (
<>
{
tampilNotif &&
<NotificationCustome
title={isData[0]?.title}
desc={isData[0]?.desc}
onClick={() => { onClickNotif(isData[0]?.category, isData[0]?.idContent, isData[0]?.id) }}
onClose={() => { '' }}
/>
}
</>
)
}

View File

@@ -9,6 +9,7 @@ import ListDiscussion from './list_discussion';
import ListDivisi from './list_divisi';
import ListEventHome from './list_event';
import ListProjects from './list_project';
import NotificationAnnouncement from './notification_announcement';
export default function ViewHome() {
@@ -18,6 +19,7 @@ export default function ViewHome() {
<HeaderHome />
<Box p={20}>
<Stack >
<NotificationAnnouncement />
<Carosole />
<Features />
<ListProjects />