feat: add department dashboard
This commit is contained in:
12
frontend/src/api/auth.ts
Normal file
12
frontend/src/api/auth.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import apiClient from "./client";
|
||||
|
||||
export const loginApi = async (
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<any> => {
|
||||
const response = await apiClient.post("/auth/login/", {
|
||||
username,
|
||||
password,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
48
frontend/src/api/client.ts
Normal file
48
frontend/src/api/client.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import axios from "axios";
|
||||
import type {InternalAxiosRequestConfig} from "axios";
|
||||
|
||||
const BASE_URL: string = "http://localhost:3000/api";
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
export const setAxiosAuthToken = (token: string | null): void => {
|
||||
if (token) {
|
||||
apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} else {
|
||||
delete apiClient.defaults.headers.common["Authorization"];
|
||||
}
|
||||
};
|
||||
|
||||
apiClient.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
if (token && config.headers) {
|
||||
config.headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error: any) => Promise.reject(error),
|
||||
);
|
||||
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
if (error.response?.status === 401) {
|
||||
console.error("Unauthorized - token missing or invalid");
|
||||
|
||||
localStorage.removeItem("token");
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default apiClient;
|
||||
57
frontend/src/api/department.ts
Normal file
57
frontend/src/api/department.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import apiClient from "@/api/client";
|
||||
|
||||
export interface Department {
|
||||
departmentId: string;
|
||||
name: string;
|
||||
para1: string;
|
||||
para2: string;
|
||||
para3: string;
|
||||
facilities: string;
|
||||
services: string;
|
||||
}
|
||||
|
||||
/* ---------------- GET ALL ---------------- */
|
||||
|
||||
export const getDepartmentsApi = async () => {
|
||||
const res = await apiClient.get("/departments/getAll");
|
||||
return res.data;
|
||||
};
|
||||
|
||||
/* ---------------- CREATE ---------------- */
|
||||
|
||||
export const createDepartmentApi = async (data: {
|
||||
departmentId: string;
|
||||
name: string;
|
||||
para1?: string;
|
||||
para2?: string;
|
||||
para3?: string;
|
||||
facilities?: string;
|
||||
services?: string;
|
||||
}) => {
|
||||
const res = await apiClient.post("/departments", data);
|
||||
return res.data;
|
||||
};
|
||||
|
||||
/* ---------------- UPDATE ---------------- */
|
||||
|
||||
export const updateDepartmentApi = async (
|
||||
id: number,
|
||||
data: {
|
||||
name?: string;
|
||||
para1?: string;
|
||||
para2?: string;
|
||||
para3?: string;
|
||||
facilities?: string;
|
||||
services?: string;
|
||||
},
|
||||
) => {
|
||||
const res = await apiClient.put(`/departments/${id}`, data);
|
||||
return res.data;
|
||||
};
|
||||
|
||||
/* ---------------- DELETE ---------------- */
|
||||
|
||||
export const deleteDepartmentApi = async (id: number) => {
|
||||
const res = await apiClient.delete(`/departments/${id}`);
|
||||
return res.data;
|
||||
};
|
||||
Reference in New Issue
Block a user