upd
This commit is contained in:
@@ -6,6 +6,9 @@ import { addMinutes, differenceInMinutes, format, startOfDay } from "date-fns";
|
||||
import Button from "./Button";
|
||||
import IScheduledSession from "../types/IScheduledSession";
|
||||
import useStore from "../stores/useStore";
|
||||
import CloseIcon from "./icons/CloseIcon";
|
||||
import api from "../utils/api";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
interface Props {
|
||||
timelineEvents: IScheduledSession[];
|
||||
@@ -91,6 +94,23 @@ function Timeline({
|
||||
onChangeDraftMode(true);
|
||||
}
|
||||
|
||||
async function handleClickRemove(eventId: string) {
|
||||
try {
|
||||
const result: { status: string; message: string } = await api
|
||||
.delete(`scheduledSessions/${eventId}`)
|
||||
.json();
|
||||
|
||||
if (result.status === "error") {
|
||||
toast.error(result.message);
|
||||
return;
|
||||
}
|
||||
|
||||
toast.success("Сеанс успешно удален!");
|
||||
} catch (error) {
|
||||
toast.error("Что-то пошло не так");
|
||||
}
|
||||
}
|
||||
|
||||
// useEffect(() => {
|
||||
// setTimelineEvents(
|
||||
// timelineEvents.filter((event: IEvent) => event.slot === slot)
|
||||
@@ -170,7 +190,7 @@ function Timeline({
|
||||
}px`,
|
||||
}}
|
||||
>
|
||||
<div className="flex flex-col justify-between h-full">
|
||||
<div className="relative flex flex-col justify-between h-full">
|
||||
<div className="space-y-1">
|
||||
<p>
|
||||
{format(new Date(event.startAt), "HH:mm")} -{" "}
|
||||
@@ -185,6 +205,13 @@ function Timeline({
|
||||
>
|
||||
<Button className="">Начать</Button>
|
||||
</a>
|
||||
<Button
|
||||
variant="tertiary"
|
||||
icon={<CloseIcon className="" />}
|
||||
onlyIcon
|
||||
onClick={() => handleClickRemove(event.id)}
|
||||
className="absolute -top-2 -right-2"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -134,13 +134,13 @@ router.post("/", async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (overlappingSessions.length >= build.sessionLimit) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message:
|
||||
"It is not possible to create a session because it overlaps with the time of another session", // Невозможно создать сеанс, поскольку он перекрывается со временем другого сеанса.
|
||||
});
|
||||
}
|
||||
// if (overlappingSessions.length >= build.sessionLimit) {
|
||||
// return res.json({
|
||||
// status: "error",
|
||||
// message:
|
||||
// "It is not possible to create a session because it overlaps with the time of another session", // Невозможно создать сеанс, поскольку он перекрывается со временем другого сеанса.
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
const scheduledSession = await ScheduledSession.create({
|
||||
@@ -226,14 +226,30 @@ router.put("/:id", async (req, res) => {
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
const scheduledSessionId = req.params.id;
|
||||
let scheduledSession;
|
||||
|
||||
try {
|
||||
scheduledSession = await ScheduledSession.findById(scheduledSessionId);
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return res.json({ status: "error", message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
if (scheduledSession?.activeSessionId) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Этот сеанс нельзя удалить, так как он уже был начат",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await ScheduledSession.findByIdAndDelete(scheduledSessionId);
|
||||
|
||||
res.json({ status: "success" });
|
||||
return res.json({ status: "success" });
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
res.json({ status: "error", message: error.message });
|
||||
return res.json({ status: "error", message: error.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user