fix calenar UI bug
This commit is contained in:
@ -87,27 +87,29 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
try {
|
try {
|
||||||
const groupedTimeSlots = mergeCheckedTimeSlots(timeSlots);
|
const groupedTimeSlots = mergeCheckedTimeSlots(timeSlots);
|
||||||
|
let avs = availabilities.filter(av => av.type !== "assignment");
|
||||||
// Determine if we need to delete and recreate, or just update
|
// Determine if we need to delete and recreate, or just update
|
||||||
const shouldRecreate = availabilities.length !== groupedTimeSlots.length || availabilities.some(av => !av.id);
|
let shouldRecreate = avs.length > 0 && avs.length !== groupedTimeSlots.length || avs.some(av => !av.id);
|
||||||
|
shouldRecreate = shouldRecreate || ( avs.length == 0 && availabilities.length > 0);
|
||||||
|
//create availability if we open a form with assignment without availability
|
||||||
|
|
||||||
if (shouldRecreate) {
|
if (shouldRecreate) {
|
||||||
// Delete existing availabilities if they have an ID
|
// Delete existing availabilities if they have an ID
|
||||||
console.log("Recreating availabilities");
|
console.log("Recreating availabilities");
|
||||||
await Promise.all(availabilities.filter(av => av.id).map(av => axiosInstance.delete(`${urls.apiUrl}${av.id}`)));
|
await Promise.all(avs.filter(av => av.id).map(av => axiosInstance.delete(`${urls.apiUrl}${av.id}`)));
|
||||||
|
|
||||||
// Create new availabilities
|
// Create new availabilities
|
||||||
const createdAvailabilities = await Promise.all(groupedTimeSlots.map(async group => {
|
avs = await Promise.all(groupedTimeSlots.map(async group => {
|
||||||
const newAvailability = createAvailabilityFromGroup(group, publisher.id);
|
const newAvailability = createAvailabilityFromGroup(group, publisher.id);
|
||||||
const response = await axiosInstance.post(urls.apiUrl, newAvailability);
|
const response = await axiosInstance.post(urls.apiUrl, newAvailability);
|
||||||
return response.data; // Assuming the new availability is returned
|
return response.data; // Assuming the new availability is returned
|
||||||
}));
|
}));
|
||||||
|
|
||||||
setAvailabilities(createdAvailabilities);
|
setAvailabilities(avs);
|
||||||
} else {
|
} else {
|
||||||
// Update existing availabilities
|
// Update existing availabilities
|
||||||
console.log("Updating existing availabilities");
|
console.log("Updating existing availabilities");
|
||||||
const updatedAvailabilities = await Promise.all(availabilities.map(async (availability, index) => {
|
avs = await Promise.all(avs.map(async (availability, index) => {
|
||||||
const group = groupedTimeSlots[index];
|
const group = groupedTimeSlots[index];
|
||||||
const id = availability.id;
|
const id = availability.id;
|
||||||
const updatedAvailability = updateAvailabilityFromGroup(availability, group);
|
const updatedAvailability = updateAvailabilityFromGroup(availability, group);
|
||||||
@ -121,7 +123,7 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
|||||||
return updatedAvailability;
|
return updatedAvailability;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
setAvailabilities(updatedAvailabilities);
|
setAvailabilities(avs);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCompletion({ updated: true });
|
handleCompletion({ updated: true });
|
||||||
@ -228,7 +230,8 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
|||||||
const handleDelete = async (e) => {
|
const handleDelete = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
try {
|
try {
|
||||||
const deletePromises = availabilities.map(async (availability) => {
|
let avs = availabilities.filter(av => av.type !== "assignment");
|
||||||
|
const deletePromises = avs.map(async (availability) => {
|
||||||
if (availability.id) {
|
if (availability.id) {
|
||||||
// console.log("deleting publisher id = ", router.query.id, "; url=" + urls.apiUrl + router.query.id);
|
// console.log("deleting publisher id = ", router.query.id, "; url=" + urls.apiUrl + router.query.id);
|
||||||
await axiosInstance.delete(urls.apiUrl + availability.id);
|
await axiosInstance.delete(urls.apiUrl + availability.id);
|
||||||
|
@ -56,7 +56,14 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
|
|||||||
|
|
||||||
// Update internal state when `events` prop changes
|
// Update internal state when `events` prop changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const updatedEvents = events.map(event => ({
|
//if we have isBySystem - set type to assignment
|
||||||
|
let updatedEvents = events.map(event => {
|
||||||
|
if (event.isBySystem) {
|
||||||
|
event.type = "assignment";
|
||||||
|
}
|
||||||
|
return event;
|
||||||
|
});
|
||||||
|
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),
|
startTime: new Date(event.startTime),
|
||||||
|
Reference in New Issue
Block a user