1
- import { useMachine } from "@xstate/react" ;
2
1
import {
3
2
CreateTemplateVersionRequest ,
4
3
TemplateVersionVariable ,
5
4
VariableValue ,
6
5
} from "api/typesGenerated" ;
7
6
import { displaySuccess } from "components/GlobalSnackbar/utils" ;
8
7
import { useOrganizationId } from "hooks/useOrganizationId" ;
9
- import { FC } from "react" ;
8
+ import { useCallback , type FC } from "react" ;
10
9
import { Helmet } from "react-helmet-async" ;
11
10
import { useNavigate , useParams } from "react-router-dom" ;
12
- import { templateVariablesMachine } from "xServices/template/templateVariablesXService" ;
13
- import { pageTitle } from "../../../utils/page" ;
11
+ import { pageTitle } from "utils/page" ;
14
12
import { useTemplateSettings } from "../TemplateSettingsLayout" ;
15
13
import { TemplateVariablesPageView } from "./TemplateVariablesPageView" ;
14
+ import {
15
+ createAndBuildTemplateVersion ,
16
+ templateVersion ,
17
+ templateVersionVariables ,
18
+ updateActiveTemplateVersion ,
19
+ } from "api/queries/templates" ;
20
+ import { useMutation , useQuery , useQueryClient } from "@tanstack/react-query" ;
21
+ import { ErrorAlert } from "components/Alert/ErrorAlert" ;
22
+ import { Loader } from "components/Loader/Loader" ;
16
23
17
24
export const TemplateVariablesPage : FC = ( ) => {
18
25
const { template : templateName } = useParams ( ) as {
19
26
organization : string ;
20
27
template : string ;
21
28
} ;
22
- const organizationId = useOrganizationId ( ) ;
29
+ const orgId = useOrganizationId ( ) ;
23
30
const { template } = useTemplateSettings ( ) ;
24
31
const navigate = useNavigate ( ) ;
25
- const [ state , send ] = useMachine ( templateVariablesMachine , {
26
- context : {
27
- organizationId,
28
- template,
29
- } ,
30
- actions : {
31
- onUpdateTemplate : ( ) => {
32
- displaySuccess ( "Template updated successfully" ) ;
33
- } ,
34
- } ,
32
+ const queryClient = useQueryClient ( ) ;
33
+ const versionId = template . active_version_id ;
34
+
35
+ const {
36
+ data : version ,
37
+ error : versionError ,
38
+ isLoading : isVersionLoading ,
39
+ } = useQuery ( { ...templateVersion ( versionId ) , keepPreviousData : true } ) ;
40
+ const {
41
+ data : variables ,
42
+ error : variablesError ,
43
+ isLoading : isVariablesLoading ,
44
+ } = useQuery ( {
45
+ ...templateVersionVariables ( versionId ) ,
46
+ keepPreviousData : true ,
35
47
} ) ;
48
+
49
+ const {
50
+ mutateAsync : sendCreateAndBuildTemplateVersion ,
51
+ error : buildError ,
52
+ isLoading : isBuilding ,
53
+ } = useMutation ( createAndBuildTemplateVersion ( orgId ) ) ;
36
54
const {
37
- activeTemplateVersion,
38
- templateVariables,
39
- getTemplateDataError,
40
- updateTemplateError,
41
- jobError,
42
- } = state . context ;
55
+ mutateAsync : sendUpdateActiveTemplateVersion ,
56
+ error : publishError ,
57
+ isLoading : isPublishing ,
58
+ } = useMutation ( updateActiveTemplateVersion ( template , queryClient ) ) ;
59
+
60
+ const publishVersion = useCallback (
61
+ async ( versionId : string ) => {
62
+ await sendUpdateActiveTemplateVersion ( versionId ) ;
63
+ displaySuccess ( "Template updated successfully" ) ;
64
+ } ,
65
+ [ sendUpdateActiveTemplateVersion ] ,
66
+ ) ;
67
+
68
+ const buildVersion = useCallback (
69
+ async ( req : CreateTemplateVersionRequest ) => {
70
+ const newVersionId = await sendCreateAndBuildTemplateVersion ( req ) ;
71
+ await publishVersion ( newVersionId ) ;
72
+ } ,
73
+ [ sendCreateAndBuildTemplateVersion , publishVersion ] ,
74
+ ) ;
75
+
76
+ const isSubmitting = Boolean ( isBuilding || isPublishing ) ;
77
+
78
+ const error = versionError ?? variablesError ;
79
+ if ( error ) {
80
+ return < ErrorAlert error = { error } /> ;
81
+ }
82
+
83
+ if ( isVersionLoading || isVariablesLoading ) {
84
+ return < Loader /> ;
85
+ }
43
86
44
87
return (
45
88
< >
@@ -48,23 +91,19 @@ export const TemplateVariablesPage: FC = () => {
48
91
</ Helmet >
49
92
50
93
< TemplateVariablesPageView
51
- isSubmitting = { state . hasTag ( "submitting" ) }
52
- templateVersion = { activeTemplateVersion }
53
- templateVariables = { templateVariables }
94
+ isSubmitting = { isSubmitting }
95
+ templateVersion = { version }
96
+ templateVariables = { variables }
54
97
errors = { {
55
- getTemplateDataError,
56
- updateTemplateError,
57
- jobError,
98
+ buildError,
99
+ publishError,
58
100
} }
59
101
onCancel = { ( ) => {
60
102
navigate ( `/templates/${ templateName } ` ) ;
61
103
} }
62
- onSubmit = { ( formData ) => {
63
- const request = filterEmptySensitiveVariables (
64
- formData ,
65
- templateVariables ,
66
- ) ;
67
- send ( { type : "UPDATE_TEMPLATE_EVENT" , request : request } ) ;
104
+ onSubmit = { async ( formData ) => {
105
+ const request = filterEmptySensitiveVariables ( formData , variables ) ;
106
+ await buildVersion ( request ) ;
68
107
} }
69
108
/>
70
109
</ >
0 commit comments