@@ -4,14 +4,21 @@ import Form from '../../components/form';
44import { IField } from '../../type' ;
55import { useTranslation } from 'react-i18next' ;
66import { Grid , Typography } from '@mui/material' ;
7- import { useSelector } from '../../../../store' ;
7+ import { useDispatch , useSelector } from '../../../../store' ;
88import { UserMiniDTO } from '../../../../models/user' ;
99import {
1010 FilterFieldType ,
1111 filterSingleField ,
1212 getDateValue ,
1313 getLabelAndValue
1414} from '../../../../utils/filter' ;
15+ import { useEffect } from 'react' ;
16+ import { getAssetsMini } from '../../../../slices/asset' ;
17+ import { getCustomersMini } from '../../../../slices/customer' ;
18+ import { getTeamsMini } from '../../../../slices/team' ;
19+ import { getLocationsMini } from '../../../../slices/location' ;
20+ import { getCategories } from '../../../../slices/category' ;
21+ import { getUsersMini } from '../../../../slices/user' ;
1522
1623interface OwnProps {
1724 onFilterChange : ( filterFields : FilterField [ ] ) => void ;
@@ -27,6 +34,7 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
2734 const { usersMini } = useSelector ( ( state ) => state . users ) ;
2835 const { assetsMini } = useSelector ( ( state ) => state . assets ) ;
2936 const { teamsMini } = useSelector ( ( state ) => state . teams ) ;
37+ const dispatch = useDispatch ( ) ;
3038
3139 const filtersConfig : {
3240 accessor : string ;
@@ -43,7 +51,7 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
4351 { accessor : 'completedBy' , fieldName : 'completedBy' , type : 'array' } ,
4452 {
4553 accessor : 'customers' ,
46- fieldName : 'customer ' ,
54+ fieldName : 'customers ' ,
4755 operator : 'inm' ,
4856 type : 'array'
4957 } ,
@@ -181,12 +189,12 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
181189 break ;
182190 }
183191 } ;
184- const getValuesFromfilterFields = ( ) : {
192+ const getValuesFromFilterFields = ( ) : {
185193 [ key : string ] :
186194 | { label : string ; value : string }
187195 | { label : string ; value : number } [ ]
188196 | boolean
189- | [ string , string ] ;
197+ | [ Date | null , Date | null ] ;
190198 } => {
191199 const typeValue = filterFields . find (
192200 ( filterField ) => filterField . field === 'parentPreventiveMaintenance'
@@ -239,7 +247,7 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
239247 customers : getLabelAndValue (
240248 filterFields ,
241249 customersMini ,
242- 'customer ' ,
250+ 'customers ' ,
243251 'name'
244252 ) ,
245253 createdBy : getLabelAndValue (
@@ -255,6 +263,27 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
255263 } ;
256264 } ;
257265 const shape = { } ;
266+
267+ const USER_FIELDS = [ 'primaryUser' , 'completedBy' , 'createdBy' , 'assignedTo' ] ;
268+
269+ useEffect ( ( ) => {
270+ const fieldsInUse = new Set ( filterFields . map ( ( f ) => f . field ) ) ;
271+
272+ if ( fieldsInUse . has ( 'asset' ) ) dispatch ( getAssetsMini ( ) ) ;
273+ if ( fieldsInUse . has ( 'customers' ) && ! customersMini . length )
274+ dispatch ( getCustomersMini ( ) ) ;
275+ if ( fieldsInUse . has ( 'team' ) && ! teamsMini . length ) dispatch ( getTeamsMini ( ) ) ;
276+ if ( fieldsInUse . has ( 'location' ) && ! locationsMini . length )
277+ dispatch ( getLocationsMini ( ) ) ;
278+ if (
279+ fieldsInUse . has ( 'category' ) &&
280+ ! categories [ 'work-order-categories' ] ?. length
281+ )
282+ dispatch ( getCategories ( 'work-order-categories' ) ) ;
283+ if ( USER_FIELDS . some ( ( f ) => fieldsInUse . has ( f ) ) && ! usersMini . length )
284+ dispatch ( getUsersMini ( ) ) ;
285+ } , [ filterFields . length ] ) ;
286+
258287 return (
259288 < Grid
260289 container
@@ -268,10 +297,12 @@ function MoreFilters({ filterFields, onFilterChange, onClose }: OwnProps) {
268297 </ Grid >
269298 < Grid item xs = { 12 } >
270299 < Form
300+ key = { `${ assetsMini . length } -${ teamsMini . length } -${ customersMini . length } -${ locationsMini . length } -${ usersMini . length } -${ categories [ 'work-order-categories' ] ?. length } ` }
271301 fields = { fields }
272302 validation = { Yup . object ( ) . shape ( shape ) }
273303 submitText = { t ( 'save' ) }
274- values = { getValuesFromfilterFields ( ) }
304+ values = { getValuesFromFilterFields ( ) }
305+ enableReinitialize
275306 onChange = { ( { field, e } ) => { } }
276307 onSubmit = { async ( values ) => {
277308 let newFilters = [ ...filterFields ] ;
0 commit comments