@@ -18,23 +18,31 @@ import { usePicklistSampleSelections } from './usePicklistSampleSelections';
1818import { isLoading } from '../../../public/LoadingState' ;
1919import { LoadingSpinner } from '../base/LoadingSpinner' ;
2020
21- export interface SharedProps {
21+ export interface PicklistEditModalProps {
2222 metricFeatureArea ?: string ;
2323 onCancel : ( ) => void ;
2424 onFinish : ( picklist : Picklist ) => void ;
2525 picklist ?: Picklist ;
26+ // If sampleFieldKey is present the modal queries the rowIds in order to fetch sampleIds via sampleFieldKey
27+ sampleFieldKey ?: string ;
28+ schemaQuery ?: SchemaQuery ;
29+ selectedRowIds ?: number [ ] | string [ ] ;
2630 showNotification ?: boolean ;
2731}
2832
29- export interface ModalProps extends SharedProps {
30- sampleIds : number [ ] ;
31- }
32-
33- export const PicklistEditModalDisplay : FC < ModalProps > = memo ( props => {
34- const { onCancel, onFinish, sampleIds, picklist, showNotification, metricFeatureArea } = props ;
33+ export const PicklistEditModal : FC < PicklistEditModalProps > = memo ( props => {
34+ const {
35+ onCancel,
36+ onFinish,
37+ picklist,
38+ sampleFieldKey,
39+ schemaQuery,
40+ selectedRowIds,
41+ showNotification,
42+ metricFeatureArea,
43+ } = props ;
3544 const [ name , setName ] = useState < string > ( picklist ?. name ?? '' ) ;
3645 const onNameChange = useCallback ( ( evt : ChangeEvent < HTMLInputElement > ) => setName ( evt . target . value ) , [ ] ) ;
37- const sampleCount = sampleIds ?. length ;
3846
3947 const [ description , setDescription ] = useState < string > ( picklist ?. Description ?? '' ) ;
4048 const onDescriptionChange = useCallback (
@@ -47,7 +55,7 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
4755 [ ]
4856 ) ;
4957 const [ submitting , setSubmitting ] = useState < boolean > ( false ) ;
50- const [ error , setError ] = useState < string > ( undefined ) ;
58+ const [ saveError , setSaveError ] = useState < string > ( undefined ) ;
5159 const { api } = useAppContext ( ) ;
5260 const { createNotification } = useNotificationsContext ( ) ;
5361
@@ -60,6 +68,14 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
6068 } ;
6169 } , [ picklist ] ) ;
6270
71+ const {
72+ error,
73+ loadingState,
74+ value : sampleIds ,
75+ } = usePicklistSampleSelections ( selectedRowIds , sampleFieldKey , schemaQuery ) ;
76+ const loading = isLoading ( loadingState ) ;
77+ const sampleCount = sampleIds ?. length ;
78+
6379 const onSavePicklist = useCallback ( async ( ) : Promise < void > => {
6480 setSubmitting ( true ) ;
6581 try {
@@ -96,7 +112,7 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
96112
97113 onFinish ( updatedList ) ;
98114 } catch ( e ) {
99- setError ( resolveErrorMessage ( e ) ) ;
115+ setSaveError ( resolveErrorMessage ( e ) ) ;
100116 setSubmitting ( false ) ;
101117 }
102118 } , [
@@ -116,7 +132,11 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
116132 ] ) ;
117133
118134 let title : string ;
119- if ( isUpdate ) {
135+ if ( loading ) {
136+ title = 'Loading Selection Data...' ;
137+ } else if ( error ) {
138+ title = 'Error Loading Selection Data' ;
139+ } else if ( isUpdate ) {
120140 title = 'Update Picklist Data' ;
121141 } else {
122142 if ( ! sampleCount ) {
@@ -128,6 +148,8 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
128148 }
129149 }
130150
151+ const showForm = ! error && ! loading ;
152+
131153 return (
132154 < Modal
133155 canConfirm = { ! ! name }
@@ -138,81 +160,38 @@ export const PicklistEditModalDisplay: FC<ModalProps> = memo(props => {
138160 onConfirm = { onSavePicklist }
139161 title = { title }
140162 >
141- < Alert > { error } </ Alert >
142- < form >
143- < div className = "form-group" >
144- < label className = "control-label" > Name *</ label >
145-
146- < input
147- className = "form-control"
148- onChange = { onNameChange }
149- placeholder = "Give this list a name"
150- type = "text"
151- value = { name }
152- />
153- </ div >
154- < div className = "form-group" >
155- < label className = "control-label" > Description</ label >
156-
157- < textarea
158- className = "form-control"
159- onChange = { onDescriptionChange }
160- placeholder = "Add a description"
161- value = { description }
162- />
163-
164- < CheckboxLK checked = { shared } name = "shared" onChange = { onSharedChanged } >
165- Share this picklist
166- </ CheckboxLK >
167- </ div >
168- </ form >
163+ < Alert > { error || saveError } </ Alert >
164+ { loading && < LoadingSpinner /> }
165+ { showForm && (
166+ < form >
167+ < div className = "form-group" >
168+ < label className = "control-label" > Name *</ label >
169+
170+ < input
171+ className = "form-control"
172+ onChange = { onNameChange }
173+ placeholder = "Give this list a name"
174+ type = "text"
175+ value = { name }
176+ />
177+ </ div >
178+ < div className = "form-group" >
179+ < label className = "control-label" > Description</ label >
180+
181+ < textarea
182+ className = "form-control"
183+ onChange = { onDescriptionChange }
184+ placeholder = "Add a description"
185+ value = { description }
186+ />
187+
188+ < CheckboxLK checked = { shared } name = "shared" onChange = { onSharedChanged } >
189+ Share this picklist
190+ </ CheckboxLK >
191+ </ div >
192+ </ form >
193+ ) }
169194 </ Modal >
170195 ) ;
171196} ) ;
172- PicklistEditModalDisplay . displayName = 'PicklistEditModalDisplay' ;
173-
174- export interface PicklistEditModalProps extends SharedProps {
175- // If sampleFieldKey is present the modal queries the rowIds in order to fetch sampleIds via sampleFieldKey
176- sampleFieldKey ?: string ;
177- schemaQuery ?: SchemaQuery ;
178- selectedRowIds ?: number [ ] | string [ ] ;
179- }
180-
181- export const PicklistEditModal : FC < PicklistEditModalProps > = memo ( props => {
182- const {
183- metricFeatureArea,
184- onCancel,
185- onFinish,
186- picklist,
187- sampleFieldKey,
188- schemaQuery,
189- selectedRowIds,
190- showNotification,
191- } = props ;
192-
193- const { error, loadingState, value : sampleIds } = usePicklistSampleSelections ( selectedRowIds , sampleFieldKey , schemaQuery ) ;
194- const loading = isLoading ( loadingState ) ;
195-
196- if ( loading || error !== undefined ) {
197- const title = loading ? 'Loading Selection Data' : 'Error Loading Selection Data' ;
198- return (
199- < Modal cancelText = "Dismiss" onCancel = { onCancel } title = { title } >
200- { loading && < LoadingSpinner /> }
201- { error }
202- </ Modal >
203- ) ;
204- }
205-
206- return (
207- < PicklistEditModalDisplay
208- metricFeatureArea = { metricFeatureArea }
209- onCancel = { onCancel }
210- onFinish = { onFinish }
211- picklist = { picklist }
212- sampleIds = { sampleIds }
213- showNotification = { showNotification }
214- />
215- ) ;
216- } ) ;
217-
218197PicklistEditModal . displayName = 'PicklistEditModal' ;
0 commit comments