Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
173cfbc
1
Parent(s): d985d6d
fix: simplify BookingModal - calendar picker, remove service selection, fix lag
Browse files
frontend/components/bookings/BookingModal.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
import { useState, useEffect, useMemo, useCallback } from 'react'
|
| 4 |
-
import { X, Calendar as CalendarIcon, Clock, Loader2, Check, User,
|
| 5 |
-
import { format, addDays, startOfToday, isSameDay, getDay } from 'date-fns'
|
| 6 |
import { ru } from 'date-fns/locale'
|
| 7 |
import { motion, AnimatePresence } from 'framer-motion'
|
| 8 |
import { bookingsApi } from '@/lib/api'
|
|
@@ -22,28 +22,14 @@ interface Resource {
|
|
| 22 |
working_hours?: Record<string, { start: string; end: string }>
|
| 23 |
}
|
| 24 |
|
| 25 |
-
interface Service {
|
| 26 |
-
id: string
|
| 27 |
-
name: string
|
| 28 |
-
duration_minutes: number
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
interface BookingSettings {
|
| 32 |
working_hours: { start: string; end: string }
|
| 33 |
-
working_days: number[]
|
| 34 |
slot_duration: number
|
| 35 |
timezone: string
|
| 36 |
}
|
| 37 |
|
| 38 |
-
|
| 39 |
-
id: string
|
| 40 |
-
start_time: string
|
| 41 |
-
status: string
|
| 42 |
-
service_name?: string
|
| 43 |
-
resource_name?: string
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
// Generate time slots from start to end with interval
|
| 47 |
function generateTimeSlots(startTime: string, endTime: string, interval: number = 30): string[] {
|
| 48 |
const slots: string[] = []
|
| 49 |
const [startH, startM] = startTime.split(':').map(Number)
|
|
@@ -62,114 +48,115 @@ function generateTimeSlots(startTime: string, endTime: string, interval: number
|
|
| 62 |
}
|
| 63 |
|
| 64 |
export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModalProps) {
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
const [selectedTime, setSelectedTime] = useState<string | null>(null)
|
| 67 |
const [selectedResource, setSelectedResource] = useState<string | null>(null)
|
| 68 |
const [bookingNote, setBookingNote] = useState('')
|
| 69 |
const [isSubmitting, setIsSubmitting] = useState(false)
|
| 70 |
const [isSuccess, setIsSuccess] = useState(false)
|
| 71 |
|
| 72 |
-
// Data
|
| 73 |
-
const [services, setServices] = useState<Service[]>([])
|
| 74 |
const [resources, setResources] = useState<Resource[]>([])
|
| 75 |
const [bookingSettings, setBookingSettings] = useState<BookingSettings | null>(null)
|
| 76 |
-
const [existingAppointments, setExistingAppointments] = useState<ExistingAppointment[]>([])
|
| 77 |
-
const [selectedService, setSelectedService] = useState<string | null>(null)
|
| 78 |
const [isLoading, setIsLoading] = useState(true)
|
| 79 |
|
| 80 |
-
// Load data
|
| 81 |
useEffect(() => {
|
| 82 |
if (!isOpen) return
|
| 83 |
|
| 84 |
setIsSuccess(false)
|
|
|
|
|
|
|
| 85 |
setIsLoading(true)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
]
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
console.error('Failed to load booking data:', e)
|
| 103 |
-
setServices([{ id: 'default', name: 'Консультация', duration_minutes: 30 }])
|
| 104 |
-
setSelectedService('default')
|
| 105 |
-
setBookingSettings({
|
| 106 |
-
working_hours: { start: '09:00', end: '18:00' },
|
| 107 |
-
working_days: [0, 1, 2, 3, 4],
|
| 108 |
-
slot_duration: 30,
|
| 109 |
-
timezone: 'Asia/Bishkek'
|
| 110 |
-
})
|
| 111 |
-
} finally {
|
| 112 |
-
setIsLoading(false)
|
| 113 |
-
}
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
loadData()
|
| 117 |
-
}, [isOpen, leadId])
|
| 118 |
-
|
| 119 |
-
// Memoized dates (next 14 days)
|
| 120 |
-
const dates = useMemo(() => {
|
| 121 |
-
return Array.from({ length: 14 }).map((_, i) => addDays(startOfToday(), i))
|
| 122 |
-
}, [])
|
| 123 |
|
| 124 |
-
//
|
| 125 |
const selectedResourceData = useMemo(() => {
|
| 126 |
if (!selectedResource) return null
|
| 127 |
return resources.find(r => r.id === selectedResource)
|
| 128 |
}, [selectedResource, resources])
|
| 129 |
|
| 130 |
-
//
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
const dayKey = String(jsDay === 0 ? 6 : jsDay - 1)
|
| 136 |
|
| 137 |
-
// If resource selected, use its working hours
|
| 138 |
if (selectedResourceData?.working_hours) {
|
| 139 |
-
|
| 140 |
-
if (!resourceHours) return null // Day off for resource
|
| 141 |
-
return resourceHours
|
| 142 |
}
|
| 143 |
|
| 144 |
-
// Otherwise use company settings
|
| 145 |
if (bookingSettings) {
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
if (!bookingSettings.working_days.includes(dayIndex)) {
|
| 149 |
-
return null // Day off for company
|
| 150 |
-
}
|
| 151 |
return bookingSettings.working_hours
|
| 152 |
}
|
| 153 |
|
| 154 |
return { start: '09:00', end: '18:00' }
|
| 155 |
}, [selectedResourceData, bookingSettings])
|
| 156 |
|
| 157 |
-
//
|
| 158 |
-
const isWorkingDay = useCallback((date: Date) => {
|
| 159 |
-
return getWorkingHoursForDate(date) !== null
|
| 160 |
-
}, [getWorkingHoursForDate])
|
| 161 |
-
|
| 162 |
-
// Memoized time slots based on selected date and resource/company settings
|
| 163 |
const timeSlots = useMemo(() => {
|
| 164 |
-
|
|
|
|
| 165 |
if (!hours) return []
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
const handleBook = useCallback(async () => {
|
| 172 |
-
if (!selectedDate || !selectedTime
|
| 173 |
|
| 174 |
setIsSubmitting(true)
|
| 175 |
try {
|
|
@@ -177,13 +164,12 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 177 |
const startDateTime = new Date(selectedDate)
|
| 178 |
startDateTime.setHours(hours, minutes, 0, 0)
|
| 179 |
|
| 180 |
-
const
|
| 181 |
-
const duration = service?.duration_minutes || 30
|
| 182 |
const endDateTime = new Date(startDateTime.getTime() + duration * 60000)
|
| 183 |
|
| 184 |
await bookingsApi.createAppointment({
|
| 185 |
lead_id: leadId,
|
| 186 |
-
service_id:
|
| 187 |
resource_id: selectedResource || null,
|
| 188 |
start_time: startDateTime.toISOString(),
|
| 189 |
end_time: endDateTime.toISOString(),
|
|
@@ -197,17 +183,10 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 197 |
} finally {
|
| 198 |
setIsSubmitting(false)
|
| 199 |
}
|
| 200 |
-
}, [selectedDate, selectedTime,
|
| 201 |
-
|
| 202 |
-
// Reset time when date or resource changes
|
| 203 |
-
useEffect(() => {
|
| 204 |
-
setSelectedTime(null)
|
| 205 |
-
}, [selectedDate, selectedResource])
|
| 206 |
|
| 207 |
if (!isOpen) return null
|
| 208 |
|
| 209 |
-
const hasExistingBooking = existingAppointments.length > 0
|
| 210 |
-
|
| 211 |
return (
|
| 212 |
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
|
| 213 |
<motion.div
|
|
@@ -215,13 +194,13 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 215 |
animate={{ opacity: 1 }}
|
| 216 |
exit={{ opacity: 0 }}
|
| 217 |
onClick={!isSubmitting ? onClose : undefined}
|
| 218 |
-
className="absolute inset-0 bg-black/80 backdrop-blur-
|
| 219 |
/>
|
| 220 |
|
| 221 |
<motion.div
|
| 222 |
initial={{ opacity: 0, scale: 0.95 }}
|
| 223 |
animate={{ opacity: 1, scale: 1 }}
|
| 224 |
-
className="relative bg-[#
|
| 225 |
>
|
| 226 |
<AnimatePresence mode="wait">
|
| 227 |
{isSuccess ? (
|
|
@@ -229,113 +208,66 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 229 |
key="success"
|
| 230 |
initial={{ opacity: 0, y: 20 }}
|
| 231 |
animate={{ opacity: 1, y: 0 }}
|
| 232 |
-
className="p-
|
| 233 |
>
|
| 234 |
-
<div className="w-
|
| 235 |
-
<Check className="w-
|
| 236 |
</div>
|
| 237 |
-
<h3 className="text-
|
| 238 |
-
<p className="text-slate-400
|
| 239 |
-
|
| 240 |
-
<span className="text-indigo-400 font-bold">{selectedTime}</span>{' '}
|
| 241 |
-
{format(selectedDate, 'd MMMM', { locale: ru })}
|
| 242 |
-
{selectedResourceData && (
|
| 243 |
-
<span className="text-slate-500"> • {selectedResourceData.name}</span>
|
| 244 |
-
)}
|
| 245 |
</p>
|
| 246 |
<button
|
| 247 |
onClick={onClose}
|
| 248 |
-
className="px-
|
| 249 |
>
|
| 250 |
Закрыть
|
| 251 |
</button>
|
| 252 |
</motion.div>
|
| 253 |
) : isLoading ? (
|
| 254 |
-
<div className="p-
|
| 255 |
-
<Loader2 className="w-
|
| 256 |
</div>
|
| 257 |
) : (
|
| 258 |
-
<motion.div key="form"
|
| 259 |
{/* Header */}
|
| 260 |
-
<div className="px-
|
| 261 |
<div>
|
| 262 |
-
<h3 className="font-bold text-
|
| 263 |
-
<CalendarIcon className="w-
|
| 264 |
Запись: {leadName || 'Клиент'}
|
| 265 |
</h3>
|
| 266 |
-
<p className="text-xs text-slate-500 mt-0.5">
|
| 267 |
-
{bookingSettings?.timezone && `Часовой пояс: ${bookingSettings.timezone}`}
|
| 268 |
-
</p>
|
| 269 |
</div>
|
| 270 |
-
<button onClick={onClose} className="p-
|
| 271 |
<X className="w-5 h-5" />
|
| 272 |
</button>
|
| 273 |
</div>
|
| 274 |
|
| 275 |
-
{/* Existing booking warning */}
|
| 276 |
-
{hasExistingBooking && (
|
| 277 |
-
<div className="mx-6 mt-4 p-3 bg-amber-500/10 border border-amber-500/30 rounded-xl flex items-start gap-3">
|
| 278 |
-
<AlertCircle className="w-5 h-5 text-amber-400 shrink-0 mt-0.5" />
|
| 279 |
-
<div>
|
| 280 |
-
<p className="text-sm font-medium text-amber-300">Клиент уже записан</p>
|
| 281 |
-
<p className="text-xs text-amber-400/70 mt-0.5">
|
| 282 |
-
{existingAppointments[0].service_name || 'Встреча'} • {' '}
|
| 283 |
-
{format(new Date(existingAppointments[0].start_time), 'd MMM HH:mm', { locale: ru })}
|
| 284 |
-
{existingAppointments[0].resource_name && ` • ${existingAppointments[0].resource_name}`}
|
| 285 |
-
</p>
|
| 286 |
-
</div>
|
| 287 |
-
</div>
|
| 288 |
-
)}
|
| 289 |
-
|
| 290 |
{/* Content */}
|
| 291 |
-
<div className="
|
| 292 |
-
{/* Services */}
|
| 293 |
-
<section>
|
| 294 |
-
<label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-3">Услуга</label>
|
| 295 |
-
<div className="flex flex-wrap gap-2">
|
| 296 |
-
{services.map(s => (
|
| 297 |
-
<button
|
| 298 |
-
key={s.id}
|
| 299 |
-
onClick={() => setSelectedService(s.id)}
|
| 300 |
-
className={cn(
|
| 301 |
-
"px-4 py-2.5 rounded-xl border text-sm font-medium transition-all",
|
| 302 |
-
selectedService === s.id
|
| 303 |
-
? "bg-indigo-500/20 border-indigo-500/50 text-white"
|
| 304 |
-
: "bg-white/5 border-white/10 text-slate-400 hover:bg-white/10"
|
| 305 |
-
)}
|
| 306 |
-
>
|
| 307 |
-
{s.name}
|
| 308 |
-
<span className="text-xs text-slate-500 ml-2">{s.duration_minutes} мин</span>
|
| 309 |
-
</button>
|
| 310 |
-
))}
|
| 311 |
-
</div>
|
| 312 |
-
</section>
|
| 313 |
-
|
| 314 |
{/* Resources */}
|
| 315 |
{resources.length > 0 && (
|
| 316 |
-
<
|
| 317 |
-
<label className="block text-xs font-
|
| 318 |
-
<User className="w-3.5 h-3.5 inline mr-1" />
|
| 319 |
-
Мастер / Ресурс
|
| 320 |
-
</label>
|
| 321 |
<div className="flex flex-wrap gap-2">
|
| 322 |
<button
|
| 323 |
onClick={() => setSelectedResource(null)}
|
| 324 |
className={cn(
|
| 325 |
-
"px-
|
| 326 |
!selectedResource
|
| 327 |
? "bg-indigo-500/20 border-indigo-500/50 text-white"
|
| 328 |
: "bg-white/5 border-white/10 text-slate-400 hover:bg-white/10"
|
| 329 |
)}
|
| 330 |
>
|
| 331 |
-
|
| 332 |
</button>
|
| 333 |
{resources.map(r => (
|
| 334 |
<button
|
| 335 |
key={r.id}
|
| 336 |
onClick={() => setSelectedResource(r.id)}
|
| 337 |
className={cn(
|
| 338 |
-
"px-
|
| 339 |
selectedResource === r.id
|
| 340 |
? "bg-indigo-500/20 border-indigo-500/50 text-white"
|
| 341 |
: "bg-white/5 border-white/10 text-slate-400 hover:bg-white/10"
|
|
@@ -345,66 +277,89 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 345 |
</button>
|
| 346 |
))}
|
| 347 |
</div>
|
| 348 |
-
|
| 349 |
-
<p className="text-xs text-slate-600 mt-2">
|
| 350 |
-
Показано расписание: {selectedResourceData.name}
|
| 351 |
-
</p>
|
| 352 |
-
)}
|
| 353 |
-
</section>
|
| 354 |
)}
|
| 355 |
|
| 356 |
-
{/*
|
| 357 |
-
<div
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
<
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
)}
|
| 377 |
-
>
|
| 378 |
-
<span className="text-[10px] font-semibold uppercase">{format(date, 'eee', { locale: ru })}</span>
|
| 379 |
-
<span className="text-lg font-bold">{format(date, 'd')}</span>
|
| 380 |
-
{!isWorking && <span className="text-[8px] text-slate-700">выходной</span>}
|
| 381 |
-
</button>
|
| 382 |
-
)
|
| 383 |
-
})}
|
| 384 |
</div>
|
| 385 |
-
</
|
| 386 |
-
|
| 387 |
-
{/*
|
| 388 |
-
<
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
</label>
|
| 395 |
{timeSlots.length === 0 ? (
|
| 396 |
-
<p className="text-sm text-slate-500">
|
| 397 |
) : (
|
| 398 |
-
<div className="grid grid-cols-
|
| 399 |
{timeSlots.map(time => (
|
| 400 |
<button
|
| 401 |
key={time}
|
| 402 |
onClick={() => setSelectedTime(time)}
|
| 403 |
className={cn(
|
| 404 |
-
"py-2 rounded-
|
| 405 |
selectedTime === time
|
| 406 |
-
? "bg-indigo-600
|
| 407 |
-
: "bg-white/5 text-slate-400
|
| 408 |
)}
|
| 409 |
>
|
| 410 |
{time}
|
|
@@ -412,34 +367,35 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 412 |
))}
|
| 413 |
</div>
|
| 414 |
)}
|
| 415 |
-
</
|
| 416 |
-
|
| 417 |
|
| 418 |
{/* Notes */}
|
| 419 |
-
<
|
| 420 |
-
<label className="block text-xs font-
|
| 421 |
-
<
|
|
|
|
| 422 |
value={bookingNote}
|
| 423 |
onChange={(e) => setBookingNote(e.target.value)}
|
| 424 |
placeholder="Детали встречи..."
|
| 425 |
-
className="w-full
|
| 426 |
/>
|
| 427 |
-
</
|
| 428 |
</div>
|
| 429 |
|
| 430 |
{/* Footer */}
|
| 431 |
-
<div className="px-
|
| 432 |
<button
|
| 433 |
onClick={handleBook}
|
| 434 |
-
disabled={!selectedDate || !selectedTime ||
|
| 435 |
-
className="w-full py-3
|
| 436 |
>
|
| 437 |
{isSubmitting ? (
|
| 438 |
<Loader2 className="w-5 h-5 animate-spin" />
|
| 439 |
) : (
|
| 440 |
<Check className="w-5 h-5" />
|
| 441 |
)}
|
| 442 |
-
{
|
| 443 |
</button>
|
| 444 |
</div>
|
| 445 |
</motion.div>
|
|
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
import { useState, useEffect, useMemo, useCallback } from 'react'
|
| 4 |
+
import { X, Calendar as CalendarIcon, Clock, Loader2, Check, User, ChevronLeft, ChevronRight } from 'lucide-react'
|
| 5 |
+
import { format, addDays, addMonths, startOfToday, isSameDay, getDay, startOfMonth, endOfMonth, eachDayOfInterval, isBefore, startOfWeek, endOfWeek, isSameMonth } from 'date-fns'
|
| 6 |
import { ru } from 'date-fns/locale'
|
| 7 |
import { motion, AnimatePresence } from 'framer-motion'
|
| 8 |
import { bookingsApi } from '@/lib/api'
|
|
|
|
| 22 |
working_hours?: Record<string, { start: string; end: string }>
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
interface BookingSettings {
|
| 26 |
working_hours: { start: string; end: string }
|
| 27 |
+
working_days: number[]
|
| 28 |
slot_duration: number
|
| 29 |
timezone: string
|
| 30 |
}
|
| 31 |
|
| 32 |
+
// Generate time slots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
function generateTimeSlots(startTime: string, endTime: string, interval: number = 30): string[] {
|
| 34 |
const slots: string[] = []
|
| 35 |
const [startH, startM] = startTime.split(':').map(Number)
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModalProps) {
|
| 51 |
+
// State
|
| 52 |
+
const [currentMonth, setCurrentMonth] = useState(new Date())
|
| 53 |
+
const [selectedDate, setSelectedDate] = useState<Date | null>(null)
|
| 54 |
const [selectedTime, setSelectedTime] = useState<string | null>(null)
|
| 55 |
const [selectedResource, setSelectedResource] = useState<string | null>(null)
|
| 56 |
const [bookingNote, setBookingNote] = useState('')
|
| 57 |
const [isSubmitting, setIsSubmitting] = useState(false)
|
| 58 |
const [isSuccess, setIsSuccess] = useState(false)
|
| 59 |
|
| 60 |
+
// Data
|
|
|
|
| 61 |
const [resources, setResources] = useState<Resource[]>([])
|
| 62 |
const [bookingSettings, setBookingSettings] = useState<BookingSettings | null>(null)
|
|
|
|
|
|
|
| 63 |
const [isLoading, setIsLoading] = useState(true)
|
| 64 |
|
| 65 |
+
// Load data
|
| 66 |
useEffect(() => {
|
| 67 |
if (!isOpen) return
|
| 68 |
|
| 69 |
setIsSuccess(false)
|
| 70 |
+
setSelectedDate(null)
|
| 71 |
+
setSelectedTime(null)
|
| 72 |
setIsLoading(true)
|
| 73 |
|
| 74 |
+
Promise.all([
|
| 75 |
+
bookingsApi.getResources().catch(() => []),
|
| 76 |
+
bookingsApi.getBookingSettings().catch(() => null)
|
| 77 |
+
]).then(([resourcesData, settingsData]) => {
|
| 78 |
+
setResources(resourcesData || [])
|
| 79 |
+
setBookingSettings(settingsData || {
|
| 80 |
+
working_hours: { start: '09:00', end: '18:00' },
|
| 81 |
+
working_days: [0, 1, 2, 3, 4],
|
| 82 |
+
slot_duration: 30,
|
| 83 |
+
timezone: 'Asia/Bishkek'
|
| 84 |
+
})
|
| 85 |
+
}).finally(() => {
|
| 86 |
+
setIsLoading(false)
|
| 87 |
+
})
|
| 88 |
+
}, [isOpen])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
// Selected resource data
|
| 91 |
const selectedResourceData = useMemo(() => {
|
| 92 |
if (!selectedResource) return null
|
| 93 |
return resources.find(r => r.id === selectedResource)
|
| 94 |
}, [selectedResource, resources])
|
| 95 |
|
| 96 |
+
// Check if date is working day
|
| 97 |
+
const isWorkingDay = useCallback((date: Date): boolean => {
|
| 98 |
+
if (isBefore(date, startOfToday())) return false
|
| 99 |
+
|
| 100 |
+
const jsDay = getDay(date)
|
| 101 |
+
const dayKey = String(jsDay === 0 ? 6 : jsDay - 1)
|
| 102 |
+
|
| 103 |
+
if (selectedResourceData?.working_hours) {
|
| 104 |
+
return !!selectedResourceData.working_hours[dayKey]
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
if (bookingSettings) {
|
| 108 |
+
const dayIndex = jsDay === 0 ? 6 : jsDay - 1
|
| 109 |
+
return bookingSettings.working_days.includes(dayIndex)
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
return jsDay !== 0 && jsDay !== 6
|
| 113 |
+
}, [selectedResourceData, bookingSettings])
|
| 114 |
+
|
| 115 |
+
// Get working hours for date
|
| 116 |
+
const getWorkingHours = useCallback((date: Date): { start: string; end: string } | null => {
|
| 117 |
+
const jsDay = getDay(date)
|
| 118 |
const dayKey = String(jsDay === 0 ? 6 : jsDay - 1)
|
| 119 |
|
|
|
|
| 120 |
if (selectedResourceData?.working_hours) {
|
| 121 |
+
return selectedResourceData.working_hours[dayKey] || null
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
|
|
|
| 124 |
if (bookingSettings) {
|
| 125 |
+
const dayIndex = jsDay === 0 ? 6 : jsDay - 1
|
| 126 |
+
if (!bookingSettings.working_days.includes(dayIndex)) return null
|
|
|
|
|
|
|
|
|
|
| 127 |
return bookingSettings.working_hours
|
| 128 |
}
|
| 129 |
|
| 130 |
return { start: '09:00', end: '18:00' }
|
| 131 |
}, [selectedResourceData, bookingSettings])
|
| 132 |
|
| 133 |
+
// Time slots for selected date
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
const timeSlots = useMemo(() => {
|
| 135 |
+
if (!selectedDate) return []
|
| 136 |
+
const hours = getWorkingHours(selectedDate)
|
| 137 |
if (!hours) return []
|
| 138 |
+
return generateTimeSlots(hours.start, hours.end, bookingSettings?.slot_duration || 30)
|
| 139 |
+
}, [selectedDate, getWorkingHours, bookingSettings?.slot_duration])
|
| 140 |
+
|
| 141 |
+
// Calendar days
|
| 142 |
+
const calendarDays = useMemo(() => {
|
| 143 |
+
const monthStart = startOfMonth(currentMonth)
|
| 144 |
+
const monthEnd = endOfMonth(currentMonth)
|
| 145 |
+
const start = startOfWeek(monthStart, { weekStartsOn: 1 })
|
| 146 |
+
const end = endOfWeek(monthEnd, { weekStartsOn: 1 })
|
| 147 |
+
return eachDayOfInterval({ start, end })
|
| 148 |
+
}, [currentMonth])
|
| 149 |
+
|
| 150 |
+
// Handlers
|
| 151 |
+
const handleDateSelect = useCallback((date: Date) => {
|
| 152 |
+
if (isWorkingDay(date)) {
|
| 153 |
+
setSelectedDate(date)
|
| 154 |
+
setSelectedTime(null)
|
| 155 |
+
}
|
| 156 |
+
}, [isWorkingDay])
|
| 157 |
|
| 158 |
const handleBook = useCallback(async () => {
|
| 159 |
+
if (!selectedDate || !selectedTime) return
|
| 160 |
|
| 161 |
setIsSubmitting(true)
|
| 162 |
try {
|
|
|
|
| 164 |
const startDateTime = new Date(selectedDate)
|
| 165 |
startDateTime.setHours(hours, minutes, 0, 0)
|
| 166 |
|
| 167 |
+
const duration = bookingSettings?.slot_duration || 30
|
|
|
|
| 168 |
const endDateTime = new Date(startDateTime.getTime() + duration * 60000)
|
| 169 |
|
| 170 |
await bookingsApi.createAppointment({
|
| 171 |
lead_id: leadId,
|
| 172 |
+
service_id: null,
|
| 173 |
resource_id: selectedResource || null,
|
| 174 |
start_time: startDateTime.toISOString(),
|
| 175 |
end_time: endDateTime.toISOString(),
|
|
|
|
| 183 |
} finally {
|
| 184 |
setIsSubmitting(false)
|
| 185 |
}
|
| 186 |
+
}, [selectedDate, selectedTime, selectedResource, bookingSettings?.slot_duration, leadId, bookingNote])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
if (!isOpen) return null
|
| 189 |
|
|
|
|
|
|
|
| 190 |
return (
|
| 191 |
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
|
| 192 |
<motion.div
|
|
|
|
| 194 |
animate={{ opacity: 1 }}
|
| 195 |
exit={{ opacity: 0 }}
|
| 196 |
onClick={!isSubmitting ? onClose : undefined}
|
| 197 |
+
className="absolute inset-0 bg-black/80 backdrop-blur-sm"
|
| 198 |
/>
|
| 199 |
|
| 200 |
<motion.div
|
| 201 |
initial={{ opacity: 0, scale: 0.95 }}
|
| 202 |
animate={{ opacity: 1, scale: 1 }}
|
| 203 |
+
className="relative bg-[#12121a] border border-white/10 rounded-2xl w-full max-w-md overflow-hidden shadow-2xl"
|
| 204 |
>
|
| 205 |
<AnimatePresence mode="wait">
|
| 206 |
{isSuccess ? (
|
|
|
|
| 208 |
key="success"
|
| 209 |
initial={{ opacity: 0, y: 20 }}
|
| 210 |
animate={{ opacity: 1, y: 0 }}
|
| 211 |
+
className="p-8 flex flex-col items-center text-center"
|
| 212 |
>
|
| 213 |
+
<div className="w-14 h-14 rounded-full bg-emerald-500/20 border border-emerald-500/30 flex items-center justify-center mb-4">
|
| 214 |
+
<Check className="w-7 h-7 text-emerald-400" />
|
| 215 |
</div>
|
| 216 |
+
<h3 className="text-lg font-bold text-white mb-1">Запись создана</h3>
|
| 217 |
+
<p className="text-slate-400 text-sm mb-4">
|
| 218 |
+
{selectedDate && format(selectedDate, 'd MMMM', { locale: ru })} в {selectedTime}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
</p>
|
| 220 |
<button
|
| 221 |
onClick={onClose}
|
| 222 |
+
className="px-6 py-2 bg-white/10 hover:bg-white/20 text-white font-medium rounded-lg transition-all"
|
| 223 |
>
|
| 224 |
Закрыть
|
| 225 |
</button>
|
| 226 |
</motion.div>
|
| 227 |
) : isLoading ? (
|
| 228 |
+
<div className="p-16 flex items-center justify-center">
|
| 229 |
+
<Loader2 className="w-6 h-6 animate-spin text-indigo-500" />
|
| 230 |
</div>
|
| 231 |
) : (
|
| 232 |
+
<motion.div key="form">
|
| 233 |
{/* Header */}
|
| 234 |
+
<div className="px-5 py-4 border-b border-white/5 flex items-center justify-between">
|
| 235 |
<div>
|
| 236 |
+
<h3 className="font-bold text-white flex items-center gap-2">
|
| 237 |
+
<CalendarIcon className="w-4 h-4 text-indigo-400" />
|
| 238 |
Запись: {leadName || 'Клиент'}
|
| 239 |
</h3>
|
| 240 |
+
<p className="text-xs text-slate-500 mt-0.5">{getTimezoneDisplayName()}</p>
|
|
|
|
|
|
|
| 241 |
</div>
|
| 242 |
+
<button onClick={onClose} className="p-1.5 hover:bg-white/10 rounded-lg transition-colors text-slate-400">
|
| 243 |
<X className="w-5 h-5" />
|
| 244 |
</button>
|
| 245 |
</div>
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
{/* Content */}
|
| 248 |
+
<div className="p-5 space-y-5">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
{/* Resources */}
|
| 250 |
{resources.length > 0 && (
|
| 251 |
+
<div>
|
| 252 |
+
<label className="block text-xs font-medium text-slate-500 mb-2">Мастер</label>
|
|
|
|
|
|
|
|
|
|
| 253 |
<div className="flex flex-wrap gap-2">
|
| 254 |
<button
|
| 255 |
onClick={() => setSelectedResource(null)}
|
| 256 |
className={cn(
|
| 257 |
+
"px-3 py-1.5 rounded-lg text-sm font-medium border transition-all",
|
| 258 |
!selectedResource
|
| 259 |
? "bg-indigo-500/20 border-indigo-500/50 text-white"
|
| 260 |
: "bg-white/5 border-white/10 text-slate-400 hover:bg-white/10"
|
| 261 |
)}
|
| 262 |
>
|
| 263 |
+
Без мастера
|
| 264 |
</button>
|
| 265 |
{resources.map(r => (
|
| 266 |
<button
|
| 267 |
key={r.id}
|
| 268 |
onClick={() => setSelectedResource(r.id)}
|
| 269 |
className={cn(
|
| 270 |
+
"px-3 py-1.5 rounded-lg text-sm font-medium border transition-all",
|
| 271 |
selectedResource === r.id
|
| 272 |
? "bg-indigo-500/20 border-indigo-500/50 text-white"
|
| 273 |
: "bg-white/5 border-white/10 text-slate-400 hover:bg-white/10"
|
|
|
|
| 277 |
</button>
|
| 278 |
))}
|
| 279 |
</div>
|
| 280 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
)}
|
| 282 |
|
| 283 |
+
{/* Calendar */}
|
| 284 |
+
<div>
|
| 285 |
+
<div className="flex items-center justify-between mb-3">
|
| 286 |
+
<label className="text-xs font-medium text-slate-500">Дата</label>
|
| 287 |
+
<div className="flex items-center gap-1">
|
| 288 |
+
<button
|
| 289 |
+
onClick={() => setCurrentMonth(m => addMonths(m, -1))}
|
| 290 |
+
className="p-1 hover:bg-white/10 rounded text-slate-400"
|
| 291 |
+
>
|
| 292 |
+
<ChevronLeft className="w-4 h-4" />
|
| 293 |
+
</button>
|
| 294 |
+
<span className="text-sm font-medium text-white min-w-[100px] text-center">
|
| 295 |
+
{format(currentMonth, 'LLLL yyyy', { locale: ru })}
|
| 296 |
+
</span>
|
| 297 |
+
<button
|
| 298 |
+
onClick={() => setCurrentMonth(m => addMonths(m, 1))}
|
| 299 |
+
className="p-1 hover:bg-white/10 rounded text-slate-400"
|
| 300 |
+
>
|
| 301 |
+
<ChevronRight className="w-4 h-4" />
|
| 302 |
+
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
</div>
|
| 304 |
+
</div>
|
| 305 |
+
|
| 306 |
+
{/* Weekday headers */}
|
| 307 |
+
<div className="grid grid-cols-7 gap-1 mb-1">
|
| 308 |
+
{['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'].map(d => (
|
| 309 |
+
<div key={d} className="text-center text-[10px] font-medium text-slate-600 py-1">{d}</div>
|
| 310 |
+
))}
|
| 311 |
+
</div>
|
| 312 |
+
|
| 313 |
+
{/* Calendar grid */}
|
| 314 |
+
<div className="grid grid-cols-7 gap-1">
|
| 315 |
+
{calendarDays.map(day => {
|
| 316 |
+
const isWorking = isWorkingDay(day)
|
| 317 |
+
const isSelected = selectedDate && isSameDay(day, selectedDate)
|
| 318 |
+
const isCurrentMonth = isSameMonth(day, currentMonth)
|
| 319 |
+
const isToday = isSameDay(day, new Date())
|
| 320 |
+
|
| 321 |
+
return (
|
| 322 |
+
<button
|
| 323 |
+
key={day.toISOString()}
|
| 324 |
+
onClick={() => handleDateSelect(day)}
|
| 325 |
+
disabled={!isWorking}
|
| 326 |
+
className={cn(
|
| 327 |
+
"aspect-square rounded-lg text-sm font-medium transition-all flex items-center justify-center",
|
| 328 |
+
!isCurrentMonth && "opacity-30",
|
| 329 |
+
isSelected
|
| 330 |
+
? "bg-indigo-600 text-white"
|
| 331 |
+
: isWorking
|
| 332 |
+
? "hover:bg-white/10 text-white"
|
| 333 |
+
: "text-slate-600 cursor-not-allowed",
|
| 334 |
+
isToday && !isSelected && "ring-1 ring-indigo-500/50"
|
| 335 |
+
)}
|
| 336 |
+
>
|
| 337 |
+
{format(day, 'd')}
|
| 338 |
+
</button>
|
| 339 |
+
)
|
| 340 |
+
})}
|
| 341 |
+
</div>
|
| 342 |
+
</div>
|
| 343 |
+
|
| 344 |
+
{/* Time slots */}
|
| 345 |
+
{selectedDate && (
|
| 346 |
+
<div>
|
| 347 |
+
<label className="block text-xs font-medium text-slate-500 mb-2">
|
| 348 |
+
Время • {format(selectedDate, 'd MMMM', { locale: ru })}
|
| 349 |
</label>
|
| 350 |
{timeSlots.length === 0 ? (
|
| 351 |
+
<p className="text-sm text-slate-500">Нерабочий день</p>
|
| 352 |
) : (
|
| 353 |
+
<div className="grid grid-cols-5 gap-1.5 max-h-[120px] overflow-y-auto">
|
| 354 |
{timeSlots.map(time => (
|
| 355 |
<button
|
| 356 |
key={time}
|
| 357 |
onClick={() => setSelectedTime(time)}
|
| 358 |
className={cn(
|
| 359 |
+
"py-2 rounded-lg text-sm font-medium transition-all",
|
| 360 |
selectedTime === time
|
| 361 |
+
? "bg-indigo-600 text-white"
|
| 362 |
+
: "bg-white/5 text-slate-400 hover:bg-white/10"
|
| 363 |
)}
|
| 364 |
>
|
| 365 |
{time}
|
|
|
|
| 367 |
))}
|
| 368 |
</div>
|
| 369 |
)}
|
| 370 |
+
</div>
|
| 371 |
+
)}
|
| 372 |
|
| 373 |
{/* Notes */}
|
| 374 |
+
<div>
|
| 375 |
+
<label className="block text-xs font-medium text-slate-500 mb-2">Комментарий</label>
|
| 376 |
+
<input
|
| 377 |
+
type="text"
|
| 378 |
value={bookingNote}
|
| 379 |
onChange={(e) => setBookingNote(e.target.value)}
|
| 380 |
placeholder="Детали встречи..."
|
| 381 |
+
className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-sm text-white placeholder:text-slate-600 focus:outline-none focus:border-indigo-500/50"
|
| 382 |
/>
|
| 383 |
+
</div>
|
| 384 |
</div>
|
| 385 |
|
| 386 |
{/* Footer */}
|
| 387 |
+
<div className="px-5 py-4 border-t border-white/5">
|
| 388 |
<button
|
| 389 |
onClick={handleBook}
|
| 390 |
+
disabled={!selectedDate || !selectedTime || isSubmitting}
|
| 391 |
+
className="w-full py-3 bg-indigo-600 hover:bg-indigo-500 text-white font-bold rounded-xl transition-all flex items-center justify-center gap-2 disabled:opacity-30 disabled:cursor-not-allowed"
|
| 392 |
>
|
| 393 |
{isSubmitting ? (
|
| 394 |
<Loader2 className="w-5 h-5 animate-spin" />
|
| 395 |
) : (
|
| 396 |
<Check className="w-5 h-5" />
|
| 397 |
)}
|
| 398 |
+
{selectedTime ? `Записать на ${selectedTime}` : 'Выберите время'}
|
| 399 |
</button>
|
| 400 |
</div>
|
| 401 |
</motion.div>
|