// frontend/src/pages/ShiftPlans/ShiftPlanEdit.tsx import React, { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { shiftPlanService } from '../../services/shiftPlanService'; import { ShiftPlan, Shift, ScheduledShift } from '../../models/ShiftPlan'; import { useNotification } from '../../contexts/NotificationContext'; import { useBackendValidation } from '../../hooks/useBackendValidation'; const ShiftPlanEdit: React.FC = () => { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const { showNotification, confirmDialog } = useNotification(); const { executeWithValidation, isSubmitting } = useBackendValidation(); const [shiftPlan, setShiftPlan] = useState(null); const [loading, setLoading] = useState(true); const [editingShift, setEditingShift] = useState(null); const [newShift, setNewShift] = useState>({ timeSlotId: '', dayOfWeek: 1, requiredEmployees: 1 }); useEffect(() => { loadShiftPlan(); }, [id]); const loadShiftPlan = async () => { if (!id) return; await executeWithValidation(async () => { try { const plan = await shiftPlanService.getShiftPlan(id); setShiftPlan(plan); } catch (error) { console.error('Error loading shift plan:', error); navigate('/shift-plans'); } finally { setLoading(false); } }); }; const handleUpdateShift = async (shift: Shift) => { if (!shiftPlan || !id) return; await executeWithValidation(async () => { // Update logic here - will be implemented when backend API is available // For now, just simulate success console.log('Updating shift:', shift); loadShiftPlan(); setEditingShift(null); showNotification({ type: 'success', title: 'Erfolg', message: 'Schicht wurde erfolgreich aktualisiert.' }); }); }; const handleAddShift = async () => { if (!shiftPlan || !id) return; // Basic frontend validation only if (!newShift.timeSlotId) { showNotification({ type: 'error', title: 'Fehlende Angaben', message: 'Bitte wählen Sie einen Zeit-Slot aus.' }); return; } if (!newShift.requiredEmployees || newShift.requiredEmployees < 1) { showNotification({ type: 'error', title: 'Fehlende Angaben', message: 'Bitte geben Sie die Anzahl der benötigten Mitarbeiter an.' }); return; } await executeWithValidation(async () => { // Add shift logic here - will be implemented when backend API is available // For now, just simulate success console.log('Adding shift:', newShift); showNotification({ type: 'success', title: 'Erfolg', message: 'Neue Schicht wurde hinzugefügt.' }); setNewShift({ timeSlotId: '', dayOfWeek: 1, requiredEmployees: 1 }); loadShiftPlan(); }); }; const handleDeleteShift = async (shiftId: string) => { const confirmed = await confirmDialog({ title: 'Schicht löschen', message: 'Möchten Sie diese Schicht wirklich löschen?', confirmText: 'Löschen', cancelText: 'Abbrechen', type: 'warning' }); if (!confirmed) return; await executeWithValidation(async () => { // Delete logic here - will be implemented when backend API is available // For now, just simulate success console.log('Deleting shift:', shiftId); loadShiftPlan(); showNotification({ type: 'success', title: 'Erfolg', message: 'Schicht wurde erfolgreich gelöscht.' }); }); }; const handlePublish = async () => { if (!shiftPlan || !id) return; await executeWithValidation(async () => { await shiftPlanService.updateShiftPlan(id, { ...shiftPlan, status: 'published' }); showNotification({ type: 'success', title: 'Erfolg', message: 'Schichtplan wurde veröffentlicht.' }); loadShiftPlan(); }); }; if (loading) { return (
Lade Schichtplan...
); } if (!shiftPlan) { return (
Schichtplan nicht gefunden
); } // Group shifts by dayOfWeek const shiftsByDay = shiftPlan.shifts.reduce((acc, shift) => { if (!acc[shift.dayOfWeek]) { acc[shift.dayOfWeek] = []; } acc[shift.dayOfWeek].push(shift); return acc; }, {} as Record); const daysOfWeek = [ { id: 1, name: 'Montag' }, { id: 2, name: 'Dienstag' }, { id: 3, name: 'Mittwoch' }, { id: 4, name: 'Donnerstag' }, { id: 5, name: 'Freitag' }, { id: 6, name: 'Samstag' }, { id: 7, name: 'Sonntag' } ]; return (

{shiftPlan.name} bearbeiten

{shiftPlan.status === 'draft' && ( )}
{/* Add new shift form */}

Neue Schicht hinzufügen

setNewShift({ ...newShift, requiredEmployees: parseInt(e.target.value) || 1 })} style={{ width: '100%', padding: '8px', borderRadius: '4px', border: '1px solid #ddd' }} disabled={isSubmitting} />
{/* Existing shifts */}
{daysOfWeek.map(day => { const shifts = shiftsByDay[day.id] || []; if (shifts.length === 0) return null; return (

{day.name}

{shifts.map(shift => { const timeSlot = shiftPlan.timeSlots.find(ts => ts.id === shift.timeSlotId); return (
{editingShift?.id === shift.id ? (
setEditingShift({ ...editingShift, requiredEmployees: parseInt(e.target.value) || 1 })} style={{ width: '100%', padding: '8px', borderRadius: '4px', border: '1px solid #ddd' }} disabled={isSubmitting} />
) : ( <>
{timeSlot?.name} ({timeSlot?.startTime?.substring(0, 5)} - {timeSlot?.endTime?.substring(0, 5)})
Benötigte Mitarbeiter: {shift.requiredEmployees}
)}
); })}
); })}
); }; export default ShiftPlanEdit;