diff --git a/app/api/appointments/student/route.ts b/app/api/appointments/student/route.ts index f49d4f1..f637e45 100644 --- a/app/api/appointments/student/route.ts +++ b/app/api/appointments/student/route.ts @@ -22,13 +22,6 @@ export async function DELETE(request: Request) { reason, } = body as PostProps; - // get the student - const student = await prisma.student.findUnique({ - where: { - userId: studentId, - }, - }); - // get the appointment const appointment = await prisma.appointment.findUnique({ where: { @@ -57,10 +50,10 @@ export async function DELETE(request: Request) { data: { message: `Zostałeś wyrzucony z zajęć z przedmiotu ${ appointment?.subject.name - } w dniu: ${appointment?.dateTime.toLocaleString("pl-PL", { - weekday: "short", + } w dniu: ${appointment?.dateTime.toLocaleDateString("pl-PL", { day: "numeric", - month: "numeric", + month: "short", + year: "numeric", })}`, reason, userId: studentId, diff --git a/app/api/leave/route.ts b/app/api/leave/route.ts index e38de88..07779e0 100644 --- a/app/api/leave/route.ts +++ b/app/api/leave/route.ts @@ -9,7 +9,12 @@ async function leave(appointmentId: string, studentId: number) { appointmentId: appointmentId, }, }, + include: { + appointment: true, + }, }); + + return appointment; } export async function POST(request: Request) { @@ -17,7 +22,31 @@ export async function POST(request: Request) { const { appointmentId, studentId } = body; try { - await leave(appointmentId, studentId); + const appointment = await leave(appointmentId, studentId); + + const student = await prisma.student.findUnique({ + where: { + userId: studentId, + }, + include: { + user: true, + }, + }); + + const studentName = `${student?.user.firstName} ${student?.user.lastName}`; + + await prisma.notification.create({ + data: { + userId: appointment.appointment.teacherId, + type: "appointment_leave", + reason: "", + message: `${studentName} opuścił korepetycje z ${ + appointment.subject + } odbuwające się w dniu ${appointment.appointment.dateTime.toLocaleDateString( + "pl-PL" + )}.`, + }, + }); return new NextResponse(null, { status: 200, diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx index 6fd8e55..8483ffe 100644 --- a/components/Sidebar.tsx +++ b/components/Sidebar.tsx @@ -95,6 +95,7 @@ export function Sidebar({ setSidebarOpen, children, notifications, + setNotifications, }: SidebarProps) { const userRole = user?.role; const [fillColor, setFillColor] = useState(""); @@ -112,10 +113,33 @@ export function Sidebar({ const pathname = usePathname(); - const [filteredNotifications, setFilteredNotifications] = useState( + const [filteredNotifications, setFilteredNotifications] = + useState(notifications); + + const [unreadNotifications, setUnreadNotifications] = useState( notifications.filter((notification) => notification.read == false) + .length ); + useEffect(() => { + if ( + notifications != filteredNotifications && + filteredNotifications.length == 0 + ) { + setFilteredNotifications( + notifications.filter( + (notification) => notification.read == false + ) + ); + } + + setUnreadNotifications( + filteredNotifications.filter( + (notification) => notification.read == false + ).length + ); + }, [notifications]); + async function handleNotificationRead(id: number) { await fetch(`/api/notifications`, { method: "PATCH", @@ -125,14 +149,17 @@ export function Sidebar({ body: JSON.stringify({ id, read: true }), }); - const newNotifications = filteredNotifications.map((notification) => { - if (notification.id == id) { - notification.read = true; - } + const newNotifications = notifications + .filter((notification) => notification.read == false) + .map((notification: any) => { + if (notification.id == id) { + notification.read = true; + } - return notification; - }); + return notification; + }); + setNotifications(newNotifications); setFilteredNotifications(newNotifications); } @@ -262,7 +289,7 @@ export function Sidebar({ className="h-8 w-8" aria-hidden="true" /> - {filteredNotifications.length > 0 && ( + {unreadNotifications > 0 && ( @@ -274,7 +301,7 @@ export function Sidebar({ Powiadomienia - {filteredNotifications.length > 0 ? ( + {unreadNotifications > 0 ? ( filteredNotifications.map( (notification, index) => ( -
+
{notification.type == - "appointment_kick" ? ( + "appointment_kick" || + "appointment_leave" ? (