Files
main-tbbank/backend/web/admin/assets/js/resources.js
2026-08-04 18:01:20 +05:00

232 lines
7.8 KiB
JavaScript

/**
* Declarative schemas for every admin CRUD resource.
*
* Column types: id | lang | text | image | date | bool | number
* Field types: lang | lang-textarea | lang-richtext | text | textarea | image | date | select
*
* Endpoints follow the backend envelope { status, message, code, data }
* where list payloads are { <listKey>: [...], count: N }.
*/
(function () {
"use strict";
window.Resources = {
categories: {
title: "Categories",
endpoint: "/api/v1/admin/categories",
listKey: "categories",
searchable: true,
statusToggle: true,
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "name", label: "Name", type: "lang" },
{ key: "status", label: "Status", type: "bool", sortKey: "status" },
{ key: "createdAt", label: "Created", type: "date", sortKey: "createdAt" },
],
fields: [
{ name: "name", label: "Name", type: "lang", required: true },
],
buildPayload: (values, mode, record) => {
if (mode === "edit") {
return { name: values.name, status: record ? !!record.status : true };
}
return { name: values.name };
},
},
"sub-categories": {
title: "Sub-categories",
endpoint: "/api/v1/admin/sub-categories",
listKey: "subCategory",
searchable: true,
statusToggle: true,
// The list endpoint is scoped by category: GET /api/v1/admin/sub-categories/:categoryId
parentFilter: {
label: "Category",
endpoint: "/api/v1/admin/categories",
listKey: "categories",
labelField: "name",
},
listUrl: (state) => "/api/v1/admin/sub-categories/" + state.parentId,
getOneUrl: (id) => "/api/v1/admin/sub-categories/" + id + "/edit",
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "name", label: "Name", type: "lang" },
{ key: "status", label: "Status", type: "bool", sortKey: "status" },
{ key: "createdAt", label: "Created", type: "date", sortKey: "createdAt" },
],
fields: [
{
name: "categoryId",
label: "Category",
type: "select",
required: true,
options: {
endpoint: "/api/v1/admin/categories",
listKey: "categories",
valueField: "Id",
labelField: "name",
},
},
{ name: "name", label: "Name", type: "lang", required: true },
{ name: "content", label: "Content", type: "lang-richtext", required: true },
],
buildPayload: (values) => ({
categoryId: Number(values.categoryId),
name: values.name,
content: values.content,
}),
},
banners: {
title: "Banners",
endpoint: "/api/v1/admin/banner",
listKey: "banners",
searchable: true,
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "imagePath", label: "Image", type: "image" },
{ key: "title", label: "Title", type: "lang" },
{ key: "createdAt", label: "Created", type: "date", sortKey: "createdAt" },
],
fields: [
{ name: "title", label: "Title", type: "lang", required: true },
{
name: "content",
label: "Content",
type: "lang-richtext",
flat: true, // stored as contentTm / contentRu / contentEn
},
{ name: "imagePath", label: "Image", type: "image", required: true },
],
buildPayload: (values) => ({
title: values.title,
contentTm: (values.content && values.content.tm) || "",
contentRu: (values.content && values.content.ru) || "",
contentEn: (values.content && values.content.en) || "",
imagePath: values.imagePath,
}),
recordToValues: (record) => ({
title: record.title,
content: {
tm: record.contentTm || "",
ru: record.contentRu || "",
en: record.contentEn || "",
},
imagePath: record.imagePath,
}),
},
news: {
title: "News",
endpoint: "/api/v1/admin/news",
listKey: "news",
searchable: true,
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "imagePath", label: "Image", type: "image" },
{ key: "title", label: "Title", type: "lang" },
{ key: "dateRelease", label: "Release date", type: "date", sortKey: "dateRelease" },
],
fields: [
{ name: "title", label: "Title", type: "lang", required: true },
{ name: "content", label: "Content", type: "lang-richtext", required: true },
{ name: "imagePath", label: "Image", type: "image", required: true },
{ name: "dateRelease", label: "Release date", type: "date", required: true },
],
buildPayload: (values) => ({
title: values.title,
content: values.content,
imagePath: values.imagePath,
dateRelease: values.dateRelease,
}),
},
static: {
title: "Static pages",
endpoint: "/api/v1/admin/static",
listKey: "statics",
searchable: true,
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "logo", label: "Logo", type: "image" },
{ key: "title", label: "Title", type: "lang" },
{ key: "createdAt", label: "Created", type: "date", sortKey: "createdAt" },
],
fields: [
{ name: "logo", label: "Logo", type: "image", required: true },
{ name: "title", label: "Title", type: "lang", required: true },
{
name: "content",
label: "Content",
type: "lang-richtext",
flat: true, // stored as contentTm / contentRu / contentEn
},
],
buildPayload: (values) => ({
logo: values.logo,
title: values.title,
contentTm: (values.content && values.content.tm) || "",
contentRu: (values.content && values.content.ru) || "",
contentEn: (values.content && values.content.en) || "",
}),
/** Map a fetched record into form values. */
recordToValues: (record) => ({
logo: record.logo,
title: record.title,
content: {
tm: record.contentTm || "",
ru: record.contentRu || "",
en: record.contentEn || "",
},
}),
},
currency: {
title: "Currency",
endpoint: "/api/v1/admin/currency",
listKey: "currencies",
searchable: true,
columns: [
{ key: "Id", label: "ID", type: "id", sortKey: "id" },
{ key: "path", label: "Image", type: "image" },
{ key: "title", label: "Title", type: "lang" },
{ key: "createdAt", label: "Created", type: "date", sortKey: "createdAt" },
],
fields: [
{ name: "path", label: "Image", type: "image", required: true },
{ name: "title", label: "Title", type: "lang", required: true },
{
name: "content",
label: "Content",
type: "lang-richtext",
flat: true, // stored as contentTm / contentRu / contentEn
},
],
buildPayload: (values) => ({
path: values.path,
title: values.title,
contentTm: (values.content && values.content.tm) || "",
contentRu: (values.content && values.content.ru) || "",
contentEn: (values.content && values.content.en) || "",
}),
recordToValues: (record) => ({
path: record.path,
title: record.title,
content: {
tm: record.contentTm || "",
ru: record.contentRu || "",
en: record.contentEn || "",
},
}),
},
};
/** Preferred display order for a multilingual value. */
window.langText = function (value) {
if (!value) return "";
if (typeof value === "string") return value;
return value.tm || value.ru || value.en || "";
};
})();