@@ -11,9 +11,11 @@ import { ParamStep } from './stepComponents/ParamStep';
11
11
import { PostProcessStep } from './stepComponents/PostProcessStep' ;
12
12
import { ConfirmationStep } from './stepComponents/ConfirmationStep' ;
13
13
import { DumbTextArea } from './stepComponents/DumbTextAreaStep' ;
14
- import { DB_COLLECTION_EXPERIMENTS } from '../../../../lib/db' ;
14
+ import { DB_COLLECTION_EXPERIMENTS , submitExperiment } from '../../../../lib/db' ;
15
15
16
16
import { getDocumentFromId } from '../../../../lib/mongodb_funcs' ;
17
+ import { useSession } from 'next-auth/react' ;
18
+ import toast , { Toaster } from 'react-hot-toast' ;
17
19
18
20
const DEFAULT_TRIAL_TIMEOUT_SECONDS = 5 * 60 * 60 ; // 5 hours in seconds
19
21
@@ -63,6 +65,8 @@ const Steps = ({ steps }) => {
63
65
64
66
65
67
const NewExperiment = ( { formState, setFormState, copyID, setCopyId, ...rest } ) => {
68
+ const { data : session } = useSession ( ) ;
69
+
66
70
const form = useForm ( {
67
71
// TODO make this follow the schema as closely as we can
68
72
initialValues : {
@@ -130,6 +134,8 @@ const NewExperiment = ({ formState, setFormState, copyID, setCopyId, ...rest })
130
134
setStatus ( FormStates . Info ) ;
131
135
} ;
132
136
137
+ const [ fileId , setFileId ] = useState ( null ) ;
138
+
133
139
useLayoutEffect ( ( ) => {
134
140
if ( formState === FormStates . Info ) {
135
141
setOpen ( false ) ;
@@ -141,124 +147,155 @@ const NewExperiment = ({ formState, setFormState, copyID, setCopyId, ...rest })
141
147
}
142
148
} , [ formState ] ) ; // TODO adding 'form' causes an update loop
143
149
150
+ useEffect ( ( ) => {
151
+ console . log ( "FileID" , fileId ) ;
152
+ } , [ fileId ] ) ;
153
+
144
154
return (
145
- < Transition . Root show = { open } as = { Fragment } >
146
- < Dialog
147
- as = 'div'
148
- className = 'fixed inset-0 overflow-hidden'
149
- onClose = { ( ) => setFormState ( 0 ) }
150
- >
151
- < div className = 'absolute inset-0 overflow-hidden' >
152
- < Dialog . Overlay className = 'absolute inset-0' />
155
+ < div >
156
+ < Toaster />
157
+ < Transition . Root show = { open } as = { Fragment } >
158
+ < Dialog
159
+ as = 'div'
160
+ className = 'fixed inset-0 overflow-hidden'
161
+ onClose = { ( ) => setFormState ( 0 ) }
162
+ >
163
+ < div className = 'absolute inset-0 overflow-hidden' >
164
+ < Dialog . Overlay className = 'absolute inset-0' />
153
165
154
- < div className = 'pointer-events-none fixed inset-y-0 right-0 flex pl-20 sm:pl-16' >
155
- < Transition . Child
156
- as = { Fragment }
157
- enter = 'transform transition ease-in-out duration-500 sm:duration-700'
158
- enterFrom = 'translate-x-full'
159
- enterTo = 'translate-x-0'
160
- leave = 'transform transition ease-in-out duration-500 sm:duration-700'
161
- leaveFrom = 'translate-x-0'
162
- leaveTo = 'translate-x-full'
163
- >
164
- < div className = 'pointer-events-auto w-screen max-w-5xl' >
165
- < form
166
- className = 'flex h-full flex-col bg-white shadow-xl'
167
- onSubmit = { form . onSubmit ( ( values ) => {
168
- } ) }
169
- >
170
- < div className = 'flex flex-col' >
171
- < div className = 'bg-gray-50 px-4 py-6 sm:px-6' >
172
- < div className = 'flex items-center align-center justify-between space-x-3' >
173
- < Steps
174
- steps = { [ 'Information' , 'Parameters' , 'User Defined Constants' , 'Post Process' , 'Confirmation' , 'Dispatch' ] . map (
175
- ( step , idx ) => {
176
- return {
177
- id : idx + 1 ,
178
- name : step ,
179
- status : status < idx ? 'upcoming' : 'complete' ,
180
- } ;
181
- }
182
- ) }
183
- />
166
+ < div className = 'pointer-events-none fixed inset-y-0 right-0 flex pl-20 sm:pl-16' >
167
+ < Transition . Child
168
+ as = { Fragment }
169
+ enter = 'transform transition ease-in-out duration-500 sm:duration-700'
170
+ enterFrom = 'translate-x-full'
171
+ enterTo = 'translate-x-0'
172
+ leave = 'transform transition ease-in-out duration-500 sm:duration-700'
173
+ leaveFrom = 'translate-x-0'
174
+ leaveTo = 'translate-x-full'
175
+ >
176
+ < div className = 'pointer-events-auto w-screen max-w-5xl' >
177
+ < form
178
+ className = 'flex h-full flex-col bg-white shadow-xl'
179
+ onSubmit = { form . onSubmit ( ( values ) => {
180
+ } ) }
181
+ >
182
+ < div className = 'flex flex-col' >
183
+ < div className = 'bg-gray-50 px-4 py-6 sm:px-6' >
184
+ < div className = 'flex items-center align-center justify-between space-x-3' >
185
+ < Steps
186
+ steps = { [ 'Information' , 'Parameters' , 'User Defined Constants' , 'Post Process' , 'Confirmation' , 'Dispatch' ] . map (
187
+ ( step , idx ) => {
188
+ return {
189
+ id : idx + 1 ,
190
+ name : step ,
191
+ status : status < idx ? 'upcoming' : 'complete' ,
192
+ } ;
193
+ }
194
+ ) }
195
+ />
196
+ </ div >
184
197
</ div >
185
198
</ div >
186
- </ div >
187
199
188
- { /* <div className='h-full flex flex-col space-y-6 py-6 sm:space-y-0 sm:divide-y sm:divide-gray-200 sm:py-0'> */ }
189
- { status === FormStates . Info ? (
190
- < InformationStep form = { form } > </ InformationStep >
191
- ) : status === FormStates . DumbTextArea ? (
192
- < DumbTextArea form = { form } > </ DumbTextArea >
193
- ) : status === FormStates . Params ? (
194
- < ParamStep form = { form } confirmedValues = { confirmedValues } setConfirmedValues = { setConfirmedValues } > { fields } </ ParamStep >
195
- ) : status === FormStates . ProcessStep ? (
196
- < PostProcessStep form = { form } > { fields } </ PostProcessStep >
197
- ) : status === FormStates . Confirmation ? (
198
- < ConfirmationStep form = { form } />
199
- ) : (
200
- < DispatchStep form = { form } id = { id } onDropComplete = { onDropComplete } />
201
- ) }
200
+ { /* <div className='h-full flex flex-col space-y-6 py-6 sm:space-y-0 sm:divide-y sm:divide-gray-200 sm:py-0'> */ }
201
+ { status === FormStates . Info ? (
202
+ < InformationStep form = { form } > </ InformationStep >
203
+ ) : status === FormStates . DumbTextArea ? (
204
+ < DumbTextArea form = { form } > </ DumbTextArea >
205
+ ) : status === FormStates . Params ? (
206
+ < ParamStep form = { form } confirmedValues = { confirmedValues } setConfirmedValues = { setConfirmedValues } > { fields } </ ParamStep >
207
+ ) : status === FormStates . ProcessStep ? (
208
+ < PostProcessStep form = { form } > { fields } </ PostProcessStep >
209
+ ) : status === FormStates . Confirmation ? (
210
+ < ConfirmationStep form = { form } />
211
+ ) : (
212
+ < DispatchStep form = { form } id = { id } updateId = { setFileId } onDropComplete = { onDropComplete } />
213
+ ) }
202
214
203
- < div className = 'flex-shrink-0 border-t border-gray-200 px-4 py-5 sm:px-6' >
204
- < div className = 'flex justify-end space-x-3' >
205
- < div className = 'flex space-x-3 flex-1' >
206
- < input
207
- type = 'number'
208
- placeholder = { 'Number of Workers' }
209
- className = 'rounded-md border-gray-300 shadow-sm focus:border-blue-500 sm:text-sm'
210
- required
211
- { ...form . getInputProps ( 'workers' ) }
212
- />
213
- < Toggle
214
- label = { 'Verbose?' }
215
- onChange = { ( ) => {
216
- form . setFieldValue ( 'verbose' , ! form . values . verbose ) ;
217
- } }
218
- />
219
- </ div >
220
- < button
221
- type = 'button'
222
- className = 'rounded-md border w-1/6 border-gray-300 bg-white py-2 px-4 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2'
223
- onClick = {
224
- status === FormStates . Info ?
225
- ( ) => {
226
- localStorage . removeItem ( 'ID' ) ;
227
- setFormState ( - 1 ) ;
215
+ < div className = 'flex-shrink-0 border-t border-gray-200 px-4 py-5 sm:px-6' >
216
+ < div className = 'flex justify-end space-x-3' >
217
+ < div className = 'flex space-x-3 flex-1' >
218
+ < input
219
+ type = 'number'
220
+ placeholder = { 'Number of Workers' }
221
+ className = 'rounded-md border-gray-300 shadow-sm focus:border-blue-500 sm:text-sm'
222
+ required
223
+ { ...form . getInputProps ( 'workers' ) }
224
+ />
225
+ < Toggle
226
+ label = { 'Verbose?' }
227
+ onChange = { ( ) => {
228
+ form . setFieldValue ( 'verbose' , ! form . values . verbose ) ;
229
+ } }
230
+ />
231
+ </ div >
232
+ < button
233
+ type = 'button'
234
+ className = 'rounded-md border w-1/6 border-gray-300 bg-white py-2 px-4 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2'
235
+ onClick = {
236
+ status === FormStates . Info ?
237
+ ( ) => {
238
+ localStorage . removeItem ( 'ID' ) ;
239
+ setFormState ( - 1 ) ;
240
+ } :
241
+ ( ) => {
242
+ setStatus ( status - 1 ) ;
243
+ }
244
+ }
245
+ >
246
+ { status === FormStates . Info ? 'Cancel' : 'Back' }
247
+ </ button >
248
+ < button
249
+ className = 'rounded-md w-1/6 border border-transparent bg-blue-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2'
250
+ { ...( status === FormStates . Dispatch ?
251
+ {
252
+ type : 'submit' , onClick : ( ) => {
253
+ if ( fileId ) {
254
+ setFormState ( - 1 ) ;
255
+ localStorage . removeItem ( 'ID' ) ;
256
+ setStatus ( FormStates . Info ) ;
257
+ console . log ( "ID WAS!" ) ;
258
+ console . log ( id ) ;
259
+ submitExperiment ( form . values as any , session ?. user ?. id as string , fileId ) . then ( async ( json ) => {
260
+ const expId = json [ 'id' ] ;
261
+ const response = await fetch ( `/api/experiments/start/${ expId } ` , {
262
+ method : 'POST' ,
263
+ headers : new Headers ( { 'Content-Type' : 'application/json' } ) ,
264
+ credentials : 'same-origin' ,
265
+ body : JSON . stringify ( { id : expId } ) ,
266
+ } ) ;
267
+ if ( response . ok ) {
268
+ console . log ( `experiment ${ expId } started!` ) ;
269
+ toast . success ( "Started experiment!" , { duration : 1500 } ) ;
270
+ }
271
+ else
272
+ {
273
+ toast . error ( "Failed to start experiment!" , { duration : 1500 } ) ;
274
+ }
275
+ } )
276
+ }
277
+ else {
278
+ toast . error ( "No file selected!" , { duration : 1500 } )
279
+ }
280
+ }
228
281
} :
229
- ( ) => {
230
- setStatus ( status - 1 ) ;
231
- }
232
- }
233
- >
234
- { status === FormStates . Info ? 'Cancel' : 'Back' }
235
- </ button >
236
- { ! ( status === FormStates . Dispatch ) && < button
237
- className = 'rounded-md w-1/6 border border-transparent bg-blue-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2'
238
- { ...( status === FormStates . Dispatch ?
239
- {
240
- type : 'submit' , onClick : ( ) => {
241
- setFormState ( - 1 ) ;
242
- localStorage . removeItem ( 'ID' ) ;
243
- setStatus ( FormStates . Info ) ;
244
- }
245
- } :
246
- {
247
- type : 'button' ,
248
- onClick : ( ) => setStatus ( status + 1 ) ,
249
- } ) }
250
- >
251
- { status === FormStates . Dispatch ? 'Dispatch' : 'Next' }
252
- </ button > }
282
+ {
283
+ type : 'button' ,
284
+ onClick : ( ) => setStatus ( status + 1 ) ,
285
+ } ) }
286
+ >
287
+ { status === FormStates . Dispatch ? 'Dispatch' : 'Next' }
288
+ </ button >
289
+ </ div >
253
290
</ div >
254
- </ div >
255
- </ form >
256
- </ div >
257
- </ Transition . Child >
291
+ </ form >
292
+ </ div >
293
+ </ Transition . Child >
294
+ </ div >
258
295
</ div >
259
- </ div >
260
- </ Dialog >
261
- </ Transition . Root >
296
+ </ Dialog >
297
+ </ Transition . Root >
298
+ </ div >
262
299
) ;
263
300
} ;
264
301
0 commit comments