fix and consolidate calendar dates usage and format
This commit is contained in:
@ -58,7 +58,9 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const updatedEvents = events.map(event => ({
|
const updatedEvents = events.map(event => ({
|
||||||
...event,
|
...event,
|
||||||
date: new Date(event.startTime).setHours(0, 0, 0, 0)
|
date: new Date(event.startTime).setHours(0, 0, 0, 0),
|
||||||
|
startTime: new Date(event.startTime),
|
||||||
|
endTime: event.endTime ? new Date(event.endTime) : undefined
|
||||||
}));
|
}));
|
||||||
|
|
||||||
setEvents(updatedEvents);
|
setEvents(updatedEvents);
|
||||||
@ -164,12 +166,15 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
|
|||||||
const totalHours = maxHour - minHour;
|
const totalHours = maxHour - minHour;
|
||||||
|
|
||||||
const handleSelect = ({ start, end }) => {
|
const handleSelect = ({ start, end }) => {
|
||||||
|
const startdate = typeof start === 'string' ? new Date(start) : start;
|
||||||
|
const enddate = typeof end === 'string' ? new Date(end) : end;
|
||||||
|
|
||||||
if (!start || !end) return;
|
if (!start || !end) return;
|
||||||
if (start < new Date() || end < new Date() || start > end) return;
|
if (startdate < new Date() || end < new Date() || startdate > end) return;
|
||||||
|
|
||||||
// Check if start and end are on the same day
|
// Check if start and end are on the same day
|
||||||
if (start.toDateString() !== end.toDateString()) {
|
if (startdate.toDateString() !== enddate.toDateString()) {
|
||||||
end = common.setTimeHHmm(start, "23:59");
|
end = common.setTimeHHmm(startdate, "23:59");
|
||||||
}
|
}
|
||||||
|
|
||||||
const startMinutes = common.getTimeInMinutes(start);
|
const startMinutes = common.getTimeInMinutes(start);
|
||||||
@ -186,7 +191,7 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
|
|||||||
setDate(start);
|
setDate(start);
|
||||||
|
|
||||||
// get exising events for the selected date
|
// get exising events for the selected date
|
||||||
const existingEvents = evts?.filter(event => (event.publisher?.id || event.publisherId) === publisherId && new Date(event.date).toDateString() === start.toDateString());
|
const existingEvents = evts?.filter(event => (event.publisher?.id || event.publisherId) === publisherId && new Date(event.date).toDateString() === startdate.toDateString());
|
||||||
// const existingEvents = evts?.filter(event => {
|
// const existingEvents = evts?.filter(event => {
|
||||||
// return event.publisherId === publisherId &&
|
// return event.publisherId === publisherId &&
|
||||||
// new Date(event.startTime).getFullYear() === start.getFullYear() &&
|
// new Date(event.startTime).getFullYear() === start.getFullYear() &&
|
||||||
|
@ -7,7 +7,7 @@ import axiosInstance from '../../src/axiosSecure';
|
|||||||
import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../src/helpers/const"
|
import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../src/helpers/const"
|
||||||
import common from "../../src/helpers/common";
|
import common from "../../src/helpers/common";
|
||||||
|
|
||||||
type Assignment = {
|
type Assignments = {
|
||||||
items: {
|
items: {
|
||||||
[key: string]: any[];
|
[key: string]: any[];
|
||||||
};
|
};
|
||||||
@ -18,7 +18,7 @@ type Assignment = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type ShiftsListProps = {
|
type ShiftsListProps = {
|
||||||
assignments: Assignment;
|
assignments: Assignments;
|
||||||
selectedtab: string;
|
selectedtab: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,11 +4,11 @@ import ProtectedRoute from '../../../components/protectedRoute';
|
|||||||
import { UserRole } from '@prisma/client';
|
import { UserRole } from '@prisma/client';
|
||||||
import axiosServer from '../../../src/axiosServer';
|
import axiosServer from '../../../src/axiosServer';
|
||||||
import common from '../../../src/helpers/common';
|
import common from '../../../src/helpers/common';
|
||||||
import { ShiftsList } from '../../../components/publisher/ShiftsList';
|
import ShiftsList from '../../../components/publisher/ShiftsList';
|
||||||
|
|
||||||
import { useSession, getSession } from 'next-auth/react';
|
import { useSession, getSession } from 'next-auth/react';
|
||||||
|
|
||||||
export default function MySchedulePage({ shifts }) {
|
export default function MySchedulePage({ assignments }) {
|
||||||
|
|
||||||
const { data: session, status } = useSession();
|
const { data: session, status } = useSession();
|
||||||
if (status === "loading") {
|
if (status === "loading") {
|
||||||
@ -20,7 +20,7 @@ export default function MySchedulePage({ shifts }) {
|
|||||||
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER, UserRole.USER]}>
|
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER, UserRole.USER]}>
|
||||||
<div className="container mx-auto p-4">
|
<div className="container mx-auto p-4">
|
||||||
<h1 className="text-xl font-semibold mb-4">Моите смени</h1>
|
<h1 className="text-xl font-semibold mb-4">Моите смени</h1>
|
||||||
<ShiftsList assignments={shifts} selectedtab={common.getCurrentYearMonth()} />
|
<ShiftsList assignments={assignments} selectedtab={common.getCurrentYearMonth()} />
|
||||||
</div>
|
</div>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
</Layout>
|
</Layout>
|
||||||
@ -67,6 +67,7 @@ export const getServerSideProps = async (context) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const assignments = publisher[0]?.assignments;
|
const assignments = publisher[0]?.assignments;
|
||||||
|
|
||||||
const transformedAssignments = assignments.map(assignment => {
|
const transformedAssignments = assignments.map(assignment => {
|
||||||
if (assignment.shift && assignment.shift.startTime) {
|
if (assignment.shift && assignment.shift.startTime) {
|
||||||
return {
|
return {
|
||||||
@ -81,10 +82,26 @@ export const getServerSideProps = async (context) => {
|
|||||||
return assignment;
|
return assignment;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const singleKey = "all";
|
||||||
|
const assignmentsStructure = {
|
||||||
|
assignments: {
|
||||||
|
items: {
|
||||||
|
[singleKey]: [],
|
||||||
|
},
|
||||||
|
keys: [singleKey],
|
||||||
|
months: {
|
||||||
|
[singleKey]: "Всички смени",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
selectedtab: singleKey,
|
||||||
|
};
|
||||||
|
|
||||||
|
transformedAssignments.forEach(assignment => {
|
||||||
|
assignmentsStructure.assignments.items[singleKey].push(assignment);
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: assignmentsStructure
|
||||||
assignments: transformedAssignments,
|
}
|
||||||
},
|
}
|
||||||
};
|
|
||||||
};
|
|
||||||
|
Reference in New Issue
Block a user