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:
45
src/app/api/notification/route.ts
Normal file
45
src/app/api/notification/route.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { funGetHome } from "./lib/api_home";
|
import { funGetHome } from "./lib/api_home";
|
||||||
import { globalParamJumlahNotif } from "./lib/val_home";
|
import { globalParamJumlahNotif } from "./lib/val_home";
|
||||||
|
import NotificationAnnouncement from "./ui/notification_announcement";
|
||||||
import ViewDetailFeature from "./ui/view_detail_feature";
|
import ViewDetailFeature from "./ui/view_detail_feature";
|
||||||
import ViewHome from "./ui/view_home";
|
import ViewHome from "./ui/view_home";
|
||||||
import ViewNotification from "./ui/view_notification";
|
import ViewNotification from "./ui/view_notification";
|
||||||
@@ -11,3 +12,4 @@ export { ViewSearch }
|
|||||||
export { ViewNotification }
|
export { ViewNotification }
|
||||||
export { funGetHome }
|
export { funGetHome }
|
||||||
export { globalParamJumlahNotif }
|
export { globalParamJumlahNotif }
|
||||||
|
export { NotificationAnnouncement }
|
||||||
@@ -11,3 +11,8 @@ export const funReadNotification = async (data: { id: string }) => {
|
|||||||
});
|
});
|
||||||
return await response.json().catch(() => null);
|
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);
|
||||||
|
}
|
||||||
65
src/module/home/ui/notification_announcement.tsx
Normal file
65
src/module/home/ui/notification_announcement.tsx
Normal 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={() => { '' }}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import ListDiscussion from './list_discussion';
|
|||||||
import ListDivisi from './list_divisi';
|
import ListDivisi from './list_divisi';
|
||||||
import ListEventHome from './list_event';
|
import ListEventHome from './list_event';
|
||||||
import ListProjects from './list_project';
|
import ListProjects from './list_project';
|
||||||
|
import NotificationAnnouncement from './notification_announcement';
|
||||||
|
|
||||||
|
|
||||||
export default function ViewHome() {
|
export default function ViewHome() {
|
||||||
@@ -18,6 +19,7 @@ export default function ViewHome() {
|
|||||||
<HeaderHome />
|
<HeaderHome />
|
||||||
<Box p={20}>
|
<Box p={20}>
|
||||||
<Stack >
|
<Stack >
|
||||||
|
<NotificationAnnouncement />
|
||||||
<Carosole />
|
<Carosole />
|
||||||
<Features />
|
<Features />
|
||||||
<ListProjects />
|
<ListProjects />
|
||||||
|
|||||||
Reference in New Issue
Block a user