@@ -21,126 +21,97 @@ export const isThisFilterAllowed = (target: string, allowedList: string[]) => {
2121 return allowedList . includes ( target )
2222}
2323
24- export const whatFilterChoiceToChoose = ( item : FieldShapeClient ) : FilterChoiceComponent => {
25- switch ( item . type ) {
26- case 'richText' :
27- case 'text' :
28- case 'password' :
29- case 'email' :
30- case 'number' :
31- case 'comboboxNumber' :
32- case 'comboboxText' :
33- case 'media' :
34- case 'create' :
35- return null
36-
37- case 'time' :
38- case 'date' :
39- return 'datetime' // as range
40-
41- case 'checkbox' :
42- case 'switch' :
43- return 'toggle' // as toggle ?
44-
45- case 'selectNumber' :
46- case 'selectText' :
47- case 'connect' :
48- case 'connectOrCreate' : {
49- if ( ! item . options || ( Array . isArray ( item . options ) && item . options . length === 0 ) ) {
50- throw new Error ( 'Missing options for select/connect field' )
51- }
52- return 'singleselect'
53- }
54-
55- default :
56- return null
24+ export function getLabelFromFieldShape ( fieldShape : FieldShapeClient , userFriendlyText : boolean ) {
25+ const isRelational = isRelationFieldShapeClient ( fieldShape )
26+ let columnOrRelationName = ''
27+ if ( isRelational ) {
28+ columnOrRelationName = fieldShape . $client . relation . name
29+ } else {
30+ columnOrRelationName = fieldShape . $client . column . name
5731 }
32+ if ( userFriendlyText ) {
33+ return `${ fieldShape . label || fieldShape . $client . fieldName } (${ columnOrRelationName } )`
34+ }
35+ return `${ columnOrRelationName } `
5836}
5937
60- export const isThisTypeFilterable = ( item : FieldShapeClient ) => {
61- switch ( item . type ) {
62- case 'richText' :
63- case 'text' :
64- case 'password' :
65- case 'email' :
66- case 'number' :
67- case 'comboboxNumber' :
68- case 'comboboxText' :
69- case 'media' :
70- case 'create' :
71- return false
38+ export function mapFieldShapeClient ( fieldShape : FieldShapeClient ) : {
39+ filterChoice : FilterChoiceComponent
40+ optionsFetchPathName : string
41+ } {
42+ switch ( fieldShape . type ) {
43+ // case 'richText':
44+ // case 'text':
45+ // case 'password':
46+ // case 'email':
47+ // case 'number':
48+ // case 'comboboxNumber':
49+ // case 'comboboxText':
50+ // case 'media':
51+ // case 'create':
7252
7353 case 'time' :
7454 case 'date' :
75- return true
55+ return {
56+ filterChoice : 'datetime' ,
57+ optionsFetchPathName : '' ,
58+ }
7659
7760 case 'checkbox' :
7861 case 'switch' :
79- return false // not ready
62+ // this component is not ready yet
63+ return {
64+ filterChoice : 'toggle' ,
65+ optionsFetchPathName : '' ,
66+ }
8067
8168 case 'selectNumber' :
8269 case 'selectText' :
8370 case 'connect' :
8471 case 'connectOrCreate' : {
85- if ( ! item . options ) throw new Error ( 'Missing optionsFetchPath' )
86- return true
72+ if (
73+ ! fieldShape . options ||
74+ ( Array . isArray ( fieldShape . options ) && fieldShape . options . length === 0 )
75+ ) {
76+ // `optionsFetchPathName` will not be completed without `options`, won't be able to fetch for choices
77+ // Act as if it is non-filterable
78+ return {
79+ filterChoice : null ,
80+ optionsFetchPathName : '' ,
81+ }
82+ }
83+ return {
84+ filterChoice : 'singleselect' ,
85+ optionsFetchPathName : fieldShape . options ,
86+ }
8787 }
8888
8989 default :
90- throw new Error ( `Unsupported field type: ${ JSON . stringify ( item ) } ` )
90+ return {
91+ filterChoice : null ,
92+ optionsFetchPathName : '' ,
93+ }
9194 }
9295}
9396
94- export const optionsFetchPathName = ( item : FieldShapeClient ) => {
95- switch ( item . type ) {
96- case 'richText' :
97- return undefined
98- case 'text' :
99- return undefined
100- case 'password' :
101- return undefined
102- case 'email' :
103- return undefined
104- case 'number' :
105- return undefined
106- case 'time' :
107- return undefined
108- case 'date' :
109- return undefined
110- case 'checkbox' :
111- return undefined
112- case 'switch' :
113- return undefined
97+ export function selectFilterChoiceWithFieldShape (
98+ fieldShape : FieldShapeClient
99+ ) : FilterChoiceComponent {
100+ return mapFieldShapeClient ( fieldShape ) . filterChoice
101+ }
114102
115- case 'selectNumber' :
116- case 'selectText' : {
117- if ( ! item . options ) throw new Error ( 'Missing optionsFetchPath' )
118- return item . options
119- }
120- case 'comboboxNumber' :
121- case 'comboboxText' : {
122- return undefined
123- }
124- case 'media' : {
125- return undefined
126- }
127- case 'create' : {
128- return undefined
129- }
130- case 'connect' :
131- case 'connectOrCreate' : {
132- if ( ! item . options ) throw new Error ( 'Missing optionsFetchPath' )
133- return item . options
134- }
135- default :
136- throw new Error ( `Unsupported field type: ${ JSON . stringify ( item ) } ` )
137- }
103+ export function isThisFieldShapeFilterable ( fieldShape : FieldShapeClient ) : boolean {
104+ return ! ! mapFieldShapeClient ( fieldShape ) . filterChoice
105+ }
106+
107+ export function getOptionsFetchPathNameWithFieldShape ( fieldShape : FieldShapeClient ) : string {
108+ return mapFieldShapeClient ( fieldShape ) . optionsFetchPathName
138109}
139110
140111export const transformFilterToPrismaString = ( filters : MinimalFilter [ ] ) => {
141112 // In case false data gets in, filter it out
142113 const filteredFilters = filters . filter ( ( f ) => {
143- const targetType = whatFilterChoiceToChoose ( f . fieldShape )
114+ const targetType = selectFilterChoiceWithFieldShape ( f . fieldShape )
144115 switch ( targetType ) {
145116 case 'datetime' :
146117 return ! ! f . value . filterValue && ! ! f . value . endFilterValue
@@ -173,7 +144,7 @@ export const transformFilterToPrismaString = (filters: MinimalFilter[]) => {
173144 }
174145
175146 const selfPrismaObjectGenerator = ( sItem : MinimalFilter ) => {
176- const targetType = whatFilterChoiceToChoose ( sItem . fieldShape )
147+ const targetType = selectFilterChoiceWithFieldShape ( sItem . fieldShape )
177148 switch ( targetType ) {
178149 case 'datetime' :
179150 return { gte : sItem . value . filterValue , lte : sItem . value . endFilterValue }
0 commit comments