1+ import React from 'react' ;
2+ import { BtnDropdown , GenericController , LoadingScreen , ServerErrorIcon , ToolTip } from '@gpa-gemstone/react-interactive' ;
3+ import LocationDrawingsModal from './LocationDrawingsModal' ;
4+ import { OpenXDA } from '@gpa-gemstone/application-typings' ;
5+ import { CrossMark } from '@gpa-gemstone/gpa-symbols' ;
6+
7+ interface LocationDrawingsButtonProps {
8+ Locations : OpenXDA . Types . Location [ ] ;
9+ }
10+
11+ const LocationDrawingsButton : React . FC < LocationDrawingsButtonProps > = ( props ) => {
12+ const [ hover , setHover ] = React . useState < 'none' | 'drawings' > ( 'none' ) ;
13+ const [ pageState , setPageState ] = React . useState < "loading" | "error" | "idle" > ( "idle" ) ;
14+ const [ selectedLocation , setSelectedLocation ] = React . useState < OpenXDA . Types . Location > ( ) ;
15+ const [ multipleLocations , setMultipleLocations ] = React . useState < boolean > ( false ) ;
16+ const [ showDrawingsModal , setShowDrawingsModal ] = React . useState < boolean > ( false ) ;
17+ const [ locationsWithErrors , setLocationsWithErrors ] = React . useState < Map < OpenXDA . Types . Location , string [ ] > > ( new Map ( ) )
18+ const LocationDrawingController = new GenericController ( `${ homePath } api/LocationDrawing` , "Name" , true ) ;
19+
20+ const isValid = ( location , drawingData ) => {
21+ let e = [ ] ;
22+
23+ if ( location == undefined
24+ || ( location . Alias == ""
25+ && location . Description == ""
26+ && location . ID == 0
27+ && location . Latitude == null
28+ && location . LocationKey == ""
29+ && location . Longitude == null
30+ && location . Name == "" ) )
31+ e . push ( 'No locations have been set.' ) ;
32+ else if ( drawingData . TotalRecords == 0 )
33+ e . push ( 'No drawings associated with location.' ) ;
34+ return e ;
35+ }
36+
37+ React . useEffect ( ( ) => { // Generates the map of errors for each location
38+ if ( props . Locations . length > 1 ) setMultipleLocations ( true ) ;
39+ else setMultipleLocations ( false ) ; // TODO: check for undefined location isValid is not doing it
40+ for ( const location of props . Locations ) {
41+ if ( location ?. ID ) {
42+ setPageState ( 'loading' ) ;
43+ LocationDrawingController . PagedSearch ( [ ] , 'Name' , true , 1 , location . ID )
44+ . done ( ( result ) => {
45+ const errors = isValid ( location , result ) ;
46+ updateLocationErrors ( location , errors ) ;
47+ setPageState ( 'idle' )
48+ } )
49+ . fail ( ( ) => setPageState ( 'error' ) ) ;
50+ }
51+ }
52+ } , [ props . Locations ] ) ;
53+
54+ const handleAddLocationError = ( locMap : Map < OpenXDA . Types . Location , string [ ] > ) => {
55+ setLocationsWithErrors ( prev => {
56+ const newMap = new Map ( prev ) ;
57+ locMap . forEach ( ( errors , loc ) => {
58+ if ( newMap . has ( loc ) ) {
59+ const existingErrors = newMap . get ( loc ) ;
60+ newMap . set ( loc , Array . from ( new Set ( [ ...existingErrors , ...errors ] ) ) ) ;
61+ } else {
62+ newMap . set ( loc , errors ) ;
63+ }
64+ } ) ;
65+ return newMap ;
66+ } ) ;
67+ }
68+
69+ const handleRemoveLocationError = ( loc : OpenXDA . Types . Location ) => {
70+ setLocationsWithErrors ( prev => {
71+ const newMap = new Map ( prev ) ;
72+ newMap . delete ( loc ) ;
73+ return newMap ;
74+ } ) ;
75+ }
76+
77+ const updateLocationErrors = ( loc : OpenXDA . Types . Location , errors : string [ ] ) => {
78+ if ( errors . length > 0 && loc != undefined ) {
79+ const locationErrorsMap = new Map < OpenXDA . Types . Location , string [ ] > ( ) ;
80+ locationErrorsMap . set ( loc , errors ) ;
81+ handleAddLocationError ( locationErrorsMap ) ;
82+ } else {
83+ handleRemoveLocationError ( loc ) ;
84+ }
85+ }
86+
87+ const handleShowDrawingsModal = ( loc ) => {
88+ if ( loc == undefined ) return ;
89+ setSelectedLocation ( loc ) ;
90+ setShowDrawingsModal ( true ) ;
91+ } ;
92+
93+ return (
94+ < div >
95+ < LoadingScreen Show = { pageState == 'loading' } />
96+ < ServerErrorIcon Show = { pageState == 'error' } Size = { 40 } Label = { 'A Server Error Occurred. Please Reload the Application.' } />
97+ { ! multipleLocations
98+ ? < >
99+ < button
100+ className = { locationsWithErrors . size > 0 ? "btn btn-primary disabled" : "btn btn-primary" }
101+ onClick = { ( ) => locationsWithErrors . size > 0 ? null : setShowDrawingsModal ( true ) }
102+ data-tooltip = { "DrawingsModal" }
103+ onMouseEnter = { ( ) => setHover ( 'drawings' ) }
104+ onMouseLeave = { ( ) => setHover ( 'none' ) }
105+ > Open { props . Locations [ 0 ] ?. Name } Drawings
106+ </ button >
107+ < ToolTip
108+ Show = { locationsWithErrors . size > 0 && hover === 'drawings' }
109+ Theme = { 'dark' }
110+ Position = { 'top' }
111+ Target = { "DrawingsModal" }
112+ Zindex = { 9999 }
113+ > { locationsWithErrors . get ( props . Locations [ 0 ] ) ?. map ( ( e , i ) => < p key = { i } > { CrossMark } { e } </ p > ) }
114+ </ ToolTip >
115+ </ >
116+ : < BtnDropdown
117+ Label = { 'Open ' + props . Locations [ 0 ] ?. Name + ' Drawings' }
118+ Callback = { ( ) => handleShowDrawingsModal ( props . Locations [ 0 ] ) }
119+ TooltipContent = {
120+ < > { locationsWithErrors . get ( props . Locations [ 0 ] ) ?. map ( ( e , i ) => < p key = { i } > { CrossMark } { e } </ p > ) } </ >
121+ }
122+ ShowToolTip = { locationsWithErrors . has ( props . Locations [ 0 ] ) }
123+ Disabled = { locationsWithErrors . has ( props . Locations [ 0 ] ) }
124+ BtnClass = { 'btn-primary' }
125+ Options = { props . Locations . slice ( 1 ) . map ( ( loc , i ) => ( {
126+ Label : 'Open ' + loc ?. Name + ' Drawings' ,
127+ Callback : ( ) => handleShowDrawingsModal ( loc ) ,
128+ Disabled : locationsWithErrors . has ( loc ) ,
129+ ToolTipContent : < > {
130+ locationsWithErrors . get ( loc ) ?. map ( ( e , i ) => < p key = { i } > { CrossMark } { e } </ p > )
131+ } </ > ,
132+ ShowToolTip : locationsWithErrors . has ( loc ) ,
133+ ToolTipLocation : "left" ,
134+ Key : i
135+ } ) ) }
136+ />
137+ }
138+ < LocationDrawingsModal
139+ LocationID = { selectedLocation ?. ID }
140+ Show = { showDrawingsModal }
141+ SetShow = { setShowDrawingsModal }
142+ />
143+ </ div >
144+ ) ;
145+ } ;
146+
147+ export default LocationDrawingsButton ;
0 commit comments