mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-05 03:02:56 +00:00
dev day #89
This commit is contained in:
@@ -2,15 +2,45 @@ import api, { API_URL } from './api';
|
||||
import { getToken } from '../utils/auth';
|
||||
|
||||
const normalizeArticle = (raw: any): Article => {
|
||||
if (!raw) return raw;
|
||||
if (!raw) return raw as Article;
|
||||
const id = raw.id ?? raw.ID ?? raw.article_id ?? raw.articleId;
|
||||
const category = raw.category ?? raw.Category;
|
||||
const author = raw.author ?? raw.Author;
|
||||
|
||||
// Normalize attachments: backend may send a JSON string or an array
|
||||
let attachments: Array<{ name: string; url: string; mime_type?: string; size?: number }> | undefined = undefined;
|
||||
const aRaw = raw.attachments ?? raw.Attachments;
|
||||
try {
|
||||
if (Array.isArray(aRaw)) {
|
||||
attachments = aRaw.map((it: any) => {
|
||||
if (typeof it === 'string') {
|
||||
const name = it.split('/').pop() || 'soubor';
|
||||
return { name, url: it };
|
||||
}
|
||||
return { name: it?.name || (String(it?.url || '').split('/').pop() || 'soubor'), url: it?.url || '', mime_type: it?.mime_type || it?.type, size: it?.size };
|
||||
});
|
||||
} else if (typeof aRaw === 'string' && aRaw.trim() !== '') {
|
||||
const parsed = JSON.parse(aRaw);
|
||||
if (Array.isArray(parsed)) {
|
||||
attachments = parsed.map((it: any) => {
|
||||
if (typeof it === 'string') {
|
||||
const name = it.split('/').pop() || 'soubor';
|
||||
return { name, url: it };
|
||||
}
|
||||
return { name: it?.name || (String(it?.url || '').split('/').pop() || 'soubor'), url: it?.url || '', mime_type: it?.mime_type || it?.type, size: it?.size };
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore malformed attachments
|
||||
}
|
||||
|
||||
return {
|
||||
...(raw as Article),
|
||||
id,
|
||||
category,
|
||||
author,
|
||||
...(attachments ? { attachments } : {}),
|
||||
} as Article;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user