11import React , { FC , useContext , useCallback , ChangeEvent , useState } from 'react'
22import {
33 Button ,
4+ ButtonShape ,
45 ComponentColor ,
6+ ComponentSize ,
57 ComponentStatus ,
68 Form ,
79 IconFont ,
810 Input ,
911 InputLabel ,
1012 InputType ,
1113 Overlay ,
14+ SquareButton ,
1215} from '@influxdata/clockface'
1316import { useHistory } from 'react-router-dom'
1417import { QueryContext } from 'src/shared/contexts/query'
@@ -30,7 +33,11 @@ import {DeleteScript} from 'src/dataExplorer/components/DeleteScript'
3033import { LanguageType } from 'src/dataExplorer/components/resources'
3134import { isFlagEnabled } from 'src/shared/utils/featureFlag'
3235import { SCRIPT_EDITOR_PARAMS } from 'src/dataExplorer/components/resources'
33-
36+ import CopyToClipboard from 'src/shared/components/CopyToClipboard'
37+ import {
38+ copyToClipboardFailed ,
39+ copyToClipboardSuccess ,
40+ } from 'src/shared/copy/notifications'
3441interface Props {
3542 language : LanguageType
3643 onClose : ( ) => void
@@ -47,6 +54,9 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
4754 const { cancel} = useContext ( QueryContext )
4855 const { setStatus, setResult} = useContext ( ResultsContext )
4956 const [ error , setError ] = useState < string > ( )
57+ // Setting the name to state rather than persisting it to session storage
58+ // so that we can cancel out of a name change if needed
59+ const [ newName , setNewName ] = useState < string > ( resource ?. data ?. name )
5060 const org = useSelector ( getOrg )
5161
5262 const handleClose = ( ) => {
@@ -80,13 +90,7 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
8090 }
8191
8292 const handleUpdateName = ( event : ChangeEvent < HTMLInputElement > ) => {
83- setResource ( {
84- ...resource ,
85- data : {
86- ...( resource ?. data ?? { } ) ,
87- name : event . target . value ,
88- } ,
89- } )
93+ setNewName ( event . target . value )
9094 }
9195
9296 const clear = useCallback ( ( ) => {
@@ -109,6 +113,7 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
109113 } , [ onClose , setStatus , setResult , cancel , history , org ?. id , type ] )
110114
111115 const handleSaveScript = ( ) => {
116+ resource . data . name = newName
112117 save ( language )
113118 . then ( ( ) => {
114119 setError ( null )
@@ -131,6 +136,17 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
131136 setOverlayType ( OverlayType . DELETE )
132137 }
133138
139+ const handleCopyAttempt = (
140+ copiedText : string ,
141+ isSuccessful : boolean
142+ ) : void => {
143+ if ( isSuccessful ) {
144+ dispatch ( notify ( copyToClipboardSuccess ( copiedText , 'Invokable URL' ) ) )
145+ } else {
146+ dispatch ( notify ( copyToClipboardFailed ( copiedText , 'Invokable URL' ) ) )
147+ }
148+ }
149+
134150 if ( type == null ) {
135151 return null
136152 }
@@ -186,7 +202,7 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
186202 name = "name"
187203 required
188204 type = { InputType . Text }
189- value = { resource ?. data ?. name }
205+ value = { newName }
190206 onChange = { handleUpdateName }
191207 status = { error ? ComponentStatus . Error : ComponentStatus . Default }
192208 />
@@ -197,12 +213,37 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
197213 ) }
198214 < InputLabel > Description</ InputLabel >
199215 < Input
216+ className = "script-description__input"
200217 name = "description"
201218 required
202219 type = { InputType . Text }
203220 value = { resource ?. data ?. description }
204221 onChange = { handleUpdateDescription }
205222 />
223+ { type === OverlayType . EDIT && (
224+ < >
225+ < InputLabel > Invokable URL</ InputLabel >
226+ < CopyToClipboard
227+ text = { `${ window . location . origin } /api/v2/scripts/${ resource . data . id } ` }
228+ onCopy = { handleCopyAttempt }
229+ >
230+ < div className = "invokable-script__url" >
231+ < Input
232+ type = { InputType . Text }
233+ value = { `${ window . location . origin } /api/v2/scripts/${ resource . data . id } ` }
234+ />
235+ < SquareButton
236+ testID = "copy-to-clipboard--script"
237+ size = { ComponentSize . Small }
238+ color = { ComponentColor . Secondary }
239+ icon = { IconFont . Duplicate_New }
240+ shape = { ButtonShape . StretchToFit }
241+ titleText = "Copy to clipboard"
242+ />
243+ </ div >
244+ </ CopyToClipboard >
245+ </ >
246+ ) }
206247 { type === OverlayType . EDIT && (
207248 < Button
208249 icon = { IconFont . Trash_New }
@@ -233,7 +274,7 @@ const SaveAsScript: FC<Props> = ({language, onClose, setOverlayType, type}) => {
233274 < Button
234275 color = { ComponentColor . Primary }
235276 status = {
236- ( resource ?. data ?. name ?. length ?? 0 ) === 0
277+ ( newName ?. length ?? 0 ) === 0
237278 ? ComponentStatus . Disabled
238279 : ComponentStatus . Default
239280 }
0 commit comments