This commit is contained in:
241
src/app/page.tsx
241
src/app/page.tsx
@@ -1,90 +1,175 @@
|
||||
import { Home as HomeIcon, Settings, Calendar, BarChart3, Leaf } from 'lucide-react'
|
||||
import Link from 'next/link'
|
||||
"use client"
|
||||
import { useEffect, useState, useRef } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { api } from '@/lib/api'
|
||||
import { Smartphone, ArrowLeft } from 'lucide-react'
|
||||
import Loading from '@/components/Loading'
|
||||
|
||||
export default function Home() {
|
||||
const menuItems = [
|
||||
{
|
||||
title: 'انتخاب دستگاه',
|
||||
description: 'اتصال به دستگاه گلخانه',
|
||||
href: '/devices',
|
||||
icon: Settings,
|
||||
color: 'from-blue-500 to-blue-600',
|
||||
iconColor: 'text-blue-500'
|
||||
},
|
||||
{
|
||||
title: 'تقویم',
|
||||
description: 'مشاهده دادههای ماهانه',
|
||||
href: '/calendar?deviceId=1',
|
||||
icon: Calendar,
|
||||
color: 'from-green-500 to-green-600',
|
||||
iconColor: 'text-green-500'
|
||||
},
|
||||
{
|
||||
title: 'جزئیات روز',
|
||||
description: 'دادههای روزانه',
|
||||
href: '/day-details?deviceId=1&year=1403&month=1',
|
||||
icon: BarChart3,
|
||||
color: 'from-purple-500 to-purple-600',
|
||||
iconColor: 'text-purple-500'
|
||||
},
|
||||
{
|
||||
title: 'دادههای تلهمتری',
|
||||
description: 'مشاهده دادههای لحظهای',
|
||||
href: '/telemetry?deviceId=1',
|
||||
icon: Leaf,
|
||||
color: 'from-orange-500 to-orange-600',
|
||||
iconColor: 'text-orange-500'
|
||||
const router = useRouter()
|
||||
const [mobile, setMobile] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const mobileRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
// Check if user is logged in
|
||||
const token = localStorage.getItem('authToken')
|
||||
const userStr = localStorage.getItem('user')
|
||||
|
||||
if (token && userStr) {
|
||||
try {
|
||||
const user = JSON.parse(userStr)
|
||||
// Redirect to devices check
|
||||
router.push('/devices')
|
||||
} catch {
|
||||
// Invalid user data, go to login
|
||||
}
|
||||
}
|
||||
]
|
||||
}, [router])
|
||||
|
||||
useEffect(() => {
|
||||
const checkAutoFill = () => {
|
||||
setTimeout(() => {
|
||||
if (mobileRef.current) {
|
||||
const filledMobile = mobileRef.current.value
|
||||
if (filledMobile && filledMobile !== mobile) {
|
||||
setMobile(filledMobile)
|
||||
}
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
|
||||
checkAutoFill()
|
||||
window.addEventListener('load', checkAutoFill)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('load', checkAutoFill)
|
||||
}
|
||||
}, [mobile])
|
||||
|
||||
const handleInputChange = (e: React.FormEvent<HTMLInputElement>) => {
|
||||
const value = e.currentTarget.value
|
||||
// Only allow digits
|
||||
const digitsOnly = value.replace(/\D/g, '')
|
||||
setMobile(digitsOnly)
|
||||
setError(null)
|
||||
}
|
||||
|
||||
const normalizeMobile = (mobile: string): string => {
|
||||
const digitsOnly = mobile.replace(/\D/g, '')
|
||||
if (digitsOnly.startsWith('9') && digitsOnly.length === 10) {
|
||||
return '0' + digitsOnly
|
||||
}
|
||||
if (digitsOnly.startsWith('0098') && digitsOnly.length === 13) {
|
||||
return '0' + digitsOnly.substring(3)
|
||||
}
|
||||
if (digitsOnly.startsWith('98') && digitsOnly.length === 12) {
|
||||
return '0' + digitsOnly.substring(2)
|
||||
}
|
||||
return digitsOnly
|
||||
}
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setError(null)
|
||||
|
||||
const normalizedMobile = normalizeMobile(mobile)
|
||||
|
||||
if (!normalizedMobile || normalizedMobile.length !== 11 || !normalizedMobile.startsWith('09')) {
|
||||
setError('لطفاً شماره موبایل معتبر وارد کنید (مثال: 09123456789)')
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
|
||||
try {
|
||||
const result = await api.sendCode({ mobile: normalizedMobile })
|
||||
|
||||
if (result.success) {
|
||||
// Redirect to verify page with mobile number
|
||||
router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`)
|
||||
} else {
|
||||
setError(result.message || 'خطا در ارسال کد')
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error sending code:', error)
|
||||
setError(error.message || 'خطا در ارتباط با سرور')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const canSubmit = mobile.length >= 10
|
||||
|
||||
if (loading) {
|
||||
return <Loading message="در حال ارسال کد..." />
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="min-h-screen p-4 md:p-8">
|
||||
<div className="max-w-6xl mx-auto">
|
||||
{/* Header */}
|
||||
<div className="text-center mb-12 mt-8">
|
||||
<div className="inline-flex items-center justify-center w-20 h-20 bg-gradient-to-br from-green-500 to-green-600 rounded-2xl shadow-lg mb-4">
|
||||
<HomeIcon className="w-10 h-10 text-white" />
|
||||
<div className="min-h-screen flex items-center justify-center p-4">
|
||||
<div className="w-full max-w-md">
|
||||
<div className="bg-white rounded-2xl shadow-xl p-8 border border-gray-100">
|
||||
<div className="text-center mb-8">
|
||||
<div className="inline-flex items-center justify-center w-16 h-16 bg-gradient-to-br from-green-500 to-green-600 rounded-2xl shadow-lg mb-4">
|
||||
<Smartphone className="w-8 h-8 text-white" />
|
||||
</div>
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-2">
|
||||
ورود با شماره موبایل
|
||||
</h1>
|
||||
<p className="text-sm text-gray-600">
|
||||
شماره موبایل خود را وارد کنید تا کد فعالسازی برای شما ارسال شود
|
||||
</p>
|
||||
</div>
|
||||
<h1 className="text-4xl md:text-5xl font-bold text-gray-900 mb-3">GreenHome</h1>
|
||||
<p className="text-lg text-gray-600">مدیریت هوشمند گلخانه</p>
|
||||
</div>
|
||||
|
||||
{/* Menu Grid */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
{menuItems.map((item) => {
|
||||
const Icon = item.icon
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className="group relative bg-white rounded-2xl shadow-md hover:shadow-xl transition-all duration-300 overflow-hidden border border-gray-100"
|
||||
>
|
||||
<div className="p-6">
|
||||
<div className="flex items-start gap-4">
|
||||
<div className={`flex-shrink-0 w-14 h-14 rounded-xl bg-gradient-to-br ${item.color} flex items-center justify-center shadow-md group-hover:scale-110 transition-transform duration-300`}>
|
||||
<Icon className="w-7 h-7 text-white" />
|
||||
</div>
|
||||
<div className="flex-1">
|
||||
<h3 className="text-xl font-semibold text-gray-900 mb-1 group-hover:text-green-600 transition-colors">
|
||||
{item.title}
|
||||
</h3>
|
||||
<p className="text-sm text-gray-500">{item.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className={`absolute bottom-0 left-0 right-0 h-1 bg-gradient-to-r ${item.color} transform scale-x-0 group-hover:scale-x-100 transition-transform duration-300`} />
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
<form onSubmit={handleSubmit} className="space-y-5">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
شماره موبایل
|
||||
</label>
|
||||
<input
|
||||
ref={mobileRef}
|
||||
type="tel"
|
||||
inputMode="numeric"
|
||||
className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 bg-gray-50 focus:border-green-500 focus:bg-white focus:outline-none focus:ring-2 focus:ring-green-500/20 transition-all duration-200 text-left"
|
||||
placeholder="09123456789"
|
||||
value={mobile}
|
||||
onInput={handleInputChange}
|
||||
disabled={loading}
|
||||
maxLength={11}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Footer Info */}
|
||||
<div className="mt-12 text-center">
|
||||
<p className="text-sm text-gray-500">
|
||||
سیستم مدیریت و مانیتورینگ گلخانه هوشمند
|
||||
</p>
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-xl text-sm text-center">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading || !canSubmit}
|
||||
className={`w-full py-3 rounded-xl text-white font-medium transition-all duration-200 flex items-center justify-center gap-2 shadow-md ${
|
||||
!loading && canSubmit
|
||||
? 'bg-gradient-to-r from-green-500 to-green-600 hover:from-green-600 hover:to-green-700 hover:shadow-lg transform hover:-translate-y-0.5'
|
||||
: 'bg-gray-300 cursor-not-allowed'
|
||||
}`}
|
||||
>
|
||||
{loading ? (
|
||||
<>
|
||||
<div className="w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin" />
|
||||
در حال ارسال...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
ارسال کد فعالسازی
|
||||
<ArrowLeft className="w-5 h-5" />
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user