@@ -38,11 +38,20 @@ type FilterTransformReturnType<T> = Record<
38
38
39
39
/**
40
40
* Hook resolving the base fields for lists.
41
+ * s
42
+ * @param [destructionList] - The destruction list to return fields for.
43
+ * @param [review] - The destruction list to return fields for.
44
+ * @param [extraFields] - Additional fields to add.
45
+ * @param [restrictFilterChoices="list"] - Allows restricting choices in filters
46
+ * to either:
47
+ * - The destruction list or review (`list`).
48
+ * - To zaken not already assigned to a destruction list (`unassigned`).
41
49
*/
42
50
export function useFields < T extends Zaak = Zaak > (
43
51
destructionList ?: DestructionList ,
44
52
review ?: Review ,
45
53
extraFields ?: TypedField < T > [ ] ,
54
+ restrictFilterChoices : "list" | "unassigned" | false = "list" ,
46
55
) : [
47
56
TypedField < T > [ ] ,
48
57
( fields : TypedField < T > [ ] ) => void ,
@@ -61,11 +70,27 @@ export function useFields<T extends Zaak = Zaak>(
61
70
} , [ ] ) ;
62
71
const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
63
72
const selectielijstKlasseChoices = useSelectielijstKlasseChoices ( ) ;
64
- const zaaktypeChoices = useZaaktypeChoices (
65
- destructionList ,
66
- review ,
67
- searchParams ,
68
- ) ;
73
+
74
+ // Fetch the zaaktype choices, if `restrictFilterChoices==="list"`: only zaaktype
75
+ // choices of zaken belonging to either the destruction list or review are loaded.
76
+ // If `restrictFilterChoices==="unassigned"`: only zaaktype choices belonging to
77
+ // zaken not assigned to any destruction list are loaded. If `restrictFilterChoices==="false"`:
78
+ // all zaaktype choices are loaded.
79
+ const zaaktypeParams = new URLSearchParams ( searchParams ) ;
80
+ zaaktypeParams . delete ( "zaaktype" ) ;
81
+
82
+ if ( restrictFilterChoices === "list" ) {
83
+ if ( destructionList ) {
84
+ zaaktypeParams . set ( "inDestructionList" , destructionList . uuid ) ;
85
+ }
86
+ if ( review ?. pk ) {
87
+ zaaktypeParams . set ( "inReview" , review . pk . toString ( ) ) ;
88
+ }
89
+ }
90
+ if ( restrictFilterChoices === "unassigned" ) {
91
+ zaaktypeParams . set ( "not_in_destruction_list" , "true" ) ;
92
+ }
93
+ const zaaktypeChoices = useZaaktypeChoices ( zaaktypeParams , false , null ) ;
69
94
70
95
// The raw, unfiltered configuration of the available base fields.
71
96
// Both filterLookup AND filterLookups will be used for clearing filters.
@@ -82,11 +107,26 @@ export function useFields<T extends Zaak = Zaak>(
82
107
name : "zaaktype" ,
83
108
filterLookup : "zaaktype" ,
84
109
filterValue : searchParams . get ( "zaaktype" ) || "" ,
85
- valueTransform : ( v : ExpandZaak ) =>
86
- zaaktypeChoices . find (
87
- ( c ) => c . value === v . _expand ?. zaaktype ?. identificatie ,
88
- ) ?. label || < Placeholder /> ,
89
- options : zaaktypeChoices ,
110
+ valueTransform : ( zaak ) => {
111
+ const expandZaak = zaak as T & {
112
+ _expand : { zaaktype : { identificatie : string } } ;
113
+ } ;
114
+ const zaaktype = expandZaak . _expand . zaaktype . identificatie ;
115
+
116
+ // Zaaktype choices not yet loaded.
117
+ if ( zaaktypeChoices === null ) {
118
+ return < Placeholder /> ;
119
+ }
120
+
121
+ // Find label by zaaktype choice.
122
+ const zaaktypeChoice = zaaktypeChoices . find (
123
+ ( { value } ) => value === zaaktype ,
124
+ ) ;
125
+
126
+ // Return label, or null.
127
+ return zaaktypeChoice ?. label || zaaktype ;
128
+ } ,
129
+ options : zaaktypeChoices || [ ] ,
90
130
type : "string" ,
91
131
width : "150px" ,
92
132
} ,
0 commit comments