Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
f345217
1
Parent(s): 173cfbc
fix: BookingModal - instant open, resource-less booking, better UI
Browse files- backend/app/api/bookings.py +27 -4
- frontend/components/bookings/BookingModal.tsx +171 -144
backend/app/api/bookings.py
CHANGED
|
@@ -115,10 +115,33 @@ async def create_appointment(
|
|
| 115 |
# Ensure tenant_id is set
|
| 116 |
appointment.tenant_id = current_user.tenant_id
|
| 117 |
|
| 118 |
-
# Check if service exists
|
| 119 |
-
service =
|
| 120 |
-
if
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
session.add(appointment)
|
| 124 |
await session.commit()
|
|
|
|
| 115 |
# Ensure tenant_id is set
|
| 116 |
appointment.tenant_id = current_user.tenant_id
|
| 117 |
|
| 118 |
+
# Check if service exists, or create default
|
| 119 |
+
service = None
|
| 120 |
+
if appointment.service_id:
|
| 121 |
+
service = await session.get(Service, appointment.service_id)
|
| 122 |
+
if not service or service.tenant_id != current_user.tenant_id:
|
| 123 |
+
raise HTTPException(status_code=404, detail="Service not found")
|
| 124 |
+
else:
|
| 125 |
+
# Find or create default service
|
| 126 |
+
stmt = select(Service).where(
|
| 127 |
+
Service.tenant_id == current_user.tenant_id,
|
| 128 |
+
Service.name == "Консультация"
|
| 129 |
+
).limit(1)
|
| 130 |
+
result = await session.execute(stmt)
|
| 131 |
+
service = result.scalar_one_or_none()
|
| 132 |
+
|
| 133 |
+
if not service:
|
| 134 |
+
service = Service(
|
| 135 |
+
tenant_id=current_user.tenant_id,
|
| 136 |
+
name="Консультация",
|
| 137 |
+
duration_minutes=30,
|
| 138 |
+
price=0.0,
|
| 139 |
+
is_active=True
|
| 140 |
+
)
|
| 141 |
+
session.add(service)
|
| 142 |
+
await session.flush()
|
| 143 |
+
|
| 144 |
+
appointment.service_id = service.id
|
| 145 |
|
| 146 |
session.add(appointment)
|
| 147 |
await session.commit()
|
frontend/components/bookings/BookingModal.tsx
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
-
import { useState, useEffect, useMemo, useCallback } from 'react'
|
| 4 |
-
import { X, Calendar as CalendarIcon,
|
| 5 |
-
import { format,
|
| 6 |
import { ru } from 'date-fns/locale'
|
| 7 |
import { motion, AnimatePresence } from 'framer-motion'
|
| 8 |
import { bookingsApi } from '@/lib/api'
|
| 9 |
import { cn } from '@/lib/utils'
|
| 10 |
-
import {
|
| 11 |
|
| 12 |
interface BookingModalProps {
|
| 13 |
isOpen: boolean
|
|
@@ -26,7 +26,6 @@ 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
|
|
@@ -47,8 +46,13 @@ function generateTimeSlots(startTime: string, endTime: string, interval: number
|
|
| 47 |
return slots
|
| 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)
|
|
@@ -56,35 +60,47 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 56 |
const [bookingNote, setBookingNote] = useState('')
|
| 57 |
const [isSubmitting, setIsSubmitting] = useState(false)
|
| 58 |
const [isSuccess, setIsSuccess] = useState(false)
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
const [
|
| 62 |
-
const [
|
| 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(() =>
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
}, [isOpen])
|
| 89 |
|
| 90 |
// Selected resource data
|
|
@@ -93,7 +109,7 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 93 |
return resources.find(r => r.id === selectedResource)
|
| 94 |
}, [selectedResource, resources])
|
| 95 |
|
| 96 |
-
//
|
| 97 |
const isWorkingDay = useCallback((date: Date): boolean => {
|
| 98 |
if (isBefore(date, startOfToday())) return false
|
| 99 |
|
|
@@ -112,42 +128,37 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 112 |
return jsDay !== 0 && jsDay !== 6
|
| 113 |
}, [selectedResourceData, bookingSettings])
|
| 114 |
|
| 115 |
-
//
|
| 116 |
-
const getWorkingHours = useCallback((date: Date)
|
| 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]
|
| 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
|
| 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 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
}, [currentMonth])
|
| 149 |
|
| 150 |
-
//
|
| 151 |
const handleDateSelect = useCallback((date: Date) => {
|
| 152 |
if (isWorkingDay(date)) {
|
| 153 |
setSelectedDate(date)
|
|
@@ -155,10 +166,13 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 155 |
}
|
| 156 |
}, [isWorkingDay])
|
| 157 |
|
|
|
|
| 158 |
const handleBook = useCallback(async () => {
|
| 159 |
if (!selectedDate || !selectedTime) return
|
| 160 |
|
| 161 |
setIsSubmitting(true)
|
|
|
|
|
|
|
| 162 |
try {
|
| 163 |
const [hours, minutes] = selectedTime.split(':').map(Number)
|
| 164 |
const startDateTime = new Date(selectedDate)
|
|
@@ -169,17 +183,17 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 169 |
|
| 170 |
await bookingsApi.createAppointment({
|
| 171 |
lead_id: leadId,
|
| 172 |
-
|
| 173 |
-
resource_id: selectedResource || null,
|
| 174 |
start_time: startDateTime.toISOString(),
|
| 175 |
end_time: endDateTime.toISOString(),
|
| 176 |
-
notes: bookingNote,
|
| 177 |
status: 'scheduled'
|
| 178 |
})
|
| 179 |
|
| 180 |
setIsSuccess(true)
|
| 181 |
-
} catch (
|
| 182 |
-
console.error('Booking error:',
|
|
|
|
| 183 |
} finally {
|
| 184 |
setIsSubmitting(false)
|
| 185 |
}
|
|
@@ -194,83 +208,78 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 194 |
animate={{ opacity: 1 }}
|
| 195 |
exit={{ opacity: 0 }}
|
| 196 |
onClick={!isSubmitting ? onClose : undefined}
|
| 197 |
-
className="absolute inset-0 bg-black/
|
| 198 |
/>
|
| 199 |
|
| 200 |
<motion.div
|
| 201 |
-
initial={{ opacity: 0, scale: 0.
|
| 202 |
-
animate={{ opacity: 1, scale: 1 }}
|
| 203 |
-
|
|
|
|
| 204 |
>
|
| 205 |
<AnimatePresence mode="wait">
|
| 206 |
{isSuccess ? (
|
| 207 |
<motion.div
|
| 208 |
key="success"
|
| 209 |
-
initial={{ opacity: 0,
|
| 210 |
-
animate={{ opacity: 1,
|
| 211 |
-
className="p-
|
| 212 |
>
|
| 213 |
-
<div className="w-
|
| 214 |
-
<Check className="w-
|
| 215 |
</div>
|
| 216 |
-
<h3 className="text-
|
| 217 |
-
<p className="text-slate-400 text-sm mb-
|
| 218 |
{selectedDate && format(selectedDate, 'd MMMM', { locale: ru })} в {selectedTime}
|
|
|
|
| 219 |
</p>
|
| 220 |
<button
|
| 221 |
onClick={onClose}
|
| 222 |
-
className="px-
|
| 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
|
| 237 |
-
|
| 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-
|
| 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-
|
| 253 |
<div className="flex flex-wrap gap-2">
|
| 254 |
<button
|
| 255 |
onClick={() => setSelectedResource(null)}
|
| 256 |
className={cn(
|
| 257 |
-
"px-
|
| 258 |
!selectedResource
|
| 259 |
-
? "bg-indigo-500/
|
| 260 |
-
: "bg-white/5 border-
|
| 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-
|
| 271 |
selectedResource === r.id
|
| 272 |
-
? "bg-indigo-500/
|
| 273 |
-
: "bg-white/5 border-
|
| 274 |
)}
|
| 275 |
>
|
| 276 |
{r.name}
|
|
@@ -281,85 +290,88 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 281 |
)}
|
| 282 |
|
| 283 |
{/* Calendar */}
|
| 284 |
-
<div>
|
| 285 |
-
<div className="flex items-center justify-between mb-
|
| 286 |
-
<
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
<ChevronRight className="w-4 h-4" />
|
| 302 |
-
</button>
|
| 303 |
-
</div>
|
| 304 |
</div>
|
| 305 |
|
| 306 |
-
{/*
|
| 307 |
-
<div className="grid grid-cols-7 gap-1 mb-
|
| 308 |
{['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'].map(d => (
|
| 309 |
-
<div key={d} className="text-center text-[10px] font-
|
| 310 |
))}
|
| 311 |
</div>
|
| 312 |
|
| 313 |
-
{/*
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
</div>
|
| 343 |
|
| 344 |
{/* Time slots */}
|
| 345 |
{selectedDate && (
|
| 346 |
<div>
|
| 347 |
-
<label className="block text-xs font-
|
| 348 |
-
|
| 349 |
</label>
|
| 350 |
{timeSlots.length === 0 ? (
|
| 351 |
-
<p className="text-sm text-slate-500">Не
|
| 352 |
) : (
|
| 353 |
-
<div className="grid grid-cols-
|
| 354 |
{timeSlots.map(time => (
|
| 355 |
<button
|
| 356 |
key={time}
|
| 357 |
onClick={() => setSelectedTime(time)}
|
| 358 |
className={cn(
|
| 359 |
-
"py-2 rounded-
|
| 360 |
selectedTime === time
|
| 361 |
-
? "bg-indigo-600 text-white"
|
| 362 |
-
: "bg-white/5 text-slate-
|
| 363 |
)}
|
| 364 |
>
|
| 365 |
{time}
|
|
@@ -372,30 +384,45 @@ export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModal
|
|
| 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-
|
| 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=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
>
|
| 393 |
{isSubmitting ? (
|
| 394 |
<Loader2 className="w-5 h-5 animate-spin" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
) : (
|
| 396 |
-
|
| 397 |
)}
|
| 398 |
-
{selectedTime ? `Записать на ${selectedTime}` : 'Выберите время'}
|
| 399 |
</button>
|
| 400 |
</div>
|
| 401 |
</motion.div>
|
|
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
+
import { useState, useEffect, useMemo, useCallback, useRef } from 'react'
|
| 4 |
+
import { X, Calendar as CalendarIcon, Loader2, Check, ChevronLeft, ChevronRight } from 'lucide-react'
|
| 5 |
+
import { format, 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'
|
| 9 |
import { cn } from '@/lib/utils'
|
| 10 |
+
import { getTimezoneDisplayName } from '@/lib/timezone'
|
| 11 |
|
| 12 |
interface BookingModalProps {
|
| 13 |
isOpen: boolean
|
|
|
|
| 26 |
working_hours: { start: string; end: string }
|
| 27 |
working_days: number[]
|
| 28 |
slot_duration: number
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
// Generate time slots
|
|
|
|
| 46 |
return slots
|
| 47 |
}
|
| 48 |
|
| 49 |
+
// Pre-cache data globally to avoid refetch on every modal open
|
| 50 |
+
let cachedResources: Resource[] | null = null
|
| 51 |
+
let cachedSettings: BookingSettings | null = null
|
| 52 |
+
let cacheTime = 0
|
| 53 |
+
const CACHE_TTL = 60000 // 1 minute
|
| 54 |
+
|
| 55 |
export function BookingModal({ isOpen, onClose, leadId, leadName }: BookingModalProps) {
|
|
|
|
| 56 |
const [currentMonth, setCurrentMonth] = useState(new Date())
|
| 57 |
const [selectedDate, setSelectedDate] = useState<Date | null>(null)
|
| 58 |
const [selectedTime, setSelectedTime] = useState<string | null>(null)
|
|
|
|
| 60 |
const [bookingNote, setBookingNote] = useState('')
|
| 61 |
const [isSubmitting, setIsSubmitting] = useState(false)
|
| 62 |
const [isSuccess, setIsSuccess] = useState(false)
|
| 63 |
+
const [error, setError] = useState<string | null>(null)
|
| 64 |
|
| 65 |
+
const [resources, setResources] = useState<Resource[]>(cachedResources || [])
|
| 66 |
+
const [bookingSettings, setBookingSettings] = useState<BookingSettings | null>(cachedSettings)
|
| 67 |
+
const [isLoading, setIsLoading] = useState(!cachedSettings)
|
|
|
|
| 68 |
|
| 69 |
+
// Load data in background - don't block modal opening
|
| 70 |
useEffect(() => {
|
| 71 |
if (!isOpen) return
|
| 72 |
|
| 73 |
+
// Reset state on open
|
| 74 |
setIsSuccess(false)
|
| 75 |
+
setError(null)
|
| 76 |
setSelectedDate(null)
|
| 77 |
setSelectedTime(null)
|
|
|
|
| 78 |
|
| 79 |
+
// Use cache if fresh
|
| 80 |
+
const now = Date.now()
|
| 81 |
+
if (cachedSettings && cachedResources && (now - cacheTime) < CACHE_TTL) {
|
| 82 |
+
setResources(cachedResources)
|
| 83 |
+
setBookingSettings(cachedSettings)
|
| 84 |
+
setIsLoading(false)
|
| 85 |
+
return
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Fetch in background
|
| 89 |
+
setIsLoading(true)
|
| 90 |
Promise.all([
|
| 91 |
bookingsApi.getResources().catch(() => []),
|
| 92 |
+
bookingsApi.getBookingSettings().catch(() => ({
|
|
|
|
|
|
|
|
|
|
| 93 |
working_hours: { start: '09:00', end: '18:00' },
|
| 94 |
working_days: [0, 1, 2, 3, 4],
|
| 95 |
+
slot_duration: 30
|
| 96 |
+
}))
|
| 97 |
+
]).then(([res, settings]) => {
|
| 98 |
+
cachedResources = res || []
|
| 99 |
+
cachedSettings = settings
|
| 100 |
+
cacheTime = Date.now()
|
| 101 |
+
setResources(cachedResources)
|
| 102 |
+
setBookingSettings(cachedSettings)
|
| 103 |
+
}).finally(() => setIsLoading(false))
|
| 104 |
}, [isOpen])
|
| 105 |
|
| 106 |
// Selected resource data
|
|
|
|
| 109 |
return resources.find(r => r.id === selectedResource)
|
| 110 |
}, [selectedResource, resources])
|
| 111 |
|
| 112 |
+
// Working day check
|
| 113 |
const isWorkingDay = useCallback((date: Date): boolean => {
|
| 114 |
if (isBefore(date, startOfToday())) return false
|
| 115 |
|
|
|
|
| 128 |
return jsDay !== 0 && jsDay !== 6
|
| 129 |
}, [selectedResourceData, bookingSettings])
|
| 130 |
|
| 131 |
+
// Working hours for date
|
| 132 |
+
const getWorkingHours = useCallback((date: Date) => {
|
| 133 |
const jsDay = getDay(date)
|
| 134 |
const dayKey = String(jsDay === 0 ? 6 : jsDay - 1)
|
| 135 |
|
| 136 |
+
if (selectedResourceData?.working_hours?.[dayKey]) {
|
| 137 |
+
return selectedResourceData.working_hours[dayKey]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|
| 139 |
|
| 140 |
+
return bookingSettings?.working_hours || { start: '09:00', end: '18:00' }
|
| 141 |
}, [selectedResourceData, bookingSettings])
|
| 142 |
|
| 143 |
+
// Time slots
|
| 144 |
const timeSlots = useMemo(() => {
|
| 145 |
if (!selectedDate) return []
|
| 146 |
+
if (!isWorkingDay(selectedDate)) return []
|
| 147 |
const hours = getWorkingHours(selectedDate)
|
|
|
|
| 148 |
return generateTimeSlots(hours.start, hours.end, bookingSettings?.slot_duration || 30)
|
| 149 |
+
}, [selectedDate, getWorkingHours, isWorkingDay, bookingSettings?.slot_duration])
|
| 150 |
|
| 151 |
// Calendar days
|
| 152 |
const calendarDays = useMemo(() => {
|
| 153 |
const monthStart = startOfMonth(currentMonth)
|
| 154 |
const monthEnd = endOfMonth(currentMonth)
|
| 155 |
+
return eachDayOfInterval({
|
| 156 |
+
start: startOfWeek(monthStart, { weekStartsOn: 1 }),
|
| 157 |
+
end: endOfWeek(monthEnd, { weekStartsOn: 1 })
|
| 158 |
+
})
|
| 159 |
}, [currentMonth])
|
| 160 |
|
| 161 |
+
// Date selection
|
| 162 |
const handleDateSelect = useCallback((date: Date) => {
|
| 163 |
if (isWorkingDay(date)) {
|
| 164 |
setSelectedDate(date)
|
|
|
|
| 166 |
}
|
| 167 |
}, [isWorkingDay])
|
| 168 |
|
| 169 |
+
// Book appointment
|
| 170 |
const handleBook = useCallback(async () => {
|
| 171 |
if (!selectedDate || !selectedTime) return
|
| 172 |
|
| 173 |
setIsSubmitting(true)
|
| 174 |
+
setError(null)
|
| 175 |
+
|
| 176 |
try {
|
| 177 |
const [hours, minutes] = selectedTime.split(':').map(Number)
|
| 178 |
const startDateTime = new Date(selectedDate)
|
|
|
|
| 183 |
|
| 184 |
await bookingsApi.createAppointment({
|
| 185 |
lead_id: leadId,
|
| 186 |
+
resource_id: selectedResource || undefined,
|
|
|
|
| 187 |
start_time: startDateTime.toISOString(),
|
| 188 |
end_time: endDateTime.toISOString(),
|
| 189 |
+
notes: bookingNote || undefined,
|
| 190 |
status: 'scheduled'
|
| 191 |
})
|
| 192 |
|
| 193 |
setIsSuccess(true)
|
| 194 |
+
} catch (err: any) {
|
| 195 |
+
console.error('Booking error:', err)
|
| 196 |
+
setError(err?.response?.data?.detail || 'Ошибка создания записи')
|
| 197 |
} finally {
|
| 198 |
setIsSubmitting(false)
|
| 199 |
}
|
|
|
|
| 208 |
animate={{ opacity: 1 }}
|
| 209 |
exit={{ opacity: 0 }}
|
| 210 |
onClick={!isSubmitting ? onClose : undefined}
|
| 211 |
+
className="absolute inset-0 bg-black/70 backdrop-blur-sm"
|
| 212 |
/>
|
| 213 |
|
| 214 |
<motion.div
|
| 215 |
+
initial={{ opacity: 0, y: 20, scale: 0.98 }}
|
| 216 |
+
animate={{ opacity: 1, y: 0, scale: 1 }}
|
| 217 |
+
transition={{ duration: 0.2, ease: 'easeOut' }}
|
| 218 |
+
className="relative bg-gradient-to-b from-[#16161f] to-[#0f0f15] border border-white/10 rounded-2xl w-full max-w-[420px] overflow-hidden shadow-2xl shadow-black/50"
|
| 219 |
>
|
| 220 |
<AnimatePresence mode="wait">
|
| 221 |
{isSuccess ? (
|
| 222 |
<motion.div
|
| 223 |
key="success"
|
| 224 |
+
initial={{ opacity: 0, scale: 0.95 }}
|
| 225 |
+
animate={{ opacity: 1, scale: 1 }}
|
| 226 |
+
className="p-10 flex flex-col items-center text-center"
|
| 227 |
>
|
| 228 |
+
<div className="w-16 h-16 rounded-full bg-gradient-to-br from-emerald-500/20 to-emerald-600/10 border border-emerald-500/30 flex items-center justify-center mb-5">
|
| 229 |
+
<Check className="w-8 h-8 text-emerald-400" />
|
| 230 |
</div>
|
| 231 |
+
<h3 className="text-xl font-bold text-white mb-2">Запись создана!</h3>
|
| 232 |
+
<p className="text-slate-400 text-sm mb-6">
|
| 233 |
{selectedDate && format(selectedDate, 'd MMMM', { locale: ru })} в {selectedTime}
|
| 234 |
+
{selectedResourceData && <span className="text-slate-500"> • {selectedResourceData.name}</span>}
|
| 235 |
</p>
|
| 236 |
<button
|
| 237 |
onClick={onClose}
|
| 238 |
+
className="px-8 py-2.5 bg-white/10 hover:bg-white/15 text-white font-medium rounded-xl transition-all"
|
| 239 |
>
|
| 240 |
Закрыть
|
| 241 |
</button>
|
| 242 |
</motion.div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
) : (
|
| 244 |
<motion.div key="form">
|
| 245 |
{/* Header */}
|
| 246 |
+
<div className="px-5 py-4 border-b border-white/5 flex items-center justify-between bg-white/[0.02]">
|
| 247 |
<div>
|
| 248 |
+
<h3 className="font-bold text-white text-lg">{leadName || 'Клиент'}</h3>
|
| 249 |
+
<p className="text-xs text-slate-500">{getTimezoneDisplayName()}</p>
|
|
|
|
|
|
|
|
|
|
| 250 |
</div>
|
| 251 |
+
<button onClick={onClose} className="p-2 hover:bg-white/10 rounded-lg transition-colors text-slate-400 hover:text-white">
|
| 252 |
<X className="w-5 h-5" />
|
| 253 |
</button>
|
| 254 |
</div>
|
| 255 |
|
| 256 |
{/* Content */}
|
| 257 |
+
<div className="p-5 space-y-5 max-h-[70vh] overflow-y-auto">
|
| 258 |
{/* Resources */}
|
| 259 |
{resources.length > 0 && (
|
| 260 |
<div>
|
| 261 |
+
<label className="block text-xs font-semibold text-slate-400 uppercase tracking-wider mb-2">Ресурс</label>
|
| 262 |
<div className="flex flex-wrap gap-2">
|
| 263 |
<button
|
| 264 |
onClick={() => setSelectedResource(null)}
|
| 265 |
className={cn(
|
| 266 |
+
"px-4 py-2 rounded-xl text-sm font-medium border-2 transition-all",
|
| 267 |
!selectedResource
|
| 268 |
+
? "bg-indigo-500/15 border-indigo-500/50 text-white shadow-lg shadow-indigo-500/10"
|
| 269 |
+
: "bg-white/5 border-transparent text-slate-400 hover:bg-white/10 hover:text-white"
|
| 270 |
)}
|
| 271 |
>
|
| 272 |
+
Не выбран
|
| 273 |
</button>
|
| 274 |
{resources.map(r => (
|
| 275 |
<button
|
| 276 |
key={r.id}
|
| 277 |
onClick={() => setSelectedResource(r.id)}
|
| 278 |
className={cn(
|
| 279 |
+
"px-4 py-2 rounded-xl text-sm font-medium border-2 transition-all",
|
| 280 |
selectedResource === r.id
|
| 281 |
+
? "bg-indigo-500/15 border-indigo-500/50 text-white shadow-lg shadow-indigo-500/10"
|
| 282 |
+
: "bg-white/5 border-transparent text-slate-400 hover:bg-white/10 hover:text-white"
|
| 283 |
)}
|
| 284 |
>
|
| 285 |
{r.name}
|
|
|
|
| 290 |
)}
|
| 291 |
|
| 292 |
{/* Calendar */}
|
| 293 |
+
<div className="bg-white/[0.03] rounded-xl p-4 border border-white/5">
|
| 294 |
+
<div className="flex items-center justify-between mb-4">
|
| 295 |
+
<button
|
| 296 |
+
onClick={() => setCurrentMonth(m => addMonths(m, -1))}
|
| 297 |
+
className="p-1.5 hover:bg-white/10 rounded-lg text-slate-400 hover:text-white transition-colors"
|
| 298 |
+
>
|
| 299 |
+
<ChevronLeft className="w-5 h-5" />
|
| 300 |
+
</button>
|
| 301 |
+
<span className="text-sm font-semibold text-white capitalize">
|
| 302 |
+
{format(currentMonth, 'LLLL yyyy', { locale: ru })}
|
| 303 |
+
</span>
|
| 304 |
+
<button
|
| 305 |
+
onClick={() => setCurrentMonth(m => addMonths(m, 1))}
|
| 306 |
+
className="p-1.5 hover:bg-white/10 rounded-lg text-slate-400 hover:text-white transition-colors"
|
| 307 |
+
>
|
| 308 |
+
<ChevronRight className="w-5 h-5" />
|
| 309 |
+
</button>
|
|
|
|
|
|
|
|
|
|
| 310 |
</div>
|
| 311 |
|
| 312 |
+
{/* Week headers */}
|
| 313 |
+
<div className="grid grid-cols-7 gap-1 mb-2">
|
| 314 |
{['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'].map(d => (
|
| 315 |
+
<div key={d} className="text-center text-[10px] font-semibold text-slate-500 py-1">{d}</div>
|
| 316 |
))}
|
| 317 |
</div>
|
| 318 |
|
| 319 |
+
{/* Days */}
|
| 320 |
+
{isLoading ? (
|
| 321 |
+
<div className="flex items-center justify-center py-8">
|
| 322 |
+
<Loader2 className="w-5 h-5 animate-spin text-indigo-400" />
|
| 323 |
+
</div>
|
| 324 |
+
) : (
|
| 325 |
+
<div className="grid grid-cols-7 gap-1">
|
| 326 |
+
{calendarDays.map(day => {
|
| 327 |
+
const isWorking = isWorkingDay(day)
|
| 328 |
+
const isSelected = selectedDate && isSameDay(day, selectedDate)
|
| 329 |
+
const isCurrentMonth = isSameMonth(day, currentMonth)
|
| 330 |
+
const isToday = isSameDay(day, new Date())
|
| 331 |
+
|
| 332 |
+
return (
|
| 333 |
+
<button
|
| 334 |
+
key={day.toISOString()}
|
| 335 |
+
onClick={() => handleDateSelect(day)}
|
| 336 |
+
disabled={!isWorking}
|
| 337 |
+
className={cn(
|
| 338 |
+
"aspect-square rounded-lg text-sm font-medium transition-all flex items-center justify-center relative",
|
| 339 |
+
!isCurrentMonth && "opacity-25",
|
| 340 |
+
isSelected
|
| 341 |
+
? "bg-indigo-600 text-white shadow-lg shadow-indigo-500/30"
|
| 342 |
+
: isWorking
|
| 343 |
+
? "hover:bg-white/10 text-white cursor-pointer"
|
| 344 |
+
: "text-slate-600 cursor-not-allowed",
|
| 345 |
+
isToday && !isSelected && "ring-1 ring-indigo-400/50"
|
| 346 |
+
)}
|
| 347 |
+
>
|
| 348 |
+
{format(day, 'd')}
|
| 349 |
+
</button>
|
| 350 |
+
)
|
| 351 |
+
})}
|
| 352 |
+
</div>
|
| 353 |
+
)}
|
| 354 |
</div>
|
| 355 |
|
| 356 |
{/* Time slots */}
|
| 357 |
{selectedDate && (
|
| 358 |
<div>
|
| 359 |
+
<label className="block text-xs font-semibold text-slate-400 uppercase tracking-wider mb-3">
|
| 360 |
+
{format(selectedDate, 'd MMMM, EEEE', { locale: ru })}
|
| 361 |
</label>
|
| 362 |
{timeSlots.length === 0 ? (
|
| 363 |
+
<p className="text-sm text-slate-500 text-center py-4">Нет доступных слотов</p>
|
| 364 |
) : (
|
| 365 |
+
<div className="grid grid-cols-4 gap-2">
|
| 366 |
{timeSlots.map(time => (
|
| 367 |
<button
|
| 368 |
key={time}
|
| 369 |
onClick={() => setSelectedTime(time)}
|
| 370 |
className={cn(
|
| 371 |
+
"py-2.5 rounded-xl text-sm font-semibold transition-all",
|
| 372 |
selectedTime === time
|
| 373 |
+
? "bg-indigo-600 text-white shadow-lg shadow-indigo-500/30"
|
| 374 |
+
: "bg-white/5 text-slate-300 hover:bg-white/10 hover:text-white"
|
| 375 |
)}
|
| 376 |
>
|
| 377 |
{time}
|
|
|
|
| 384 |
|
| 385 |
{/* Notes */}
|
| 386 |
<div>
|
|
|
|
| 387 |
<input
|
| 388 |
type="text"
|
| 389 |
value={bookingNote}
|
| 390 |
onChange={(e) => setBookingNote(e.target.value)}
|
| 391 |
+
placeholder="Комментарий к записи..."
|
| 392 |
+
className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-xl text-sm text-white placeholder:text-slate-500 focus:outline-none focus:border-indigo-500/50 focus:bg-white/[0.07] transition-all"
|
| 393 |
/>
|
| 394 |
</div>
|
| 395 |
+
|
| 396 |
+
{/* Error */}
|
| 397 |
+
{error && (
|
| 398 |
+
<div className="p-3 bg-red-500/10 border border-red-500/20 rounded-xl text-red-400 text-sm text-center">
|
| 399 |
+
{error}
|
| 400 |
+
</div>
|
| 401 |
+
)}
|
| 402 |
</div>
|
| 403 |
|
| 404 |
{/* Footer */}
|
| 405 |
+
<div className="px-5 py-4 border-t border-white/5 bg-white/[0.02]">
|
| 406 |
<button
|
| 407 |
onClick={handleBook}
|
| 408 |
disabled={!selectedDate || !selectedTime || isSubmitting}
|
| 409 |
+
className={cn(
|
| 410 |
+
"w-full py-3.5 font-bold rounded-xl transition-all flex items-center justify-center gap-2",
|
| 411 |
+
selectedDate && selectedTime
|
| 412 |
+
? "bg-gradient-to-r from-indigo-600 to-indigo-500 hover:from-indigo-500 hover:to-indigo-400 text-white shadow-lg shadow-indigo-500/25"
|
| 413 |
+
: "bg-white/5 text-slate-500 cursor-not-allowed"
|
| 414 |
+
)}
|
| 415 |
>
|
| 416 |
{isSubmitting ? (
|
| 417 |
<Loader2 className="w-5 h-5 animate-spin" />
|
| 418 |
+
) : selectedTime ? (
|
| 419 |
+
<>
|
| 420 |
+
<CalendarIcon className="w-4 h-4" />
|
| 421 |
+
Записать на {selectedTime}
|
| 422 |
+
</>
|
| 423 |
) : (
|
| 424 |
+
'Выберите дату и время'
|
| 425 |
)}
|
|
|
|
| 426 |
</button>
|
| 427 |
</div>
|
| 428 |
</motion.div>
|