Files
Filamenteka/src/services/api.ts
DaX 0df9d5d294 Add sale management feature for admin panel
- Add database migration for sale fields (percentage, active, dates)
- Update API to handle sale operations and bulk updates
- Create SaleManager component for admin interface
- Update FilamentTableV2 to display sale prices on frontend
- Add sale column in admin dashboard
- Implement sale price calculations with strikethrough styling
2025-07-05 14:48:31 +02:00

103 lines
2.7 KiB
TypeScript

import axios from 'axios';
import { Filament } from '@/src/types/filament';
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api';
// Create axios instance with default config
const api = axios.create({
baseURL: API_URL,
headers: {
'Content-Type': 'application/json',
},
});
// Add auth token to requests
api.interceptors.request.use((config) => {
const token = localStorage.getItem('authToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
// Handle auth errors
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401 || error.response?.status === 403) {
// Clear auth data
localStorage.removeItem('authToken');
localStorage.removeItem('tokenExpiry');
// Only redirect if we're in a protected admin route
if (window.location.pathname.includes('/upadaj/')) {
window.location.href = '/upadaj';
}
}
return Promise.reject(error);
}
);
export const authService = {
login: async (username: string, password: string) => {
const response = await api.post('/login', { username, password });
return response.data;
},
};
export const colorService = {
getAll: async () => {
const response = await api.get('/colors');
return response.data;
},
create: async (color: { name: string; hex: string; cena_refill?: number; cena_spulna?: number }) => {
const response = await api.post('/colors', color);
return response.data;
},
update: async (id: string, color: { name: string; hex: string; cena_refill?: number; cena_spulna?: number }) => {
const response = await api.put(`/colors/${id}`, color);
return response.data;
},
delete: async (id: string) => {
const response = await api.delete(`/colors/${id}`);
return response.data;
},
};
export const filamentService = {
getAll: async () => {
const response = await api.get('/filaments');
return response.data;
},
create: async (filament: Partial<Filament>) => {
const response = await api.post('/filaments', filament);
return response.data;
},
update: async (id: string, filament: Partial<Filament>) => {
const response = await api.put(`/filaments/${id}`, filament);
return response.data;
},
delete: async (id: string) => {
const response = await api.delete(`/filaments/${id}`);
return response.data;
},
updateBulkSale: async (data: {
filamentIds?: string[];
salePercentage: number;
saleStartDate?: string;
saleEndDate?: string;
enableSale: boolean;
}) => {
const response = await api.post('/filaments/sale/bulk', data);
return response.data;
},
};
export default api;