adminizer 4.5.0-build.194 → 4.5.0-build.196

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { ControllerHelper } from "../helpers/controllerHelper.js";
2
2
  import { RequestProcessor } from "../lib/requestProcessor.js";
3
3
  import { FieldsHelper } from "../helpers/fieldsHelper.js";
4
- import { saveRelationsMediaManager } from "../lib/media-manager/helpers/MediaManagerHelper.js";
4
+ import { detachMediaManagerField, saveRelationsMediaManager } from "../lib/media-manager/helpers/MediaManagerHelper.js";
5
5
  import { DataAccessor } from "../lib/DataAccessor.js";
6
6
  import { Adminizer } from "../lib/Adminizer.js";
7
7
  import inertiaAddHelper from "../helpers/inertiaAddHelper.js";
@@ -50,15 +50,9 @@ export default async function add(req, res) {
50
50
  }
51
51
  }
52
52
  }
53
- if (fieldConfigConfig.type === 'mediamanager' && typeof reqData[prop] === "string") {
54
- try {
55
- const parsed = JSON.parse(reqData[prop]);
56
- rawReqData[prop] = parsed;
57
- }
58
- catch (error) {
59
- throw `Error assign association-many mediamanager data for ${prop}, ${reqData[prop]}`;
60
- }
61
- delete reqData[prop];
53
+ if (fieldConfigConfig.type === 'mediamanager') {
54
+ detachMediaManagerField(reqData, rawReqData, prop);
55
+ continue;
62
56
  }
63
57
  // delete property from association-many and association if empty
64
58
  if (fields[prop] && fields[prop].model && (fields[prop].model.type === 'association-many' || fields[prop].model.type === 'association')) {
@@ -1,7 +1,7 @@
1
1
  import { ControllerHelper } from "../helpers/controllerHelper.js";
2
2
  import { RequestProcessor } from "../lib/requestProcessor.js";
3
3
  import { FieldsHelper } from "../helpers/fieldsHelper.js";
4
- import { getRelationsMediaManager, saveRelationsMediaManager } from "../lib/media-manager/helpers/MediaManagerHelper.js";
4
+ import { detachMediaManagerField, getRelationsMediaManager, saveRelationsMediaManager } from "../lib/media-manager/helpers/MediaManagerHelper.js";
5
5
  import { DataAccessor } from "../lib/DataAccessor.js";
6
6
  import { Adminizer } from "../lib/Adminizer.js";
7
7
  import inertiaAddHelper from "../helpers/inertiaAddHelper.js";
@@ -76,15 +76,9 @@ export default async function edit(req, res) {
76
76
  }
77
77
  }
78
78
  }
79
- if (fieldConfigConfig.type === 'mediamanager' && typeof reqData[prop] === "string") {
80
- try {
81
- const parsed = JSON.parse(reqData[prop]);
82
- rawReqData[prop] = parsed;
83
- }
84
- catch (error) {
85
- throw `Error assign association-many mediamanager data for ${prop}, ${reqData[prop]}`;
86
- }
87
- delete reqData[prop];
79
+ if (fieldConfigConfig.type === 'mediamanager') {
80
+ detachMediaManagerField(reqData, rawReqData, prop);
81
+ continue;
88
82
  }
89
83
  if (fields[prop] && fields[prop].model && fields[prop].model.type === 'json' && reqData[prop] !== '') {
90
84
  if (typeof reqData[prop] === "string") {
@@ -1,7 +1,9 @@
1
1
  import { Fields } from "../../../helpers/fieldsHelper";
2
- import { MediaManagerItem, MediaManagerWidgetItem, MediaManagerWidgetJSON } from "../AbstractMediaManager";
2
+ import { MediaManagerWidgetData, MediaManagerItem, MediaManagerWidgetItem, MediaManagerWidgetJSON } from "../AbstractMediaManager";
3
3
  import { Adminizer } from "../../Adminizer";
4
4
  type PostParams = Record<string, string | number | boolean | object | string[] | number[] | null>;
5
+ export declare function normalizeMediaManagerWidgetData(value: PostParams[string] | undefined, prop: string): MediaManagerWidgetData[];
6
+ export declare function detachMediaManagerField(reqData: PostParams, rawReqData: PostParams, prop: string): void;
5
7
  /**
6
8
  * Create a random file name with prefix and type. If prefix is true, the file name will be prefixed with a random string.
7
9
  * @param filenameOrig
@@ -1,3 +1,32 @@
1
+ export function normalizeMediaManagerWidgetData(value, prop) {
2
+ if (value === undefined || value === null || value === "") {
3
+ return [];
4
+ }
5
+ if (typeof value === "string") {
6
+ let parsedValue;
7
+ try {
8
+ parsedValue = JSON.parse(value);
9
+ }
10
+ catch (error) {
11
+ throw new Error(`Error assign association-many mediamanager data for ${prop}, ${value}`);
12
+ }
13
+ if (parsedValue === null) {
14
+ return [];
15
+ }
16
+ if (!Array.isArray(parsedValue)) {
17
+ throw new Error(`Error assign association-many mediamanager data for ${prop}, ${value}`);
18
+ }
19
+ return parsedValue;
20
+ }
21
+ if (Array.isArray(value)) {
22
+ return value;
23
+ }
24
+ throw new Error(`Error assign association-many mediamanager data for ${prop}, ${JSON.stringify(value)}`);
25
+ }
26
+ export function detachMediaManagerField(reqData, rawReqData, prop) {
27
+ rawReqData[prop] = normalizeMediaManagerWidgetData(reqData[prop], prop);
28
+ delete reqData[prop];
29
+ }
1
30
  /**
2
31
  * Create a random file name with prefix and type. If prefix is true, the file name will be prefixed with a random string.
3
32
  * @param filenameOrig
@@ -23,7 +52,7 @@ export async function saveRelationsMediaManager(adminizer, fields, reqData, mode
23
52
  let fieldConfigConfig = fields[prop].config;
24
53
  let options = fieldConfigConfig.options;
25
54
  if (fieldConfigConfig.type === 'mediamanager') {
26
- let data = reqData[prop];
55
+ const data = normalizeMediaManagerWidgetData(reqData[prop], prop);
27
56
  let mediaManager = adminizer.mediaManagerHandler.get(options?.id ?? 'default');
28
57
  await mediaManager.setRelations(data, model, recordId, prop);
29
58
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "adminizer",
3
3
  "type": "module",
4
- "version": "4.5.0-build.194",
4
+ "version": "4.5.0-build.196",
5
5
  "main": "index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",
8
- "./lib/*": "./dist/lib/*.js",
8
+ "./lib/DataAccessor": "./dist/lib/DataAccessor.js",
9
9
  "./ui/*": "./dist/ui/components/ui/*.js"
10
10
  },
11
11
  "scripts": {