Initial commit: HotelSync PMS v0.1.0
- React 18 + TypeScript + Vite + Tailwind CSS - Шахматка бронирований (drag-to-book) - Страницы: Calendar, Bookings, Rooms, Housekeeping, Channels, API Docs, Settings - Роли: super_admin, hotel_manager, housekeeper - Светлая/тёмная тема - Docker + Nginx конфигурация - Лендинг hotelsync.ru
This commit is contained in:
47
src/contexts/AuthContext.tsx
Normal file
47
src/contexts/AuthContext.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { createContext, useContext, useState } from 'react'
|
||||
import type { User, AuthSession } from '../types'
|
||||
import { MOCK_USERS } from '../data/mockData'
|
||||
|
||||
interface AuthContextValue {
|
||||
session: AuthSession | null
|
||||
user: User | null
|
||||
login: (email: string, password: string) => Promise<boolean>
|
||||
logout: () => void
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null)
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [session, setSession] = useState<AuthSession | null>(() => {
|
||||
const stored = sessionStorage.getItem('hotelsync-session')
|
||||
return stored ? JSON.parse(stored) : null
|
||||
})
|
||||
|
||||
const login = async (email: string, _password: string): Promise<boolean> => {
|
||||
// Mock authentication — in production, call POST /auth/login
|
||||
await new Promise(r => setTimeout(r, 800))
|
||||
const user = MOCK_USERS.find(u => u.email.toLowerCase() === email.toLowerCase())
|
||||
if (!user) return false
|
||||
const s: AuthSession = { user, token: 'mock-jwt-token-' + user.id }
|
||||
setSession(s)
|
||||
sessionStorage.setItem('hotelsync-session', JSON.stringify(s))
|
||||
return true
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
setSession(null)
|
||||
sessionStorage.removeItem('hotelsync-session')
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ session, user: session?.user ?? null, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const ctx = useContext(AuthContext)
|
||||
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
|
||||
return ctx
|
||||
}
|
||||
42
src/contexts/ThemeContext.tsx
Normal file
42
src/contexts/ThemeContext.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
|
||||
type Theme = 'light' | 'dark'
|
||||
|
||||
interface ThemeContextValue {
|
||||
theme: Theme
|
||||
toggle: () => void
|
||||
}
|
||||
|
||||
const ThemeContext = createContext<ThemeContextValue | null>(null)
|
||||
|
||||
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
||||
const [theme, setTheme] = useState<Theme>(() => {
|
||||
const stored = localStorage.getItem('hotelsync-theme') as Theme | null
|
||||
if (stored) return stored
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.documentElement
|
||||
if (theme === 'dark') {
|
||||
root.classList.add('dark')
|
||||
} else {
|
||||
root.classList.remove('dark')
|
||||
}
|
||||
localStorage.setItem('hotelsync-theme', theme)
|
||||
}, [theme])
|
||||
|
||||
const toggle = () => setTheme(t => t === 'light' ? 'dark' : 'light')
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ theme, toggle }}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
const ctx = useContext(ThemeContext)
|
||||
if (!ctx) throw new Error('useTheme must be used within ThemeProvider')
|
||||
return ctx
|
||||
}
|
||||
Reference in New Issue
Block a user