1- import { useState , useEffect } from "react" ;
1+ import { useState , useMemo } from "react" ;
22import { useNavigate } from "@tanstack/react-router" ;
33import { BCDesignTokens } from "epic.theme" ;
4- import { ConditionModel } from "@/models/Condition" ;
5- import { Box , Button , FormControlLabel , Grid , styled , Stack , Switch , Typography } from "@mui/material" ;
4+ import { CONDITION_STATUS , ConditionModel , ConditionStatus } from "@/models/Condition" ;
5+ import { DocumentStatus } from "@/models/Document" ;
6+ import { Box , Button , FormControlLabel , Grid , Stack , Switch , Typography , styled } from "@mui/material" ;
7+ import LayersOutlinedIcon from '@mui/icons-material/LayersOutlined' ;
8+ import FileDownloadOutlinedIcon from '@mui/icons-material/FileDownloadOutlined' ;
69import { ContentBoxSkeleton } from "../Shared/ContentBox/ContentBoxSkeleton" ;
710import { ContentBox } from "../Shared/ContentBox" ;
811import ConditionTable from "../Conditions/ConditionsTable" ;
9- import { DocumentStatus } from "@/models/Document" ;
1012import DocumentStatusChip from "../Projects/DocumentStatusChip" ;
11- import LayersOutlinedIcon from '@mui/icons-material/LayersOutlined' ;
12- import FileDownloadOutlinedIcon from '@mui/icons-material/FileDownloadOutlined' ;
1313import ConsolidatedConditionFilters from "@/components/Filters/ConsolidatedConditionFilters" ;
1414import { useConditionFilters } from "@/components/Filters/conditionFilterStore" ;
15- import { CONDITION_STATUS , ConditionStatus } from "@/models/Condition" ;
1615import { useExportConsolidatedConditionsPDF } from "@/hooks/api/useConsolidatedConditions" ;
1716
1817export const CardInnerBox = styled ( Box ) ( {
@@ -42,40 +41,58 @@ export const ConsolidatedConditions = ({
4241 consolidationLevel
4342} : ConditionsParam ) => {
4443 const navigate = useNavigate ( ) ;
45- const [ noConditions , setNoConditions ] = useState ( conditions ?. length === 0 ) ;
46- const [ allApproved , setAllApproved ] = useState ( false ) ;
47- const [ hasAmendments , setHasAmendments ] = useState ( false ) ;
48- const [ isLoading , setIsLoading ] = useState ( true ) ;
4944 const [ isToggled , setIsToggled ] = useState ( true ) ;
5045
46+ const noConditions = useMemo ( ( ) => {
47+ if ( ! conditions || conditions . length === 0 ) return true ;
48+ if ( conditions . length === 1 ) {
49+ return conditions . some (
50+ ( c ) => ! c . condition_name || ! c . condition_number || c . is_approved === null
51+ ) ;
52+ }
53+ return false ;
54+ } , [ conditions ] ) ;
55+
56+ const allApproved = useMemo (
57+ ( ) => conditions ?. every ( ( c ) => c . is_approved === true ) ?? false ,
58+ [ conditions ]
59+ ) ;
60+
61+ const hasAmendments = useMemo (
62+ ( ) => conditions ?. some ( ( c ) => c . amendment_names != null ) ?? false ,
63+ [ conditions ]
64+ ) ;
65+
5166 const { filters } = useConditionFilters ( ) ;
5267 const { mutate : exportPDF , isPending : isExporting } = useExportConsolidatedConditionsPDF ( projectName ) ;
5368
5469 const handleExportPDF = ( ) => exportPDF ( projectId ) ;
5570
56- const filteredConditions = conditions ?. filter ( ( condition ) => {
57- const matchesSearch = filters . search_text
58- ? condition . condition_name ?. toLowerCase ( ) . includes ( filters . search_text . toLowerCase ( ) ) ?? false
59- : true ;
71+ const filteredConditions = useMemo ( ( ) =>
72+ conditions ?. filter ( ( condition ) => {
73+ const matchesSearch = filters . search_text
74+ ? condition . condition_name ?. toLowerCase ( ) . includes ( filters . search_text . toLowerCase ( ) ) ?? false
75+ : true ;
6076
61- const matchesSource = filters . source_document
62- ? condition . source_document ?. toLowerCase ( ) . includes ( filters . source_document . toLowerCase ( ) ) ?? false
63- : true ;
77+ const matchesSource = filters . source_document
78+ ? condition . source_document ?. toLowerCase ( ) . includes ( filters . source_document . toLowerCase ( ) ) ?? false
79+ : true ;
6480
65- const matchesAmendment = filters . amendment_names
66- ? condition . amendment_names ?. toLowerCase ( ) . includes ( filters . amendment_names . toLowerCase ( ) ) ?? false
67- : true ;
81+ const matchesAmendment = filters . amendment_names
82+ ? condition . amendment_names ?. toLowerCase ( ) . includes ( filters . amendment_names . toLowerCase ( ) ) ?? false
83+ : true ;
6884
69- const conditionStatus : ConditionStatus = condition . is_approved
70- ? CONDITION_STATUS . true . value
71- : CONDITION_STATUS . false . value ;
85+ const conditionStatus : ConditionStatus = condition . is_approved
86+ ? CONDITION_STATUS . true . value
87+ : CONDITION_STATUS . false . value ;
7288
73- const matchesStatus = filters . status && filters . status . length > 0
74- ? filters . status . includes ( conditionStatus )
75- : true ;
89+ const matchesStatus = filters . status ? .length > 0
90+ ? filters . status . includes ( conditionStatus )
91+ : true ;
7692
77- return matchesSearch && matchesSource && matchesAmendment && matchesStatus ;
78- } ) ;
93+ return matchesSearch && matchesSource && matchesAmendment && matchesStatus ;
94+ } ) ,
95+ [ conditions , filters ] ) ;
7996
8097 const handleToggle = ( event : React . ChangeEvent < HTMLInputElement > ) => {
8198 const checked = event . target . checked ;
@@ -87,31 +104,6 @@ export const ConsolidatedConditions = ({
87104 }
88105 } ;
89106
90- useEffect ( ( ) => {
91- // Check if all conditions have status as true
92- if ( conditions && conditions . length > 0 ) {
93- const checkIfAllApproved = conditions . every ( ( condition ) => condition . is_approved === true ) ;
94- const conditionHasAmendments = conditions . some ( condition => condition . amendment_names != null ) ;
95-
96- const invalidConditions =
97- conditions . length === 1 &&
98- conditions . some (
99- ( condition ) =>
100- ! condition . condition_name ||
101- ! condition . condition_number ||
102- condition . is_approved === null
103- ) ;
104- setNoConditions ( invalidConditions ) ;
105- setAllApproved ( checkIfAllApproved ) ;
106- setHasAmendments ( conditionHasAmendments ) ;
107- }
108- setIsLoading ( false ) ;
109- } , [ conditions ] ) ;
110-
111- if ( isLoading ) {
112- return < div > Loading...</ div > ;
113- }
114-
115107 return (
116108 < Stack spacing = { 2 } direction = { "column" } sx = { { width : '100%' } } >
117109 < ContentBox
0 commit comments