@@ -16,7 +16,6 @@ import {
16
16
PrimaryButton ,
17
17
SecondaryButton ,
18
18
} from "../landing-page/form-elements.tsx" ;
19
- import { API } from "../../api/api.ts" ;
20
19
import { useGlobalState } from "../../global-state/context-provider.tsx" ;
21
20
import { Spinner } from "../loader/loader.tsx" ;
22
21
import { FlexContainer } from "../generic-components.ts" ;
@@ -27,13 +26,7 @@ import { NavigateToRootButton } from "../navigate-to-root-button/navigate-to-roo
27
26
import { ResponsiveFormContainer } from "../user-settings/user-settings.tsx" ;
28
27
import { isMobile } from "../../bowser.ts" ;
29
28
import { Checkbox } from "../checkbox/checkbox.tsx" ;
30
-
31
- type FormValues = {
32
- productionName : string ;
33
- defaultLine : string ;
34
- defaultLineProgramOutput : boolean ;
35
- lines : { name : string ; programOutputLine ?: boolean } [ ] ;
36
- } ;
29
+ import { FormValues , useCreateProduction } from "./use-create-production.tsx" ;
37
30
38
31
const HeaderWrapper = styled . div `
39
32
display: flex;
@@ -88,10 +81,8 @@ const CheckboxWrapper = styled.div`
88
81
89
82
export const CreateProductionPage = ( ) => {
90
83
const [ , dispatch ] = useGlobalState ( ) ;
91
- const [ createdProductionId , setCreatedProductionId ] = useState < string | null > (
92
- null
93
- ) ;
94
- const [ loading , setLoading ] = useState < boolean > ( false ) ;
84
+ const [ createNewProduction , setCreateNewProduction ] =
85
+ useState < FormValues | null > ( null ) ;
95
86
const [ copiedUrl , setCopiedUrl ] = useState < boolean > ( false ) ;
96
87
const {
97
88
formState : { errors } ,
@@ -116,33 +107,16 @@ export const CreateProductionPage = () => {
116
107
} ,
117
108
} ) ;
118
109
110
+ const { createdProductionId, loading } = useCreateProduction ( {
111
+ createNewProduction,
112
+ } ) ;
113
+
119
114
const { error : productionFetchError , production } = useFetchProduction (
120
115
createdProductionId ? parseInt ( createdProductionId , 10 ) : null
121
116
) ;
122
117
123
118
const onSubmit : SubmitHandler < FormValues > = ( value ) => {
124
- setLoading ( true ) ;
125
- API . createProduction ( {
126
- name : value . productionName ,
127
- lines : [
128
- {
129
- name : value . defaultLine ,
130
- programOutputLine : value . defaultLineProgramOutput ,
131
- } ,
132
- ...value . lines ,
133
- ] ,
134
- } )
135
- . then ( ( v ) => {
136
- setCreatedProductionId ( v . productionId ) ;
137
- setLoading ( false ) ;
138
- } )
139
- . catch ( ( error ) => {
140
- dispatch ( {
141
- type : "ERROR" ,
142
- payload : error ,
143
- } ) ;
144
- setLoading ( false ) ;
145
- } ) ;
119
+ setCreateNewProduction ( value ) ;
146
120
} ;
147
121
148
122
// Reset form values when created production id changes
0 commit comments