generated from corrad-software/corrad-af-2024
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:
parent
3945f208b0
commit
6cb4396f20
@ -78,7 +78,8 @@
|
||||
```json
|
||||
{
|
||||
"status": 200,
|
||||
"message": "Folder deleted successfully"
|
||||
"message": "Folder deleted successfully",
|
||||
"folder": {...}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -322,25 +322,78 @@ const getFileTypeColor = (fileName) => {
|
||||
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
|
||||
const buildBreadcrumbs = (path) => {
|
||||
if (path === '/') {
|
||||
return [{ name: 'Root', path: '/', type: 'root' }];
|
||||
}
|
||||
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
const crumbs = [{ name: 'Root', path: '/', type: 'root' }];
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
|
||||
// const documentStructure.value = await mapAPIToDocumentStructure();
|
||||
let itemsToSearch = documentStructure.value;
|
||||
let currentPath = '';
|
||||
segments.forEach((segment, index) => {
|
||||
|
||||
for (const segment of segments) {
|
||||
currentPath += '/' + segment;
|
||||
const item = findItemByPath(currentPath);
|
||||
crumbs.push({
|
||||
name: item?.name || segment,
|
||||
path: currentPath,
|
||||
type: item?.type || 'folder'
|
||||
const isApiStructure = itemsToSearch.length > 0 && 'cb_id' in itemsToSearch[0];
|
||||
|
||||
// Slugify function to match path segments with names
|
||||
const slugify = (text) => text.toString().toLowerCase()
|
||||
.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;
|
||||
};
|
||||
@ -434,11 +487,18 @@ const loadFolderContents = async (path) => {
|
||||
};
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
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);
|
||||
if (found) return found;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user