From 26af8382adff66b765161de6b1e36473d75ba0e9 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 11 May 2024 14:10:36 +0300 Subject: [PATCH] disable edits for published dates if not admin --- components/calendar/avcalendar.tsx | 22 +++++++++++++++++++--- pages/dash.tsx | 20 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/components/calendar/avcalendar.tsx b/components/calendar/avcalendar.tsx index d6497b5..cfac429 100644 --- a/components/calendar/avcalendar.tsx +++ b/components/calendar/avcalendar.tsx @@ -48,9 +48,19 @@ const messages = { // Any other labels you want to translate... }; -const AvCalendar = ({ publisherId, events, selectedDate, cartEvents }) => { - - const isAdmin = ProtectedRoute.IsInRole(UserRole.ADMIN); +const AvCalendar = ({ publisherId, events, selectedDate, cartEvents, lastPublishedDate }) => { + const [editLockedBefore, setEditLockedBefore] = useState(new Date(lastPublishedDate)); + const [isAdmin, setIsAdmin] = useState(false); + useEffect(() => { + (async () => { + try { + setIsAdmin(await ProtectedRoute.IsInRole(UserRole.ADMIN)); + } catch (error) { + console.error("Failed to check admin role:", error); + } + })(); + }, []); + //const isAdmin = ProtectedRoute.IsInRole(UserRole.ADMIN); const [date, setDate] = useState(new Date()); //ToDo: see if we can optimize this @@ -227,6 +237,12 @@ const AvCalendar = ({ publisherId, events, selectedDate, cartEvents }) => { //readonly for past dates (ToDo: if not admin) if (!isAdmin) { if (startdate < new Date() || end < new Date() || startdate > end) return; + //or if schedule is published (lastPublishedDate) + if (editLockedBefore && startdate < editLockedBefore) { + toast.error(`Не можете да променяте предпочитанията си за дати преди ${common.getDateFormatedShort(editLockedBefore)}.`, { autoClose: 5000 }); + return; + + } } // Check if start and end are on the same day if (startdate.toDateString() !== enddate.toDateString()) { diff --git a/pages/dash.tsx b/pages/dash.tsx index 951d9ca..53b3615 100644 --- a/pages/dash.tsx +++ b/pages/dash.tsx @@ -21,8 +21,10 @@ import CartEventForm from "components/cartevent/CartEventForm"; interface IProps { initialItems: Availability[]; initialUserId: string; + cartEvents: any; + lastPublishedDate: Date; } -export default function IndexPage({ initialItems, initialUserId, cartEvents }: IProps) { +export default function IndexPage({ initialItems, initialUserId, cartEvents, lastPublishedDate }: IProps) { const { data: session } = useSession(); const [userName, setUserName] = useState(session?.user?.name); const [userId, setUserId] = useState(initialUserId); @@ -79,7 +81,7 @@ export default function IndexPage({ initialItems, initialUserId, cartEvents }: I
- + @@ -229,6 +231,7 @@ export const getServerSideProps = async (context) => { startTime: updatedItem.shift.startTime.toISOString(), endTime: updatedItem.shift.endTime.toISOString() }; + updatedItem.isPublished = updatedItem.shift.isPublished; } return updatedItem; @@ -239,6 +242,7 @@ export const getServerSideProps = async (context) => { console.log("First availability startTime: " + items[0]?.startTime); console.log("First availability startTime: " + items[0]?.startTime.toLocaleString()); + const prisma = common.getPrismaClient(); let cartEvents = await prisma.cartEvent.findMany({ where: { @@ -253,11 +257,23 @@ export const getServerSideProps = async (context) => { } }); cartEvents = common.convertDatesToISOStrings(cartEvents); + const lastPublishedDate = (await prisma.shift.findFirst({ + where: { + isPublished: true, + }, + select: { + endTime: true, + }, + orderBy: { + endTime: 'desc' + } + })).endTime; return { props: { initialItems: items, userId: sessionServer?.user.id, cartEvents: cartEvents, + lastPublishedDate: lastPublishedDate.toISOString(), // messages: (await import(`../content/i18n/${context.locale}.json`)).default }, };