Skip to content

Commit 23e2d9c

Browse files
authored
Merge pull request #118 from devforth/groupName
Group name
2 parents 4904c01 + a6e9db4 commit 23e2d9c

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

adminforth/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import AdminForthAuth from './auth.js';
32
import MongoConnector from './dataConnectors/mongo.js';
43
import PostgresConnector from './dataConnectors/postgres.js';
@@ -414,6 +413,15 @@ class AdminForth implements IAdminForth {
414413
return { error: err };
415414
}
416415

416+
for (const column of resource.columns) {
417+
const fieldName = column.name;
418+
if (fieldName in record) {
419+
if (!column.showIn?.create || column.backendOnly) {
420+
return { error: `Field "${fieldName}" cannot be modified as it is restricted from creation` };
421+
}
422+
}
423+
}
424+
417425
// execute hook if needed
418426
for (const hook of listify(resource.hooks?.create?.beforeSave)) {
419427
console.log('🪲 Hook beforeSave', hook);
@@ -490,6 +498,15 @@ class AdminForth implements IAdminForth {
490498
delete record[column.name];
491499
}
492500

501+
for (const column of resource.columns) {
502+
const fieldName = column.name;
503+
if (fieldName in record) {
504+
if (!column.showIn?.edit || column.editReadonly || column.backendOnly) {
505+
return { error: `Field "${fieldName}" cannot be modified as it is restricted from editing` };
506+
}
507+
}
508+
}
509+
493510
// execute hook if needed
494511
for (const hook of listify(resource.hooks?.edit?.beforeSave)) {
495512
const resp = await hook({

adminforth/modules/restApi.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
458458
}
459459
});
460460

461+
if (resource.options.fieldGroups) {
462+
resource.options.fieldGroups.forEach((group, i) => {
463+
if (group.groupName) {
464+
translateRoutines[`fieldGroup${i}`] = tr(group.groupName, `resource.${resource.resourceId}.fieldGroup`);
465+
}
466+
});
467+
}
468+
461469
const translated: Record<string, string> = {};
462470
await Promise.all(
463471
Object.entries(translateRoutines).map(async ([key, value]) => {
@@ -531,6 +539,10 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
531539
),
532540
options: {
533541
...resource.options,
542+
fieldGroups: resource.options.fieldGroups?.map((group, i) => ({
543+
...group,
544+
groupName: translated[`fieldGroup${i}`] || group.groupName,
545+
})),
534546
bulkActions: allowedBulkActions.map(
535547
(action, i) => ({
536548
...action,

dev-demo/resources/apartments.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default {
6363
{
6464
name: "id",
6565
label: "Identifier", // if you wish you can redefine label
66-
showIn: ["filter", "show", "list"], // show in filter and in show page
66+
showIn: {filter: true, show: true, list: true, create: false, edit: false}, // show in filter and in show page
6767
primaryKey: true,
6868
// @ts-ignore
6969
fillOnCreate: ({ initialRecord, adminUser }) =>
@@ -77,7 +77,7 @@ export default {
7777
name: "title",
7878
// type: AdminForthDataTypes.JSON,
7979
required: true,
80-
showIn: ["list", "create", "edit", "filter", "show"], // the default is full set
80+
showIn: {list: true, create: true, edit: true, filter: true, show: true}, // the default is full set
8181
maxLength: 255, // you can set max length for string fields
8282
minLength: 3, // you can set min length for string fields
8383
components: {
@@ -187,7 +187,7 @@ export default {
187187
},
188188
{
189189
name: "price",
190-
showIn: ["create", "edit", "filter", "show"],
190+
showIn: {create: true, edit: true, filter: true, show: true},
191191
allowMinMaxQuery: true, // use better experience for filtering e.g. date range, set it only if you have index on this column or if there will be low number of rows
192192
editingNote: "Price is in USD", // you can appear note on editing or creating page
193193
},
@@ -216,7 +216,7 @@ export default {
216216
{ value: 4, label: "4 rooms" },
217217
{ value: 5, label: "5 rooms" },
218218
],
219-
showIn: ["filter", "show", "create", "edit"],
219+
showIn: {filter: true, show: true, create: true, edit: true},
220220
},
221221
{
222222
name: "room_sizes",
@@ -230,15 +230,15 @@ export default {
230230
name: "description",
231231
sortable: false,
232232
type: AdminForthDataTypes.RICHTEXT,
233-
showIn: ["filter", "show", "edit", "create"],
233+
showIn: {filter: true, show: true, edit: true, create: true},
234234
},
235235
{
236236
name: "listed",
237237
required: true, // will be required on create/edit
238238
},
239239
{
240240
name: "user_id",
241-
showIn: ["filter", "show", "edit", "list", "create"],
241+
showIn: {filter: true, show: true, edit: true, list: true, create: true},
242242
foreignResource: {
243243
resourceId: "users",
244244
hooks: {

0 commit comments

Comments
 (0)