uchill/front_minimal/src/utils/materials-api.js

72 lines
3.1 KiB
JavaScript

import axios from 'src/utils/axios';
// ----------------------------------------------------------------------
export async function getMaterials(params) {
const res = await axios.get('/materials/materials/', { params });
const {data} = res;
if (Array.isArray(data)) return { results: data, count: data.length };
return { results: data?.results ?? [], count: data?.count ?? 0 };
}
export async function getMyMaterials() {
const res = await axios.get('/materials/materials/my_materials/');
const {data} = res;
if (Array.isArray(data)) return data;
return data?.results ?? [];
}
export async function getMaterialById(id) {
const res = await axios.get(`/materials/materials/${id}/`);
return res.data;
}
export async function createMaterial(data) {
const formData = new FormData();
formData.append('title', data.title);
if (data.description) formData.append('description', data.description);
if (data.file) formData.append('file', data.file);
if (data.category) formData.append('category', String(data.category));
if (data.is_public !== undefined) formData.append('is_public', String(data.is_public));
const res = await axios.post('/materials/materials/', formData);
return res.data;
}
export async function updateMaterial(id, data) {
const formData = new FormData();
if (data.title) formData.append('title', data.title);
if (data.description !== undefined) formData.append('description', data.description);
if (data.file) formData.append('file', data.file);
if (data.category) formData.append('category', String(data.category));
if (data.is_public !== undefined) formData.append('is_public', String(data.is_public));
const res = await axios.patch(`/materials/materials/${id}/`, formData);
return res.data;
}
export async function deleteMaterial(id) {
await axios.delete(`/materials/materials/${id}/`);
}
export async function shareMaterial(id, userIds) {
await axios.post(`/materials/materials/${id}/share/`, { user_ids: userIds });
}
export async function getMaterialCategories() {
const res = await axios.get('/materials/categories/');
return res.data;
}
// Helper: icon by type
export function getMaterialTypeIcon(material) {
const type = material?.material_type;
const mime = (material?.file_type || '').toLowerCase();
const name = material?.file_name || material?.file || '';
if (type === 'image' || mime.startsWith('image/') || /\.(jpe?g|png|gif|webp|bmp|svg)$/i.test(name)) return 'eva:image-outline';
if (type === 'video' || mime.startsWith('video/') || /\.(mp4|webm|mov|avi)$/i.test(name)) return 'eva:video-outline';
if (type === 'audio' || mime.startsWith('audio/') || /\.(mp3|wav|ogg|m4a)$/i.test(name)) return 'eva:headphones-outline';
if (type === 'document' || mime.includes('pdf') || /\.(pdf|docx?|odt)$/i.test(name)) return 'eva:file-text-outline';
if (type === 'presentation' || /\.(pptx?|odp)$/i.test(name)) return 'eva:monitor-outline';
if (type === 'archive' || /\.(zip|rar|7z|tar)$/i.test(name)) return 'eva:archive-outline';
return 'eva:file-outline';
}