Skip to content

Commit

Permalink
Refactor appointment leave functionality and update sidebar notificat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
bartosz-skejcik committed Dec 22, 2023
1 parent c6060cc commit ae01fef
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
13 changes: 3 additions & 10 deletions app/api/appointments/student/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 30 additions & 1 deletion app/api/leave/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,44 @@ async function leave(appointmentId: string, studentId: number) {
appointmentId: appointmentId,
},
},
include: {
appointment: true,
},
});

return appointment;
}

export async function POST(request: Request) {
const body = await request.json();
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,
Expand Down
50 changes: 39 additions & 11 deletions components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function Sidebar({
setSidebarOpen,
children,
notifications,
setNotifications,
}: SidebarProps) {
const userRole = user?.role;
const [fillColor, setFillColor] = useState("");
Expand All @@ -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",
Expand All @@ -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);
}

Expand Down Expand Up @@ -262,7 +289,7 @@ export function Sidebar({
className="h-8 w-8"
aria-hidden="true"
/>
{filteredNotifications.length > 0 && (
{unreadNotifications > 0 && (
<span className="absolute -mt-8 ml-5 flex">
<span className="absolute inline-flex h-2.5 w-2.5 animate-ping rounded-full bg-green-400 opacity-75"></span>
<span className="relative inline-flex h-2.5 w-2.5 rounded-full bg-green-500"></span>
Expand All @@ -274,7 +301,7 @@ export function Sidebar({
Powiadomienia
</DropdownMenuLabel>
<DropdownMenuSeparator />
{filteredNotifications.length > 0 ? (
{unreadNotifications > 0 ? (
filteredNotifications.map(
(notification, index) => (
<DropdownMenuItem
Expand All @@ -290,9 +317,10 @@ export function Sidebar({
: "bg-white hover:bg-neutral-100"
)}
>
<div className="flex items-center justify-between p-0.5 gap-3">
<div className="flex items-center justify-between p-0.5 gap-3 cursor-pointer">
{notification.type ==
"appointment_kick" ? (
"appointment_kick" ||
"appointment_leave" ? (
<UserMinusIcon
className="h-8 w-8 text-red-500"
aria-hidden="true"
Expand Down

0 comments on commit ae01fef

Please sign in to comment.