65 lines
2.5 KiB
TypeScript
65 lines
2.5 KiB
TypeScript
import { apiClient } from './client';
|
|
import { Promotion, CreatePromotionData, UpdatePromotionData, ApiResponse } from '@/types/promotion';
|
|
|
|
export const promotionsApi = {
|
|
// Get all promotions
|
|
getPromotions: async (token: string): Promise<Promotion[]> => {
|
|
return apiClient.get<Promotion[]>('/promotions', token);
|
|
},
|
|
|
|
// Get single promotion
|
|
getPromotion: async (id: number, token: string): Promise<Promotion> => {
|
|
return apiClient.get<Promotion>(`/promotions/${id}`, token);
|
|
},
|
|
|
|
// Create new promotion
|
|
createPromotion: async (data: CreatePromotionData, token: string): Promise<ApiResponse<Promotion>> => {
|
|
const formData = new FormData();
|
|
|
|
// Append all fields to FormData
|
|
Object.entries(data).forEach(([key, value]) => {
|
|
if (value !== null && value !== undefined) {
|
|
if (key === 'image' && value instanceof File) {
|
|
formData.append('image', value);
|
|
} else {
|
|
formData.append(key, String(value));
|
|
}
|
|
}
|
|
});
|
|
|
|
return apiClient.post<ApiResponse<Promotion>>('/promotions', formData, token);
|
|
},
|
|
|
|
// Update promotion
|
|
updatePromotion: async (id: number, data: UpdatePromotionData, token: string): Promise<ApiResponse<Promotion>> => {
|
|
const formData = new FormData();
|
|
|
|
// Add method spoofing for Laravel
|
|
// formData.append('_method', 'PUT');
|
|
|
|
// Append all fields to FormData
|
|
Object.entries(data).forEach(([key, value]) => {
|
|
if (value !== null && value !== undefined) {
|
|
if (key === 'image' && value instanceof File) {
|
|
formData.append('image', value);
|
|
} else if (key === 'is_active') {
|
|
formData.append(key, value ? '1' : '0');
|
|
} else {
|
|
formData.append(key, String(value));
|
|
}
|
|
}
|
|
});
|
|
|
|
return apiClient.post<ApiResponse<Promotion>>(`/promotions/${id}`, formData, token);
|
|
},
|
|
|
|
// Delete promotion
|
|
deletePromotion: async (id: number, token: string): Promise<ApiResponse<any>> => {
|
|
return apiClient.delete<ApiResponse<any>>(`/promotions/${id}`, undefined, token);
|
|
},
|
|
|
|
// Toggle promotion active status
|
|
toggleActive: async (id: number, isActive: boolean, token: string): Promise<ApiResponse<Promotion>> => {
|
|
return apiClient.put<ApiResponse<Promotion>>(`/promotions/${id}`, { is_active: isActive }, token);
|
|
}
|
|
}; |