22
33import { useEffect , useRef } from 'react' ;
44
5- import { Dialog , Form } from '@douglasneuroinformatics/libui/components' ;
5+ import { Button , Dialog } from '@douglasneuroinformatics/libui/components' ;
66import { useNotificationsStore } from '@douglasneuroinformatics/libui/hooks' ;
7- import type { AuthPayload } from '@opendatacapture/schemas/auth' ;
87import axios , { isAxiosError } from 'axios' ;
98import type { AxiosResponse } from 'axios' ;
10- import { z } from 'zod/v4' ;
119
1210import { useAppStore } from '@/store' ;
1311
14- const $UploadBundleData = z . object ( {
15- apiBaseUrl : z . url ( ) ,
16- username : z . string ( ) . min ( 1 ) ,
17- password : z . string ( ) . min ( 1 )
18- } ) ;
19-
20- type UploadBundleData = z . infer < typeof $UploadBundleData > ;
21-
2212export type UploadBundleDialogProps = {
2313 isOpen : boolean ;
2414 setIsOpen : ( value : boolean ) => void ;
2515} ;
2616
2717export const UploadBundleDialog = ( { isOpen, setIsOpen } : UploadBundleDialogProps ) => {
2818 const addNotification = useNotificationsStore ( ( store ) => store . addNotification ) ;
29- const defaultApiBaseUrl = useAppStore ( ( store ) => store . settings . apiBaseUrl ) ;
19+
20+ const auth = useAppStore ( ( store ) => store . auth ) ;
21+ const apiBaseUrl = useAppStore ( ( store ) => store . settings . apiBaseUrl ) ;
22+
3023 const transpilerStateRef = useRef ( useAppStore . getState ( ) . transpilerState ) ;
3124
3225 useEffect ( ( ) => {
@@ -38,46 +31,23 @@ export const UploadBundleDialog = ({ isOpen, setIsOpen }: UploadBundleDialogProp
3831 ) ;
3932 } , [ ] ) ;
4033
41- const handleSubmit = async ( { apiBaseUrl , username , password } : UploadBundleData ) => {
34+ const handleSubmit = async ( ) => {
4235 const state = transpilerStateRef . current ;
4336 if ( state . status === 'building' || state . status === 'initial' ) {
4437 addNotification ( { message : 'Upload Failed: Transpilation Incomplete' , type : 'error' } ) ;
4538 return ;
4639 } else if ( state . status === 'error' ) {
4740 addNotification ( { message : 'Upload Failed: Transpilation Error' , type : 'error' } ) ;
4841 return ;
42+ } else if ( ! auth ) {
43+ addNotification ( { message : 'Login Required' , type : 'error' } ) ;
44+ return ;
4945 }
5046
47+ const accessToken = auth . accessToken ;
5148 const bundle = state . bundle ;
52- const loginPath = `${ apiBaseUrl } /v1/auth/login` ;
5349 const createInstrumentPath = `${ apiBaseUrl } /v1/instruments` ;
5450
55- let loginResponse : AxiosResponse < AuthPayload > ;
56- try {
57- loginResponse = await axios . post (
58- loginPath ,
59- { username, password } ,
60- {
61- headers : {
62- Accept : 'application/json'
63- } ,
64- validateStatus : ( status ) => status === 200
65- }
66- ) ;
67- } catch ( err ) {
68- console . error ( err ) ;
69- let message : string ;
70- if ( isAxiosError ( err ) ) {
71- message = err . response ? `${ err . response . status } ${ err . response . statusText } ` : err . message ;
72- } else {
73- message = 'Unknown Error' ;
74- }
75- addNotification ( { message, type : 'error' , title : 'HTTP Request Failed' } ) ;
76- return ;
77- }
78-
79- const accessToken = loginResponse . data . accessToken ;
80-
8151 let createInstrumentResponse : AxiosResponse ;
8252 try {
8353 createInstrumentResponse = await axios . post (
@@ -120,45 +90,9 @@ export const UploadBundleDialog = ({ isOpen, setIsOpen }: UploadBundleDialogProp
12090 </ Dialog . Description >
12191 </ Dialog . Header >
12292 < Dialog . Body className = "grid gap-4" >
123- < Form
124- content = { [
125- {
126- title : 'Instance Settings' ,
127- fields : {
128- apiBaseUrl : {
129- description : 'The base path for your Open Data Capture REST API.' ,
130- kind : 'string' ,
131- placeholder : 'e.g., https://demo.opendatacapture.org/api' ,
132- label : 'API Base URL' ,
133- variant : 'input'
134- }
135- }
136- } ,
137- {
138- title : 'Login Credentials' ,
139- fields : {
140- username : {
141- kind : 'string' ,
142- label : 'Username' ,
143- variant : 'input'
144- } ,
145- password : {
146- kind : 'string' ,
147- label : 'Password' ,
148- variant : 'password'
149- }
150- }
151- }
152- ] }
153- initialValues = { {
154- apiBaseUrl : defaultApiBaseUrl
155- } }
156- validationSchema = { $UploadBundleData }
157- onSubmit = { ( data ) => {
158- void handleSubmit ( data ) ;
159- setIsOpen ( false ) ;
160- } }
161- />
93+ < Button type = "button" onClick = { ( ) => void handleSubmit ( ) . then ( ( ) => setIsOpen ( false ) ) } >
94+ Upload
95+ </ Button >
16296 </ Dialog . Body >
16397 </ Dialog . Content >
16498 </ Dialog >
0 commit comments