feat: configurable bed types via BedTypesContext
- Add BedTypesContext with default types + add/remove/update operations - Replace hardcoded BED_ITEM_TYPES array in RoomModal with useBedTypes() - Add "Справочник спальных мест" section to RoomCategoriesPage (expandable, same UX as amenities: add icon+label, rename, delete) - BedItem.type changed from union to string for dynamic support Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
53
src/contexts/BedTypesContext.tsx
Normal file
53
src/contexts/BedTypesContext.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createContext, useContext, useState } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
export interface BedTypeItem {
|
||||
value: string // unique key stored in DB (e.g. 'double', 'sofa')
|
||||
label: string // display name
|
||||
icon: string // emoji
|
||||
}
|
||||
|
||||
const DEFAULT_BED_TYPES: BedTypeItem[] = [
|
||||
{ value: 'single', label: 'Кровать 1-сп.', icon: '🛏' },
|
||||
{ value: 'double', label: 'Кровать 2-сп.', icon: '🛏' },
|
||||
{ value: 'queen', label: 'Queen-кровать', icon: '🛏' },
|
||||
{ value: 'king', label: 'King-кровать', icon: '🛏' },
|
||||
{ value: 'twin', label: 'Две кровати', icon: '🛏' },
|
||||
{ value: 'sofa', label: 'Диван', icon: '🛋' },
|
||||
{ value: 'bunk', label: 'Двухъярусная', icon: '🛏' },
|
||||
{ value: 'cot', label: 'Раскладушка', icon: '🪑' },
|
||||
]
|
||||
|
||||
interface BedTypesContextValue {
|
||||
bedTypes: BedTypeItem[]
|
||||
addBedType: (item: BedTypeItem) => void
|
||||
removeBedType: (value: string) => void
|
||||
updateBedType: (value: string, patch: Partial<Omit<BedTypeItem, 'value'>>) => void
|
||||
}
|
||||
|
||||
const BedTypesContext = createContext<BedTypesContextValue | null>(null)
|
||||
|
||||
export function BedTypesProvider({ children }: { children: ReactNode }) {
|
||||
const [bedTypes, setBedTypes] = useState<BedTypeItem[]>(DEFAULT_BED_TYPES)
|
||||
|
||||
const addBedType = (item: BedTypeItem) =>
|
||||
setBedTypes(prev => prev.find(b => b.value === item.value) ? prev : [...prev, item])
|
||||
|
||||
const removeBedType = (value: string) =>
|
||||
setBedTypes(prev => prev.filter(b => b.value !== value))
|
||||
|
||||
const updateBedType = (value: string, patch: Partial<Omit<BedTypeItem, 'value'>>) =>
|
||||
setBedTypes(prev => prev.map(b => b.value === value ? { ...b, ...patch } : b))
|
||||
|
||||
return (
|
||||
<BedTypesContext.Provider value={{ bedTypes, addBedType, removeBedType, updateBedType }}>
|
||||
{children}
|
||||
</BedTypesContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useBedTypes() {
|
||||
const ctx = useContext(BedTypesContext)
|
||||
if (!ctx) throw new Error('useBedTypes must be used within BedTypesProvider')
|
||||
return ctx
|
||||
}
|
||||
Reference in New Issue
Block a user