@@ -27,19 +27,36 @@ import {getOrg} from 'src/organizations/selectors'
2727import OpenScript from 'src/dataExplorer/components/OpenScript'
2828
2929interface Props {
30- onClear : ( ) => void
3130 onClose : ( ) => void
3231 type : OverlayType | null
3332}
3433
35- const SaveAsScript : FC < Props > = ( { onClose, onClear , type} ) => {
34+ const SaveAsScript : FC < Props > = ( { onClose, type} ) => {
3635 const dispatch = useDispatch ( )
3736 const history = useHistory ( )
38- const { query, resource, setResource, save} = useContext ( PersistanceContext )
37+ const { hasChanged, resource, setResource, save} = useContext (
38+ PersistanceContext
39+ )
3940 const { cancel} = useContext ( QueryContext )
4041 const { setStatus, setResult} = useContext ( ResultsContext )
4142 const org = useSelector ( getOrg )
4243
44+ const handleClose = ( ) => {
45+ if ( ! resource ?. data ?. id ) {
46+ // clear out any meta data that's been set by the user prior to saving
47+ // if they decide to cancel out of the process and close the modal
48+ delete resource . data ?. name
49+ delete resource . data ?. description
50+ setResource ( {
51+ ...resource ,
52+ data : {
53+ ...( resource ?. data ?? { } ) ,
54+ } ,
55+ } )
56+ }
57+ onClose ( )
58+ }
59+
4360 const handleUpdateDescription = ( event : ChangeEvent < HTMLInputElement > ) => {
4461 setResource ( {
4562 ...resource ,
@@ -64,7 +81,6 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
6481 cancel ( )
6582 setStatus ( RemoteDataState . NotStarted )
6683 setResult ( null )
67- onClear ( )
6884
6985 history . replace ( `/orgs/${ org . id } /data-explorer/from/script` )
7086 if ( type !== OverlayType . OPEN ) {
@@ -75,17 +91,13 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
7591 const handleSaveScript = ( ) => {
7692 try {
7793 save ( ) . then ( ( ) => {
78- dispatch (
79- notify ( scriptSaveSuccess ( resource ?. data ?. name ?? 'Untitled Script' ) )
80- )
94+ dispatch ( notify ( scriptSaveSuccess ( resource ?. data ?. name ?? '' ) ) )
8195 if ( type === OverlayType . NEW ) {
8296 clear ( )
8397 }
8498 } )
8599 } catch ( error ) {
86- dispatch (
87- notify ( scriptSaveFail ( resource ?. data ?. name ?? 'Untitled Script' ) )
88- )
100+ dispatch ( notify ( scriptSaveFail ( resource ?. data ?. name ?? '' ) ) )
89101 console . error ( { error} )
90102 } finally {
91103 if ( type !== OverlayType . OPEN ) {
@@ -104,15 +116,36 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
104116 overlayTitle = 'Do you want to save your Script first?'
105117 }
106118
107- if ( query . length === 0 ) {
108- return < OpenScript onClose = { onClose } />
119+ if ( ! hasChanged && type === OverlayType . OPEN ) {
120+ return < OpenScript onCancel = { handleClose } onClose = { onClose } />
121+ }
122+
123+ const displayWarning =
124+ ( type === OverlayType . NEW && ! resource ?. data ?. name ) ||
125+ ( type === OverlayType . OPEN && ! resource ?. data ?. name ) ||
126+ ( type === OverlayType . OPEN && hasChanged && resource ?. data ?. id )
127+
128+ let saveText = 'Save'
129+
130+ if ( resource ?. data ?. id ) {
131+ if ( type === OverlayType . SAVE ) {
132+ saveText = 'Update'
133+ } else {
134+ saveText = 'Yes, Update'
135+ }
136+ } else {
137+ if ( type === OverlayType . SAVE ) {
138+ saveText = 'Save'
139+ } else {
140+ saveText = 'Yes, Save'
141+ }
109142 }
110143
111144 return (
112145 < Overlay . Container maxWidth = { 500 } >
113- < Overlay . Header title = { overlayTitle } onDismiss = { onClose } />
146+ < Overlay . Header title = { overlayTitle } onDismiss = { handleClose } />
114147 < Overlay . Body >
115- { type !== OverlayType . SAVE && (
148+ { displayWarning && (
116149 < div className = "save-script-overlay__warning-text" >
117150 "{ resource ?. data ?. name ?? 'Untitled Script' } " will be overwritten by
118151 a new one if you don’t save it.
@@ -127,6 +160,11 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
127160 type = { InputType . Text }
128161 value = { resource ?. data ?. name }
129162 onChange = { handleUpdateName }
163+ status = {
164+ resource ?. data ?. id
165+ ? ComponentStatus . Disabled
166+ : ComponentStatus . Default
167+ }
130168 />
131169 < InputLabel > Description</ InputLabel >
132170 < Input
@@ -141,7 +179,7 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
141179 < Overlay . Footer >
142180 < Button
143181 color = { ComponentColor . Tertiary }
144- onClick = { onClose }
182+ onClick = { handleClose }
145183 text = "Cancel"
146184 />
147185 { type !== OverlayType . SAVE && (
@@ -161,7 +199,7 @@ const SaveAsScript: FC<Props> = ({onClose, onClear, type}) => {
161199 : ComponentStatus . Default
162200 }
163201 onClick = { handleSaveScript }
164- text = { type === OverlayType . SAVE ? 'Save' : 'Yes, Save' }
202+ text = { saveText }
165203 />
166204 ) }
167205 </ Overlay . Footer >
0 commit comments