fix loading issue and api error on empty response
This commit is contained in:
@@ -46,6 +46,7 @@ export default function LoginPage() {
|
||||
if (result.success) {
|
||||
// Redirect to verify page with mobile number
|
||||
router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`)
|
||||
return // Don't set loading to false — component will unmount
|
||||
} else {
|
||||
setError(result.message || 'خطا در ارسال کد')
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ export default function Home() {
|
||||
if (result.success) {
|
||||
// Redirect to verify page with mobile number
|
||||
router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`)
|
||||
return // Don't set loading to false — component will unmount
|
||||
} else {
|
||||
setError(result.message || 'خطا در ارسال کد')
|
||||
}
|
||||
|
||||
@@ -93,14 +93,17 @@ function VerifyCodeContent() {
|
||||
} else if (devicesResult.totalCount === 1) {
|
||||
// Single device - redirect to calendar
|
||||
router.push(`/calendar?deviceId=${devicesResult.items[0].id}`)
|
||||
return // Don't set loading to false — component will unmount
|
||||
} else {
|
||||
// Multiple devices - redirect to device list
|
||||
router.push('/devices')
|
||||
return // Don't set loading to false — component will unmount
|
||||
}
|
||||
} catch (deviceError) {
|
||||
console.error('Error fetching devices:', deviceError)
|
||||
// Still redirect to devices page to handle error there
|
||||
router.push('/devices')
|
||||
return // Don't set loading to false — component will unmount
|
||||
}
|
||||
} else {
|
||||
setError(result.message || 'کد وارد شده نادرست است')
|
||||
|
||||
@@ -17,6 +17,8 @@ import type {
|
||||
|
||||
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> {
|
||||
const res = await fetch(url, { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers || {}) } })
|
||||
if (!res.ok) {
|
||||
@@ -33,7 +35,14 @@ async function http<T>(url: string, init?: RequestInit): Promise<T> {
|
||||
error.status = res.status
|
||||
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 = {
|
||||
@@ -65,7 +74,12 @@ export const api = {
|
||||
updateDeviceSettings: (dto: DeviceSettingsDto) => http<void>(`${API_BASE}/api/devicesettings`, { method: 'PUT', body: JSON.stringify(dto) }),
|
||||
|
||||
// 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) }),
|
||||
canResend: (mobile: string) => http<{ canResend: boolean }>(`${API_BASE}/api/auth/can-resend?mobile=${encodeURIComponent(mobile)}`),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user