2025-04-08 00:05:15 +08:00

157 lines
5.6 KiB
TypeScript

// api/api.ts
import { updateLoggedInState } from '../utils/utils';
// --- Define API Response Interfaces ---
export interface LoginResponseData { // Exported
userId: string;
fullName: string;
profilePicture: string;
schoolId: string;
birthdate: string;
token: string;
}
export interface ApiResponse<T> { // Exported
success: boolean;
data?: T;
message?: string;
}
export interface ProfileResponseData extends LoginResponseData { // Exported
posts: { id: number; content: string }[];
}
export interface AdminDashboardData { // Exported
studentsCount: number;
teachersCount: number;
}
export interface StudentListData { // Exported
id: number;
name: string;
yearLevel: string;
courses: string[];
}
export interface StudentFinancialData {
tuitionFee: string;
miscellaneousFee: string;
labFee: string;
currentAccount: string;
downPayment: string;
midtermPayment: string;
prefinalPayment: string;
finalPayment: string;
}
// --- Mock API Endpoints ---
const mockAPI = {
login: async (credentials: any): Promise<ApiResponse<LoginResponseData>> => { // Explicit return type, parameter type remains 'any' as per original spec
return new Promise((resolve, reject) => {
setTimeout(() => {
if (credentials.userId === '123456' && credentials.password === 'password') {
const userData: LoginResponseData = {
userId: generateMockUserId(), // Mock User ID
fullName: 'John Doe',
profilePicture: 'path/to/profile.jpg', // Placeholder
schoolId: '123456',
birthdate: '2000-01-01',
token: 'mock-user-token-123', // Mock token
};
resolve({ success: true, data: userData });
updateLoggedInState(true, userData.token); // Update login state on successful login
} else {
reject({ success: false, message: 'Invalid credentials' });
}
}, 1000); // Simulate API latency
});
},
logout: async (): Promise<ApiResponse<void>> => {
return new Promise(resolve => {
setTimeout(() => {
updateLoggedInState(false, null);
resolve({ success: true });
}, 500);
});
},
getProfile: async (userId: string): Promise<ApiResponse<ProfileResponseData>> => {
return new Promise(resolve => {
setTimeout(() => {
const profileData: ProfileResponseData = {
userId: userId,
fullName: 'John Doe',
profilePicture: 'path/to/profile.jpg', // Placeholder
schoolId: '123456',
birthdate: '2000-01-01',
token: 'mock-user-token-123', // Added token here to match ProfileResponseData interface
posts: [
{ id: 1, content: 'First post on profile!' },
{ id: 2, content: 'Another profile post.' }
]
};
resolve({ success: true, data: profileData });
}, 500);
});
},
updateAccountSettings: async (userId: string, settings: any): Promise<ApiResponse<void>> => { // parameter type remains 'any'
return new Promise(resolve => {
setTimeout(() => {
console.log('Account settings updated for user:', userId, settings);
resolve({ success: true });
}, 500);
});
},
getAdminDashboardData: async (): Promise<ApiResponse<AdminDashboardData>> => {
return new Promise(resolve => {
setTimeout(() => {
const dashboardData: AdminDashboardData = {
studentsCount: 150,
teachersCount: 30,
};
resolve({ success: true, data: dashboardData });
}, 500);
});
},
getStudentList: async (): Promise<ApiResponse<StudentListData[]>> => {
return new Promise(resolve => {
setTimeout(() => {
const studentList: StudentListData[] = [
{ id: 1, name: 'Alice Smith', yearLevel: '1', courses: ['Math', 'Science'] },
{ id: 2, name: 'Bob Johnson', yearLevel: '2', courses: ['History', 'English'] },
];
resolve({ success: true, data: studentList });
}, 500);
});
},
getStudentFinancialData: async (_studentId: string): Promise<ApiResponse<StudentFinancialData>> => { // parameter type remains 'string', unused parameter prefixed with _
return new Promise(resolve => {
setTimeout(() => {
const financialData: StudentFinancialData = {
tuitionFee: '5000',
miscellaneousFee: '500',
labFee: '200',
currentAccount: '-100',
downPayment: '2000',
midtermPayment: '1500',
prefinalPayment: '1000',
finalPayment: '500',
};
resolve({ success: true, data: financialData });
}, 500);
});
}
};
let mockUserIdCounter = 100000;
const generateMockUserId = () => {
return String(mockUserIdCounter++);
};
declare global {
interface Window {
globalAPI: typeof mockAPI;
}
}
export const setupGlobalAPI = () => {
window.globalAPI = mockAPI;
};
export const globalAPI = mockAPI;