@@ -19,7 +19,7 @@ import {variableSchema, arrayOfVariables} from 'src/schemas/variables'
1919// APIs
2020import * as api from 'src/client'
2121import { hydrateVars } from 'src/variables/utils/hydrateVars'
22- import { createVariableFromTemplate as createVariableFromTemplateAJAX } from 'src/templates/api'
22+ import { createResourceFromPkgerTemplate } from 'src/templates/api'
2323
2424// Utils
2525import {
@@ -44,7 +44,6 @@ import {
4444 AppState ,
4545 GetState ,
4646 RemoteDataState ,
47- VariableTemplate ,
4847 Label ,
4948 GenVariable ,
5049 Variable ,
@@ -63,6 +62,7 @@ import {
6362} from 'src/shared/utils/filterUnusedVars'
6463import { getActiveTimeMachine } from 'src/timeMachine/selectors'
6564import { getActiveQuery } from 'src/timeMachine/selectors'
65+ import { getVariables as getVariablesAction } from 'src/variables/actions/thunks'
6666
6767type Action = VariableAction | EditorAction | NotifyAction
6868
@@ -267,26 +267,28 @@ export const createVariable = (
267267 }
268268}
269269
270- export const createVariableFromTemplate = (
271- template : VariableTemplate
272- ) => async ( dispatch : Dispatch < Action > , getState : GetState ) => {
270+ export const createVariableFromTemplate = ( template : api . Template ) => async (
271+ dispatch : Dispatch < Action > ,
272+ getState : GetState
273+ ) => {
273274 try {
274275 const state = getState ( )
275276 const org = getOrg ( state )
276- const resp = await createVariableFromTemplateAJAX ( template , org . id )
277+ // only one variable in the exported template so we can safely just index the 0th element
278+ const variableName = template [ 0 ] . spec . name
279+
280+ await createResourceFromPkgerTemplate ( template , org . id )
281+
282+ await getVariablesAction ( ) ( dispatch , getState )
277283
278- const createdVar = normalize < Variable , VariableEntities , string > (
279- resp ,
280- variableSchema
281- )
282284 event ( 'variable.create.from_template.success' , {
283- id : resp ?. id ,
284- name : resp ?. name ,
285+ name : variableName ,
285286 } )
286- dispatch ( setVariable ( resp . id , RemoteDataState . Done , createdVar ) )
287- dispatch ( notify ( copy . createVariableSuccess ( resp . name ) ) )
287+ dispatch ( notify ( copy . createVariableSuccess ( variableName ) ) )
288288 } catch ( error ) {
289- event ( 'variable.create.from_template.failure' , { template : template ?. id } )
289+ event ( 'variable.create.from_template.failure' , {
290+ template : template [ 0 ] . meta . name ,
291+ } )
290292 console . error ( error )
291293 dispatch ( notify ( copy . createVariableFailed ( error . message ) ) )
292294 }
0 commit comments