feat: Implement a structured API service layer for authentication and user management, replacing the old API utility.

This commit is contained in:
pradeepkumar
2025-12-22 11:53:38 +05:30
parent aa97e36cfd
commit c4ccb2bd6c
10 changed files with 469 additions and 198 deletions

View File

@@ -2,7 +2,7 @@
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { api, type User } from '@/lib/api';
import { usersService, User, getErrorMessage } from '@/services';
export default function UsersPage() {
const router = useRouter();
@@ -21,12 +21,13 @@ export default function UsersPage() {
setIsLoading(true);
setError('');
try {
const response = await api.getUsers(currentPage, limit);
const response = await usersService.getUsers({ page: currentPage, limit });
setUsers(response.users || []);
setTotalUsers(response.total || 0);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch users');
if (err instanceof Error && err.message.includes('Unauthorized')) {
const errorMessage = getErrorMessage(err);
setError(errorMessage);
if (errorMessage.includes('Unauthorized')) {
router.push('/login');
}
} finally {