Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F995221
shared-crud-form-fields.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
shared-crud-form-fields.ts
View Options
import
{
toPascalCase
}
from
"js-convert-case"
;
import
{
extractCollectionClassname
}
from
"../../generate-collections.js"
;
import
extract_fields_from_collection
,
{
ExtractedFieldInfo
,
}
from
"../../utils/extract-fields-from-collection.js"
;
import
_locreq
from
"locreq"
;
import
{
curryImportPath
}
from
"../../utils/import-path.js"
;
import
{
formatWithPrettier
}
from
"../../utils/prettier.js"
;
import
{
default_handler
}
from
"./handlers/default.js"
;
import
{
enum_handler
}
from
"./handlers/enum.js"
;
import
{
image_handler
}
from
"./handlers/image.js"
;
import
{
file_handler
}
from
"./handlers/file.js"
;
import
{
int_handler
}
from
"./handlers/int.js"
;
import
{
boolean_handler
}
from
"./handlers/boolean.js"
;
import
{
jdd_handler
}
from
"./handlers/jdd.js"
;
import
{
date_handler
}
from
"./handlers/date.js"
;
import
{
deep_reverse_single_reference_handler
}
from
"./handlers/deep-reverse-single-reference.js"
;
import
{
structured_array_handler
}
from
"./handlers/structured-array.js"
;
import
{
single_reference_handler
}
from
"./handlers/single-reference.js"
;
const
target_locreq
=
_locreq
(
process
.
cwd
());
export
type
FieldHandlerResult
=
{
imports
:
Partial
<
{
shared
:
string
;
edit
:
string
}
>
;
hide_field
:
boolean
;
field
:
string
;
hide_control
:
boolean
;
controls
:
string
;
is_required
:
boolean
;
is_required_expr
:
string
;
fields_var
:
string
;
top_level_shared
:
string
;
name
:
string
;
hide_initial_value_edit
:
boolean
;
initial_value_edit
:
|
Partial
<
{
main_value
:
string
;
parsed_value
:
string
;
fallback_value
:
string
;
}
>
|
string
;
hide_initial_value_create
:
boolean
;
initial_value_create
:
string
;
pre_edit
:
string
;
post_edit
:
string
;
pre_create
:
string
;
post_create
:
string
;
sealious_value
:
(
result
:
Exclude
<
FieldHandlerResult
,
"sealious_value"
>
)
=>
string
;
hide_sealious_value
:
boolean
;
fallback_value_sealious
:
string
;
main_value_sealious
:
string
;
};
export
type
FieldHandler
<
Defaults
extends
Partial
<
FieldHandlerResult
>
,
Result
extends
Partial
<
FieldHandlerResult
>
>
=
(
collection_name
:
string
,
field
:
ExtractedFieldInfo
,
importPath
:
(
path
:
string
)
=>
string
,
defaults
:
Defaults
)
=>
Promise
<
Result
>
;
export
const
field_handlers
:
Record
<
Exclude
<
string
,
"default"
>
,
FieldHandler
<
FieldHandlerResult
,
Partial
<
FieldHandlerResult
>>
|
undefined
>
&
{
default
:
FieldHandler
<
Record
<
string
,
never
>
,
FieldHandlerResult
>
;
}
=
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
default
:
default_handler
as
any
,
enum
:
enum_handler
,
image
:
image_handler
,
file
:
file_handler
,
int
:
int_handler
,
boolean
:
boolean_handler
,
jdd
:
jdd_handler
,
date
:
date_handler
,
"deep-reverse-single-reference"
:
deep_reverse_single_reference_handler
,
"structured-array"
:
structured_array_handler
,
"single-reference"
:
single_reference_handler
,
};
export
async
function
getFieldHandler
(
collection_name
:
string
,
field
:
ExtractedFieldInfo
,
importPath
:
(
path
:
string
)
=>
string
)
{
const
defaults
=
await
field_handlers
.
default
(
collection_name
,
field
,
importPath
,
{}
);
const
result
=
{
...
defaults
,
...((
await
field_handlers
[
field
.
type
]
?
.(
collection_name
,
field
,
importPath
,
defaults
))
||
{}),
};
const
result_initial_value_edit
=
result
.
initial_value_edit
;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
result
.
initial_value_edit
=
typeof
result_initial_value_edit
==
"string"
?
result_initial_value_edit
:
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(
defaults
.
initial_value_edit
as
any
),
...
result_initial_value_edit
,
};
return
result
;
}
export
async
function
sharedCrudFormFields
(
collection_name
:
string
,
newfilefullpath
:
string
)
{
const
[
uppercase_collection
,
collection_fields
]
=
await
Promise
.
all
([
extractCollectionClassname
(
target_locreq
.
resolve
(
"src/back/collections/"
+
collection_name
+
".ts"
)
),
extract_fields_from_collection
(
collection_name
),
]);
const
importPath
=
curryImportPath
(
newfilefullpath
);
const
field_handler_results
=
await
Promise
.
all
(
collection_fields
.
map
(
async
(
field
)
=>
{
return
getFieldHandler
(
collection_name
,
field
,
importPath
);
})
);
const
result
=
`import { Controls, Fields } from "@sealcode/sealgen";
import {
${
uppercase_collection
}
} from "
${
importPath
(
"src/back/collections/collections.js"
)
}
";
${
Array
.
from
(
new
Set
(
field_handler_results
.
filter
(
({
hide_field
,
hide_control
}
) => !hide_field && !hide_control
)
.map(({ imports }) => imports.shared || "")
)
).join("\n")}
export const
${
toPascalCase
(
collection_name
)
}
FormFields = <const>
{
${
field_handler_results
.
filter
(({
hide_field
}
) => !hide_field)
.map(({ field }) => field)
.filter((e) => e != "")
.join(",\n")}
};
export const
${
toPascalCase
(
collection_name
)
}
FormControls = [
${
field_handler_results
.
filter
(({
hide_control
}
) => !hide_control)
.map(({ controls }) => controls)
.filter((e) => e != "")
.join(",\n")}
];
${
Array
.
from
(
new
Set
(
field_handler_results
.
map
(({
top_level_shared
}
) => top_level_shared)
)
).join("\n")}
`
;
return
formatWithPrettier
(
result
);
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Mon, Dec 23, 00:54 (3 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556721
Default Alt Text
shared-crud-form-fields.ts (5 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment