import { useState } from 'react';
import Layout from "../../../components/layout";
import ProtectedRoute from '../../../components/protectedRoute';
import { UserRole } from '@prisma/client';
import axiosServer from '../../../src/axiosServer';
import common from '../../../src/helpers/common';
import Modal from 'components/Modal';
import ConfirmationModal from 'components/ConfirmationModal';
import PublisherSearchBox from '../../../components/publisher/PublisherSearchBox'; // Update the path
import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../../src/helpers/const"
import { useSession, getSession } from 'next-auth/react';
import axiosInstance from 'src/axiosSecure';
import { toast } from 'react-toastify';
import LocalShippingIcon from '@mui/icons-material/LocalShipping';
export default function MySchedulePage({ assignments }) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isConfirmModalOpen, setIsConfirmModalOpen] = useState(false);
const [useFilterDate, setUseFilterDate] = useState(true);
const [assignment, setАssignment] = useState(null);
const [newPublisher, setNewPublisher] = useState(null);
const { data: session, status } = useSession();
if (status === "loading") {
return
Loading...
;
}
const handleReplaceInAssignment = () => {
// Add publisher as assignment logic
setIsModalOpen(false);
setIsConfirmModalOpen(false);
console.log("publisher", newPublisher.firstName, " ", newPublisher.lastName, " set to shift ", assignment.shift.id);
//api.replaceInAssignment()
axiosInstance.post('/api/?action=replaceInAssignment', {
oldPublisherId: session.user.id,
newPublisherId: newPublisher.id,
shiftId: assignment.shift.id,
}).then(response => {
console.log("response", response);
//toast success and confirm the change
// refresh the page after toast is closed
toast.success("Промяната е записана!", {
onClose: () => {
window.location.reload();
}
});
}).catch(error => {
console.log("error", error);
});
};
const searchReplacement = (assignmentId) => {
axiosInstance.post('/api/email?action=sendCoverMeRequestByEmail', {
assignmentId: assignmentId,
}).then(response => {
console.log("response", response);
//toast success and confirm the change
toast.success("Заявката за заместник е изпратена!", {
onClose: () => {
window.location.reload();
}
});
}).catch(error => {
console.log("error", error);
});
}
return (
Моите смени
{assignments && assignments.map((assignment) => (
{assignment.dateStr}
- Час
-
{GetTimeFormat(assignment.shift.startTime)} - {GetTimeFormat(assignment.shift.endTime)}
- Смяна
-
{assignment.shift.assignments.map((a, index) => {
return (
{a.publisher.firstName} {a.publisher.lastName}
{a.isWithTransport && }
)
}
)}
- Действия
-
))}
setIsModalOpen(false)}
forDate={new Date(assignment?.shift.startTime)}
useFilterDate={useFilterDate}
onUseFilterDateChange={(value) => setUseFilterDate(value)}>
{
setIsConfirmModalOpen(true);
setNewPublisher(publisher);
}}
showAllAuto={true}
showSearch={true}
showList={false}
/>
{ setIsConfirmModalOpen(false); setNewPublisher(null); }}
onConfirm={handleReplaceInAssignment}
message="Това действие ще те замести в назначената ти смяна. Потвърждаваш ли, че заместника знае за тази промяна."
/>
);
}
//get future assignments for the current user (session.user.id)
export const getServerSideProps = async (context) => {
const session = await getSession(context);
context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate");
if (!session) {
return {
redirect: {
destination: '/auth/login', // Adjust the login path as needed
permanent: false,
},
};
}
const prisma = common.getPrismaClient();
const monthInfo = common.getMonthInfo(new Date());
//minus 1 day from the firstMonday to get the last Sunday
const lastSunday = new Date(monthInfo.firstMonday);
lastSunday.setDate(lastSunday.getDate() - 1);
const publisher = await prisma.publisher.findUnique({
where: {
id: session.user.id,
assignments: {
some: {
shift: {
startTime: {
gte: lastSunday,
},
},
},
},
},
include: {
assignments: {
include: {
shift: {
include: {
assignments: {
include: {
publisher: {
select: {
id: true,
firstName: true,
lastName: true,
}
}
}
}
},
},
},
},
},
});
const assignments = publisher?.assignments || [];
const transformedAssignments = assignments?.map(assignment => {
if (assignment.shift && assignment.shift.startTime) {
return {
...assignment,
dateStr: assignment.shift.startTime.toLocaleDateString("bg-BG", { day: "numeric", month: "long" }),
shift: {
...assignment.shift,
startTime: assignment.shift.startTime.toISOString(),
endTime: assignment.shift.endTime.toISOString(),
},
};
}
return assignment;
});
return {
props: { assignments: transformedAssignments }
}
}