@@ -8,11 +8,9 @@ import LoadingIndicator from '../../../components/LoadingIndicator';
88import { useAllProjects } from '../../../hooks/projects.hooks' ;
99import ErrorPage from '../../ErrorPage' ;
1010import { add , sub } from 'date-fns' ;
11- import { useQuery } from '../../../hooks/utils.hooks' ;
1211import { useHistory } from 'react-router-dom' ;
1312import {
1413 applyChangesToWBSElement ,
15- buildGanttSearchParams ,
1614 constructCollectionsFromTeamPreviewAndProjects ,
1715 constructFinalizedChanges ,
1816 GanttChange ,
@@ -21,7 +19,8 @@ import {
2119 GanttTask ,
2220 isProjectPreview ,
2321 RequestEventChange ,
24- transformProjectToGanttTask
22+ transformProjectToGanttTask ,
23+ useGanttFilters
2524} from '../../../utils/gantt.utils' ;
2625import { routes } from '../../../utils/routes' ;
2726import { Box } from '@mui/material' ;
@@ -52,15 +51,9 @@ import { projectWbsPipe } from '../../../utils/pipes';
5251import { projectPreviewTransformer } from '../../../apis/transformers/projects.transformers' ;
5352
5453const ProjectGanttChartPage : FC = ( ) => {
55- const query = useQuery ( ) ;
5654 const history = useHistory ( ) ;
5755 const toast = useToast ( ) ;
5856
59- const ganttParams = localStorage . getItem ( 'ganttURL' ) ;
60- if ( ganttParams && history . location . search !== ganttParams ) {
61- history . push ( `${ history . location . pathname + ganttParams } ` ) ;
62- }
63-
6457 const { isLoading : projectsIsLoading , isError : projectsIsError , data : projects , error : projectsError } = useAllProjects ( ) ;
6558
6659 const {
@@ -87,20 +80,7 @@ const ProjectGanttChartPage: FC = () => {
8780 const [ editedProjects , setEditedProjects ] = useState < ProjectPreview [ ] > ( [ ] ) ;
8881
8982 /******************** Filters ***************************/
90- const showCars = query . getAll ( 'car' ) . map ( ( car ) => parseInt ( car ) ) ;
91-
92- const showTeamTypes = query . getAll ( 'teamType' ) ;
93-
94- const showTeams = query . getAll ( 'team' ) ;
95-
96- const showOnlyOverdue = query . get ( 'overdue' ) ? query . get ( 'overdue' ) === 'true' : false ;
97-
98- const [ ganttFilters , setGanttFilters ] = useState < GanttFilters > ( {
99- showCars,
100- showTeamTypes,
101- showTeams,
102- showOnlyOverdue
103- } ) ;
83+ const { filters, setFilters } = useGanttFilters ( 'project-gantt' ) ;
10484
10585 useEffect ( ( ) => {
10686 const requestRefresh = (
@@ -129,13 +109,16 @@ const ProjectGanttChartPage: FC = () => {
129109 projectPreviewTransformer
130110 )
131111 ) ;
132- history . push ( `${ history . location . pathname + buildGanttSearchParams ( ganttFilters ) } ` ) ;
133112 } ;
134113
135114 if ( projects && teams ) {
136- requestRefresh ( projects , teams , editedProjects , addedProjects , ganttFilters , searchText ) ;
115+ requestRefresh ( projects , teams , editedProjects , addedProjects , filters , searchText ) ;
137116 }
138- } , [ teams , projects , addedProjects , setAllProjects , setCollections , editedProjects , ganttFilters , searchText , history ] ) ;
117+ } , [ teams , projects , addedProjects , setAllProjects , setCollections , editedProjects , filters , searchText , history ] ) ;
118+
119+ const handleSetGanttFilters = ( newFilters : GanttFilters ) => {
120+ setFilters ( newFilters ) ;
121+ } ;
139122
140123 if (
141124 projectsIsLoading ||
@@ -155,34 +138,33 @@ const ProjectGanttChartPage: FC = () => {
155138
156139 const carFilterHandler = ( car : number ) => {
157140 return ( event : ChangeEvent < HTMLInputElement > ) => {
158- setGanttFilters (
141+ handleSetGanttFilters (
159142 event . target . checked
160- ? { ...ganttFilters , showCars : Array . from ( new Set ( [ ...ganttFilters . showCars , car ] ) ) }
161- : { ...ganttFilters , showCars : ganttFilters . showCars . filter ( ( c ) => c !== car ) }
143+ ? { ...filters , showCars : Array . from ( new Set ( [ ...filters . showCars , car ] ) ) }
144+ : { ...filters , showCars : filters . showCars . filter ( ( c ) => c !== car ) }
162145 ) ;
163146 } ;
164147 } ;
165148
166149 const teamTypeFilterHandler = ( teamType : TeamType ) => {
167150 return ( event : ChangeEvent < HTMLInputElement > ) => {
168- setGanttFilters (
151+ handleSetGanttFilters (
169152 event . target . checked
170153 ? {
171- ...ganttFilters ,
172- showTeamTypes : Array . from ( new Set ( [ ...ganttFilters . showTeamTypes , teamType . name ] ) )
154+ ...filters ,
155+ showTeamTypes : Array . from ( new Set ( [ ...filters . showTeamTypes , teamType . name ] ) )
173156 }
174- : { ...ganttFilters , showTeamTypes : ganttFilters . showTeamTypes . filter ( ( t ) => t !== teamType . name ) }
157+ : { ...filters , showTeamTypes : filters . showTeamTypes . filter ( ( t ) => t !== teamType . name ) }
175158 ) ;
176- history . push ( `${ history . location . pathname + buildGanttSearchParams ( ganttFilters ) } ` ) ;
177159 } ;
178160 } ;
179161
180162 const teamFilterHandler = ( team : TeamPreview ) => {
181163 return ( event : ChangeEvent < HTMLInputElement > ) => {
182- setGanttFilters (
164+ handleSetGanttFilters (
183165 event . target . checked
184- ? { ...ganttFilters , showTeams : Array . from ( new Set ( [ ...ganttFilters . showTeams , team . teamName ] ) ) }
185- : { ...ganttFilters , showTeams : ganttFilters . showTeams . filter ( ( t ) => t !== team . teamName ) }
166+ ? { ...filters , showTeams : Array . from ( new Set ( [ ...filters . showTeams , team . teamName ] ) ) }
167+ : { ...filters , showTeams : filters . showTeams . filter ( ( t ) => t !== team . teamName ) }
186168 ) ;
187169 } ;
188170 } ;
@@ -195,7 +177,7 @@ const ProjectGanttChartPage: FC = () => {
195177 return {
196178 filterLabel : teamType . name ,
197179 handler : teamTypeFilterHandler ( teamType ) ,
198- defaultChecked : ganttFilters . showTeamTypes . includes ( teamType . name )
180+ defaultChecked : filters . showTeamTypes . includes ( teamType . name )
199181 } ;
200182 } ) ;
201183
@@ -207,16 +189,16 @@ const ProjectGanttChartPage: FC = () => {
207189 return {
208190 filterLabel : team . teamName ,
209191 handler : teamFilterHandler ( team ) ,
210- defaultChecked : ganttFilters . showTeams . includes ( team . teamName )
192+ defaultChecked : filters . showTeams . includes ( team . teamName )
211193 } ;
212194 } ) ;
213195
214196 const overdueHandler = [
215197 {
216198 filterLabel : 'Overdue' ,
217199 handler : ( event : ChangeEvent < HTMLInputElement > ) =>
218- setGanttFilters ( { ...ganttFilters , showOnlyOverdue : event . target . checked } ) ,
219- defaultChecked : ganttFilters . showOnlyOverdue
200+ handleSetGanttFilters ( { ...filters , showOnlyOverdue : event . target . checked } ) ,
201+ defaultChecked : filters . showOnlyOverdue
220202 }
221203 ] ;
222204
@@ -229,7 +211,7 @@ const ProjectGanttChartPage: FC = () => {
229211 return {
230212 filterLabel : carNum === 0 ? 'None' : `Car ${ carNum } ` ,
231213 handler : carFilterHandler ( carNum ) ,
232- defaultChecked : ganttFilters . showCars . includes ( carNum )
214+ defaultChecked : filters . showCars . includes ( carNum )
233215 } ;
234216 } ) ;
235217
0 commit comments