feat: added dropdowns, categories and some filters
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// src/api/CategoriesApi.js
|
||||
export default class CategoriesApi {
|
||||
constructor(token) {
|
||||
this.root = 'https://inventory-bff.dream-views.com/api/v1';
|
||||
// IMPORTANTE: singular "Tag", no "Tags"
|
||||
this.baseUrl = 'https://inventory-bff.dream-views.com/api/v1/Tag';
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@@ -13,65 +14,103 @@ export default class CategoriesApi {
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Tag ----
|
||||
// Utilidad: validar ObjectId (24-hex) — el DAL lo exige para ChangeStatus
|
||||
static isHex24(v) {
|
||||
return typeof v === 'string' && /^[0-9a-fA-F]{24}$/.test(v);
|
||||
}
|
||||
|
||||
// (Opcional) Utilidad: validar campos mínimos en create/update para evitar 400
|
||||
static ensureFields(obj, fields) {
|
||||
const missing = fields.filter((k) => obj[k] === undefined || obj[k] === null || obj[k] === '');
|
||||
if (missing.length) {
|
||||
throw new Error(`Missing required field(s): ${missing.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
// GET /Tag/GetAll
|
||||
async getAll() {
|
||||
const res = await fetch(`${this.root}/Tag/GetAll`, {
|
||||
const res = await fetch(`${this.baseUrl}/GetAll`, {
|
||||
method: 'GET',
|
||||
headers: this.headers(false),
|
||||
});
|
||||
if (!res.ok) throw new Error(`GetAll error ${res.status}: ${await res.text()}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`GetAll error ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
// POST /Tag/Create
|
||||
// payload esperado (min): tenantId, tagName, typeId(_id TagType), slug, displayOrder, icon, parentTagId([])
|
||||
async create(payload) {
|
||||
const res = await fetch(`${this.root}/Tag/Create`, {
|
||||
// Validaciones básicas para evitar 400 comunes
|
||||
CategoriesApi.ensureFields(payload, ['tenantId', 'tagName', 'typeId', 'icon']);
|
||||
if (!Array.isArray(payload.parentTagId)) {
|
||||
payload.parentTagId = [];
|
||||
}
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/Create`, {
|
||||
method: 'POST',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) throw new Error(`Create error ${res.status}: ${await res.text()}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Create error ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
// PUT /Tag/Update
|
||||
// payload esperado (min): id(GUID) ó _id(24-hex) según backend, + tenantId, tagName, typeId, icon, etc.
|
||||
async update(payload) {
|
||||
const res = await fetch(`${this.root}/Tag/Update`, {
|
||||
CategoriesApi.ensureFields(payload, ['tenantId', 'tagName', 'typeId', 'icon']);
|
||||
const res = await fetch(`${this.baseUrl}/Update`, {
|
||||
method: 'PUT',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) throw new Error(`Update error ${res.status}: ${await res.text()}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Update error ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
// PATCH ChangeStatus: body { id, status }
|
||||
// PATCH /Tag/ChangeStatus
|
||||
// Debe mandarse { id: <_id 24-hex>, status: 'Active'|'Inactive' }
|
||||
async changeStatus({ id, status }) {
|
||||
const res = await fetch(`${this.root}/Tag/ChangeStatus`, {
|
||||
if (!CategoriesApi.isHex24(id)) {
|
||||
// Evitar el 500 "String should contain only hexadecimal digits."
|
||||
throw new Error('ChangeStatus requires a Mongo _id (24-hex) for "id".');
|
||||
}
|
||||
if (!status) {
|
||||
throw new Error('ChangeStatus requires "status" field.');
|
||||
}
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/ChangeStatus`, {
|
||||
method: 'PATCH',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify({ id, status }),
|
||||
});
|
||||
if (!res.ok) throw new Error(`ChangeStatus error ${res.status}: ${await res.text()}`);
|
||||
return res.json?.() ?? null;
|
||||
if (!res.ok) {
|
||||
throw new Error(`ChangeStatus error ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
// Algunos endpoints devuelven vacío; devolvemos parsed o true
|
||||
try {
|
||||
return await res.json();
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /Tag/Delete (si lo usan; muchos usan soft-delete con ChangeStatus/Update)
|
||||
async delete(payload) {
|
||||
const res = await fetch(`${this.tagUrl}/Delete`, {
|
||||
const res = await fetch(`${this.baseUrl}/Delete`, {
|
||||
method: 'DELETE',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) throw new Error(`Delete error ${res.status}: ${await res.text()}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
// ---- TagType ----
|
||||
async getAllTypes() {
|
||||
const res = await fetch(`${this.root}/TagType/GetAll`, {
|
||||
method: 'GET',
|
||||
headers: this.headers(false),
|
||||
});
|
||||
if (!res.ok) throw new Error(`TagType GetAll error ${res.status}: ${await res.text()}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Delete error ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
src/api/TagTypeApi.js
Normal file
26
src/api/TagTypeApi.js
Normal file
@@ -0,0 +1,26 @@
|
||||
// src/api/TagTypeApi.js
|
||||
export default class TagTypeApi {
|
||||
constructor(token) {
|
||||
this.baseUrl = 'https://inventory-bff.dream-views.com/api/v1/TagType';
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
headers(json = true) {
|
||||
return {
|
||||
accept: 'application/json',
|
||||
...(json ? { 'Content-Type': 'application/json' } : {}),
|
||||
...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
const res = await fetch(`${this.baseUrl}/GetAll`, {
|
||||
method: 'GET',
|
||||
headers: this.headers(false),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`TagType.GetAll ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user