Adding mapping function to match API data with current document structure

The current API endpoint data doesn't match the document structure in the frontend logic. So I introduced a mapping function but it has not been called into the code yet as this needs a major refactoring to do.
This commit is contained in:
shb 2025-06-13 11:29:45 +08:00
parent 3945f208b0
commit 6cb4396f20
2 changed files with 72 additions and 11 deletions

View File

@ -78,7 +78,8 @@
```json ```json
{ {
"status": 200, "status": 200,
"message": "Folder deleted successfully" "message": "Folder deleted successfully",
"folder": {...}
} }
``` ```

View File

@ -322,25 +322,78 @@ const getFileTypeColor = (fileName) => {
return colorMap[extension] || colorMap.default; return colorMap[extension] || colorMap.default;
}; };
const mapAPIToDocumentStructure = async () => {
const response = await fetch('/api/dms/folder');
const APIData = await response.json();
// const documentStructure.value = APIData.map(obj => {
// id: obj.cb_id,
// name: obj.cb_name,
// type: obj.cb_type || "folder",
// parent_id: obj.cb_parent_id,
// access_level: obj.cb_access_level || "public",
// itemCount: null,
// children: obj.children_count || null
// })
return documentStructure.value;
}
// Navigation functions // Navigation functions
const buildBreadcrumbs = (path) => { const buildBreadcrumbs = (path) => {
if (path === '/') { if (path === '/') {
return [{ name: 'Root', path: '/', type: 'root' }]; return [{ name: 'Root', path: '/', type: 'root' }];
} }
const segments = path.split('/').filter(Boolean);
const crumbs = [{ name: 'Root', path: '/', type: 'root' }]; const crumbs = [{ name: 'Root', path: '/', type: 'root' }];
const segments = path.split('/').filter(Boolean);
// const documentStructure.value = await mapAPIToDocumentStructure();
let itemsToSearch = documentStructure.value;
let currentPath = ''; let currentPath = '';
segments.forEach((segment, index) => {
for (const segment of segments) {
currentPath += '/' + segment; currentPath += '/' + segment;
const item = findItemByPath(currentPath); const isApiStructure = itemsToSearch.length > 0 && 'cb_id' in itemsToSearch[0];
crumbs.push({
name: item?.name || segment, // Slugify function to match path segments with names
path: currentPath, const slugify = (text) => text.toString().toLowerCase()
type: item?.type || 'folder' .replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '');
const foundItem = itemsToSearch.find(item => {
const itemName = isApiStructure ? item.cabinet_name : item.name;
if (!itemName) return false;
if (item.path) {
// Prefer matching with existing path property
return item.path === currentPath;
}
// Fallback to comparing slugified names
return slugify(itemName) === segment;
}); });
});
if (foundItem) {
const itemName = isApiStructure ? foundItem.cabinet_name : foundItem.name;
const itemPath = foundItem.path || currentPath;
const itemType = foundItem.type || 'folder';
crumbs.push({
name: itemName,
path: itemPath,
type: itemType
});
itemsToSearch = foundItem.children || [];
} else {
// If an item is not found in the structure, use the segment as its name
// This can happen for paths pointing to files or during transitions
crumbs.push({
name: segment,
path: currentPath,
type: 'folder'
});
itemsToSearch = []; // Stop searching deeper
}
}
return crumbs; return crumbs;
}; };
@ -434,11 +487,18 @@ const loadFolderContents = async (path) => {
}; };
const findItemByPath = (path, items = documentStructure.value) => { const findItemByPath = (path, items = documentStructure.value) => {
// Check if we're using the API-based structure or mock structure
const isApiStructure = items.length > 0 && 'cb_id' in items[0];
for (const item of items) { for (const item of items) {
if (item.path === path) { const itemPath = isApiStructure ? (item.path || ('/' + (item.cabinet_name || '').toString().toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, ''))) : item.path;
if (itemPath === path) {
return item; return item;
} }
if (item.children && path.startsWith(item.path)) {
// Check children if they exist and path might be inside this subtree
if (item.children && path.startsWith(itemPath + (itemPath === '/' ? '' : '/'))) {
const found = findItemByPath(path, item.children); const found = findItemByPath(path, item.children);
if (found) return found; if (found) return found;
} }