11import {
22 ProvisionerJobLog ,
3+ ProvisionerJobStatus ,
34 TemplateVersion ,
45 UploadResponse ,
56 WorkspaceResource ,
67} from "api/typesGenerated"
78import { assign , createMachine } from "xstate"
89import * as API from "api/api"
910import { TemplateVersionFiles } from "util/templateVersion"
10- import * as Tar from "tar-js"
11+ import Tar from "tar-js"
1112
1213export interface CreateVersionData {
1314 file : File
@@ -40,21 +41,34 @@ export const templateVersionEditorMachine = createMachine(
4041 | { type : "ADD_BUILD_LOG" ; log : ProvisionerJobLog }
4142 | { type : "UPDATE_ACTIVE_VERSION" } ,
4243 services : { } as {
44+ uploadTar : {
45+ data : UploadResponse
46+ }
4347 createBuild : {
4448 data : TemplateVersion
4549 }
4650 cancelBuild : {
51+ data : void
52+ }
53+ fetchVersion : {
4754 data : TemplateVersion
4855 }
56+ getResources : {
57+ data : WorkspaceResource [ ]
58+ }
59+ updateActiveVersion : {
60+ data : void
61+ }
4962 } ,
5063 } ,
64+ tsTypes : { } as import ( "./templateVersionEditorXService.typegen" ) . Typegen0 ,
5165 initial : "idle" ,
5266 states : {
5367 idle : {
5468 on : {
5569 CREATE_VERSION : {
5670 actions : [ "assignCreateBuild" ] ,
57- target : "uploadTar " ,
71+ target : "cancelingBuild " ,
5872 } ,
5973 UPDATE_ACTIVE_VERSION : {
6074 target : "updatingActiveVersion" ,
@@ -71,14 +85,24 @@ export const templateVersionEditorMachine = createMachine(
7185 } ,
7286 } ,
7387 } ,
88+ cancelingBuild : {
89+ tags : "loading" ,
90+ invoke : {
91+ id : "cancelBuild" ,
92+ src : "cancelBuild" ,
93+ onDone : {
94+ target : "uploadTar" ,
95+ } ,
96+ } ,
97+ } ,
7498 uploadTar : {
7599 tags : "loading" ,
76100 invoke : {
77101 id : "uploadTar" ,
78102 src : "uploadTar" ,
79103 onDone : {
80104 target : "creatingBuild" ,
81- actions : [ "assignUploadResponse" ] ,
105+ actions : "assignUploadResponse" ,
82106 } ,
83107 } ,
84108 } ,
@@ -88,7 +112,7 @@ export const templateVersionEditorMachine = createMachine(
88112 id : "createBuild" ,
89113 src : "createBuild" ,
90114 onDone : {
91- actions : [ "assignBuild" ] ,
115+ actions : "assignBuild" ,
92116 target : "watchingBuildLogs" ,
93117 } ,
94118 } ,
@@ -104,14 +128,14 @@ export const templateVersionEditorMachine = createMachine(
104128 } ,
105129 on : {
106130 ADD_BUILD_LOG : {
107- actions : [ "addBuildLog" ] ,
131+ actions : "addBuildLog" ,
108132 } ,
109133 CANCEL_VERSION : {
110- actions : [ "cancelBuild" ] ,
134+ actions : "cancelBuild" ,
111135 target : "idle" ,
112136 } ,
113137 CREATE_VERSION : {
114- actions : [ "cancelBuild" , " assignCreateBuild"] ,
138+ actions : [ "assignCreateBuild" ] ,
115139 target : "uploadTar" ,
116140 } ,
117141 } ,
@@ -145,8 +169,8 @@ export const templateVersionEditorMachine = createMachine(
145169 assignCreateBuild : assign ( {
146170 files : ( _ , event ) => event . files ,
147171 templateId : ( _ , event ) => event . templateId ,
148- buildLogs : [ ] ,
149- resources : [ ] ,
172+ buildLogs : ( _ , _1 ) => [ ] ,
173+ resources : ( _ , _1 ) => [ ] ,
150174 } ) ,
151175 assignResources : assign ( {
152176 resources : ( _ , event ) => event . data ,
@@ -174,7 +198,7 @@ export const templateVersionEditorMachine = createMachine(
174198 ...context . version ,
175199 job : {
176200 ...context . version . job ,
177- status : "running" ,
201+ status : "running" as ProvisionerJobStatus ,
178202 } ,
179203 }
180204 } ,
@@ -238,20 +262,22 @@ export const templateVersionEditorMachine = createMachine(
238262 }
239263 return API . getTemplateVersionResources ( ctx . version . id )
240264 } ,
241- cancelBuild : ( ctx ) => {
265+ cancelBuild : async ( ctx ) => {
242266 if ( ! ctx . version ) {
243- throw new Error ( "template version must be set" )
267+ return
268+ }
269+ if ( ctx . version . job . status === "running" ) {
270+ await API . cancelTemplateVersionBuild ( ctx . version . id )
244271 }
245- return API . cancelTemplateVersionBuild ( ctx . version . id )
246272 } ,
247- updateActiveVersion : ( ctx ) => {
273+ updateActiveVersion : async ( ctx ) => {
248274 if ( ! ctx . templateId ) {
249275 throw new Error ( "template must be set" )
250276 }
251277 if ( ! ctx . version ) {
252278 throw new Error ( "template version must be set" )
253279 }
254- return API . updateActiveTemplateVersion ( ctx . templateId , {
280+ await API . updateActiveTemplateVersion ( ctx . templateId , {
255281 id : ctx . version . id ,
256282 } )
257283 } ,
0 commit comments