fix loading issue and api error on empty response

This commit is contained in:
2026-05-11 21:21:21 +03:30
parent e32fb90296
commit 676f1373c1
4 changed files with 21 additions and 2 deletions

View File

@@ -46,6 +46,7 @@ export default function LoginPage() {
if (result.success) { if (result.success) {
// Redirect to verify page with mobile number // Redirect to verify page with mobile number
router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`) router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`)
return // Don't set loading to false — component will unmount
} else { } else {
setError(result.message || 'خطا در ارسال کد') setError(result.message || 'خطا در ارسال کد')
} }

View File

@@ -62,6 +62,7 @@ export default function Home() {
if (result.success) { if (result.success) {
// Redirect to verify page with mobile number // Redirect to verify page with mobile number
router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`) router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`)
return // Don't set loading to false — component will unmount
} else { } else {
setError(result.message || 'خطا در ارسال کد') setError(result.message || 'خطا در ارسال کد')
} }

View File

@@ -93,14 +93,17 @@ function VerifyCodeContent() {
} else if (devicesResult.totalCount === 1) { } else if (devicesResult.totalCount === 1) {
// Single device - redirect to calendar // Single device - redirect to calendar
router.push(`/calendar?deviceId=${devicesResult.items[0].id}`) router.push(`/calendar?deviceId=${devicesResult.items[0].id}`)
return // Don't set loading to false — component will unmount
} else { } else {
// Multiple devices - redirect to device list // Multiple devices - redirect to device list
router.push('/devices') router.push('/devices')
return // Don't set loading to false — component will unmount
} }
} catch (deviceError) { } catch (deviceError) {
console.error('Error fetching devices:', deviceError) console.error('Error fetching devices:', deviceError)
// Still redirect to devices page to handle error there // Still redirect to devices page to handle error there
router.push('/devices') router.push('/devices')
return // Don't set loading to false — component will unmount
} }
} else { } else {
setError(result.message || 'کد وارد شده نادرست است') setError(result.message || 'کد وارد شده نادرست است')

View File

@@ -17,6 +17,8 @@ import type {
const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? 'https://ghback.nabaksoft.ir' const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? 'https://ghback.nabaksoft.ir'
console.log('api-base: ', API_BASE);
async function http<T>(url: string, init?: RequestInit): Promise<T> { async function http<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers || {}) } }) const res = await fetch(url, { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers || {}) } })
if (!res.ok) { if (!res.ok) {
@@ -33,7 +35,14 @@ async function http<T>(url: string, init?: RequestInit): Promise<T> {
error.status = res.status error.status = res.status
throw error throw error
} }
return res.json() as Promise<T>
// Check if response has content
const text = await res.text()
if (!text) {
return undefined as T // or return null as T, depending on your preference
}
return JSON.parse(text) as T
} }
export const api = { export const api = {
@@ -65,7 +74,12 @@ export const api = {
updateDeviceSettings: (dto: DeviceSettingsDto) => http<void>(`${API_BASE}/api/devicesettings`, { method: 'PUT', body: JSON.stringify(dto) }), updateDeviceSettings: (dto: DeviceSettingsDto) => http<void>(`${API_BASE}/api/devicesettings`, { method: 'PUT', body: JSON.stringify(dto) }),
// Authentication // Authentication
sendCode: (request: SendCodeRequest) => http<SendCodeResponse>(`${API_BASE}/api/auth/send-code`, { method: 'POST', body: JSON.stringify(request) }), sendCode: (request: SendCodeRequest) => {
const url = `${API_BASE}/api/auth/send-code`;
console.log('posting to ' + url);
return http<SendCodeResponse>(url, { method: 'POST', body: JSON.stringify(request) })
},
verifyCode: (request: VerifyCodeRequest) => http<VerifyCodeResponse>(`${API_BASE}/api/auth/verify-code`, { method: 'POST', body: JSON.stringify(request) }), verifyCode: (request: VerifyCodeRequest) => http<VerifyCodeResponse>(`${API_BASE}/api/auth/verify-code`, { method: 'POST', body: JSON.stringify(request) }),
canResend: (mobile: string) => http<{ canResend: boolean }>(`${API_BASE}/api/auth/can-resend?mobile=${encodeURIComponent(mobile)}`), canResend: (mobile: string) => http<{ canResend: boolean }>(`${API_BASE}/api/auth/can-resend?mobile=${encodeURIComponent(mobile)}`),