disable edits for published dates if not admin
This commit is contained in:
@ -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()) {
|
||||
|
@ -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
|
||||
<div className="text-center font-bold pb-3 xs:pb-1">
|
||||
<PublisherInlineForm publisherId={userId} />
|
||||
</div>
|
||||
<AvCalendar publisherId={userId} events={events} selectedDate={new Date()} cartEvents={cartEvents} />
|
||||
<AvCalendar publisherId={userId} events={events} selectedDate={new Date()} cartEvents={cartEvents} lastPublishedDate={lastPublishedDate} />
|
||||
</div>
|
||||
</div>
|
||||
</ProtectedRoute>
|
||||
@ -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
|
||||
},
|
||||
};
|
||||
|
Reference in New Issue
Block a user