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...
|
// Any other labels you want to translate...
|
||||||
};
|
};
|
||||||
|
|
||||||
const AvCalendar = ({ publisherId, events, selectedDate, cartEvents }) => {
|
const AvCalendar = ({ publisherId, events, selectedDate, cartEvents, lastPublishedDate }) => {
|
||||||
|
const [editLockedBefore, setEditLockedBefore] = useState(new Date(lastPublishedDate));
|
||||||
const isAdmin = ProtectedRoute.IsInRole(UserRole.ADMIN);
|
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());
|
const [date, setDate] = useState(new Date());
|
||||||
//ToDo: see if we can optimize this
|
//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)
|
//readonly for past dates (ToDo: if not admin)
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
if (startdate < new Date() || end < new Date() || startdate > end) return;
|
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
|
// Check if start and end are on the same day
|
||||||
if (startdate.toDateString() !== enddate.toDateString()) {
|
if (startdate.toDateString() !== enddate.toDateString()) {
|
||||||
|
@ -21,8 +21,10 @@ import CartEventForm from "components/cartevent/CartEventForm";
|
|||||||
interface IProps {
|
interface IProps {
|
||||||
initialItems: Availability[];
|
initialItems: Availability[];
|
||||||
initialUserId: string;
|
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 { data: session } = useSession();
|
||||||
const [userName, setUserName] = useState(session?.user?.name);
|
const [userName, setUserName] = useState(session?.user?.name);
|
||||||
const [userId, setUserId] = useState(initialUserId);
|
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">
|
<div className="text-center font-bold pb-3 xs:pb-1">
|
||||||
<PublisherInlineForm publisherId={userId} />
|
<PublisherInlineForm publisherId={userId} />
|
||||||
</div>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
@ -229,6 +231,7 @@ export const getServerSideProps = async (context) => {
|
|||||||
startTime: updatedItem.shift.startTime.toISOString(),
|
startTime: updatedItem.shift.startTime.toISOString(),
|
||||||
endTime: updatedItem.shift.endTime.toISOString()
|
endTime: updatedItem.shift.endTime.toISOString()
|
||||||
};
|
};
|
||||||
|
updatedItem.isPublished = updatedItem.shift.isPublished;
|
||||||
}
|
}
|
||||||
|
|
||||||
return updatedItem;
|
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);
|
||||||
console.log("First availability startTime: " + items[0]?.startTime.toLocaleString());
|
console.log("First availability startTime: " + items[0]?.startTime.toLocaleString());
|
||||||
|
|
||||||
|
|
||||||
const prisma = common.getPrismaClient();
|
const prisma = common.getPrismaClient();
|
||||||
let cartEvents = await prisma.cartEvent.findMany({
|
let cartEvents = await prisma.cartEvent.findMany({
|
||||||
where: {
|
where: {
|
||||||
@ -253,11 +257,23 @@ export const getServerSideProps = async (context) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
cartEvents = common.convertDatesToISOStrings(cartEvents);
|
cartEvents = common.convertDatesToISOStrings(cartEvents);
|
||||||
|
const lastPublishedDate = (await prisma.shift.findFirst({
|
||||||
|
where: {
|
||||||
|
isPublished: true,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
endTime: true,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
endTime: 'desc'
|
||||||
|
}
|
||||||
|
})).endTime;
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
initialItems: items,
|
initialItems: items,
|
||||||
userId: sessionServer?.user.id,
|
userId: sessionServer?.user.id,
|
||||||
cartEvents: cartEvents,
|
cartEvents: cartEvents,
|
||||||
|
lastPublishedDate: lastPublishedDate.toISOString(),
|
||||||
// messages: (await import(`../content/i18n/${context.locale}.json`)).default
|
// messages: (await import(`../content/i18n/${context.locale}.json`)).default
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user