transport toggle on calendar working

This commit is contained in:
Dobromir Popov
2024-03-06 00:57:32 +02:00
parent cf17cc74a2
commit 07e4b1b80f
2 changed files with 32 additions and 18 deletions

View File

@ -11,5 +11,5 @@ TELEGRAM_BOT=true
SSL_KEY=./certificates/localhost-key.pem SSL_KEY=./certificates/localhost-key.pem
SSL_CERT=./certificates/localhost.pem SSL_CERT=./certificates/localhost.pem
# DATABASE_URL=mysql://root:Zelen0ku4e@192.168.0.10:3306/cart_dev DATABASE_URL=mysql://root:Zelen0ku4e@192.168.0.10:3306/cart_dev
DATABASE_URL=mysql://cart:cartpw@localhost:3306/cart # DATABASE_URL=mysql://cart:cartpw@localhost:3306/cart

View File

@ -2,6 +2,8 @@ import React, { useState, useEffect } from 'react';
import axiosInstance from '../../src/axiosSecure'; import axiosInstance from '../../src/axiosSecure';
import PublisherSearchBox from '../publisher/PublisherSearchBox'; // Update the path import PublisherSearchBox from '../publisher/PublisherSearchBox'; // Update the path
import { DirectionsBus } from '@mui/material'; // Import MUI CircularProgress for loading indicator
const common = require('src/helpers/common'); const common = require('src/helpers/common');
@ -52,10 +54,11 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
const [useFilterDate, setUseFilterDate] = useState(true); const [useFilterDate, setUseFilterDate] = useState(true);
const [selectedPublisher, setSelectedPublisher] = useState(null); const [selectedPublisher, setSelectedPublisher] = useState(null);
const [showCopyHint, setShowCopyHint] = useState(false); const [showCopyHint, setShowCopyHint] = useState(false);
const [transportProvided, setTransportProvided] = useState(false);
// Update assignments when shift changes // Update assignments when shift changes
useEffect(() => { useEffect(() => {
setAssignments(shift.assignments); setAssignments(shift.assignments);
setTransportProvided(!shift.requiresTransport || shift.assignments.some(ass => ass.isWithTransport));
}, [shift.assignments]); }, [shift.assignments]);
const handleShiftClick = (shiftId) => { const handleShiftClick = (shiftId) => {
@ -106,7 +109,7 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
publisher: { connect: { id: publisher.id } }, publisher: { connect: { id: publisher.id } },
shift: { connect: { id: shiftId } }, shift: { connect: { id: shiftId } },
//isactive: true, //isactive: true,
isConfirmed: true isConfirmed: true,
}; };
const { data } = await axiosInstance.post("/api/data/assignments", newAssignment); const { data } = await axiosInstance.post("/api/data/assignments", newAssignment);
// Update the 'publisher' property of the returned data with the full publisher object // Update the 'publisher' property of the returned data with the full publisher object
@ -124,6 +127,16 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
setTimeout(() => setShowCopyHint(false), 1500); setTimeout(() => setShowCopyHint(false), 1500);
}; };
async function toggleTransport(assignment): Promise<void> {
try {
assignment.isWithTransport = !assignment.isWithTransport;
const { data } = await axiosInstance.put("/api/data/assignments/" + assignment.id,
{ isWithTransport: assignment.isWithTransport })
.then(() => {
setTransportProvided(assignments.some(ass => ass.isWithTransport))
});
} catch (error) { }
}
@ -156,6 +169,7 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
// Determine border styles // Determine border styles
let borderStyles = ''; let borderStyles = '';
let canTransport = false;
if (selectedPublisher && selectedPublisher.id === ass.publisher.id) { if (selectedPublisher && selectedPublisher.id === ass.publisher.id) {
borderStyles += 'border-2 border-blue-300'; // Bottom border for selected publishers borderStyles += 'border-2 border-blue-300'; // Bottom border for selected publishers
} }
@ -172,10 +186,12 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
else { else {
// checkig if the publisher is available for this assignment // checkig if the publisher is available for this assignment
if (publisherInfo.availabilities?.some(av => const av = publisherInfo.availabilities?.find(av =>
av.startTime <= shift.startTime && av.startTime <= shift.startTime && av.endTime >= shift.endTime
av.endTime >= shift.endTime)) { );
if (av) {
borderStyles += 'border-l-2 border-blue-500 '; // Left border for specific availability conditions borderStyles += 'border-l-2 border-blue-500 '; // Left border for specific availability conditions
ass.canTransport = av.isWithTransportIn || av.isWithTransportOut;
} }
if (publisherInfo.hasUpToDateAvailabilities) { if (publisherInfo.hasUpToDateAvailabilities) {
@ -194,9 +210,6 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
} }
} }
function toggleTransport(id: any): void {
throw new Error('Function not implemented.');
}
return ( return (
<div key={index} <div key={index}
@ -206,13 +219,14 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
<span className="text-gray-700">{publisherInfo.firstName} {publisherInfo.lastName}</span> <span className="text-gray-700">{publisherInfo.firstName} {publisherInfo.lastName}</span>
<div className="flex items-left" > <div className="flex items-left" >
{/* //if shift.isWithTransport, add trnsport button toggle, which sets ass.isWithTransportIn */} {/* //if shift.isWithTransport, add trnsport button toggle, which sets ass.isWithTransportIn */}
{shift.requiresTransport && {shift.requiresTransport && (
( <span
<button onClick={() => toggleTransport(ass.id)} className={`text-white hover:bg-orange-600 px-3 py-1 ml-2 rounded-md ${ass.isWithTransport ? 'bg-green-500' : 'bg-orange-500'}`} > onClick={ass.canTransport ? () => toggleTransport(ass) : undefined}
транспорт className={`material-icons ${ass.isWithTransport || ass.canTransport ? 'text-green-500 font-bold' : (transportProvided ? 'text-gray-400 ' : 'text-orange-400 font-bold')} ${ass.canTransport ? ' cursor-pointer' : 'cursor-not-allowed'} px-3 py-1 ml-2 rounded-md`}
</button> >
) {ass.isWithTransport ? "транспорт" : ass.canTransport ? "може транспорт" : "без транспорт"}
} </span>
)}
<button onClick={() => removeAssignment(ass.id)} className="text-white bg-red-500 hover:bg-red-600 px-3 py-1 ml-2 rounded-md" > <button onClick={() => removeAssignment(ass.id)} className="text-white bg-red-500 hover:bg-red-600 px-3 py-1 ml-2 rounded-md" >
махни махни
</button> </button>
@ -257,7 +271,7 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a
showList={false} showList={false}
/> />
</Modal> </Modal>
</div> </div >
); );
} }