@@ -14,11 +14,14 @@ import {
14
14
fetchUser ,
15
15
updateEmail as updateEmailInAtlas ,
16
16
confirmEmail as confirmEmailInAtlas ,
17
+ uploadToIpfs ,
17
18
type User ,
18
19
type AddUserData ,
19
20
type UpdateEmailData ,
20
21
type ConfirmEmailData ,
21
22
type ConfirmEmailResponse ,
23
+ Roles ,
24
+ Products ,
22
25
} from "utils/atlas" ;
23
26
24
27
import { isUndefined } from "src/utils" ;
@@ -29,11 +32,13 @@ interface IAtlasProvider {
29
32
isAddingUser : boolean ;
30
33
isFetchingUser : boolean ;
31
34
isUpdatingUser : boolean ;
35
+ isUploadingFile : boolean ;
32
36
user : User | undefined ;
33
37
userExists : boolean ;
34
38
authoriseUser : ( ) => void ;
35
39
addUser : ( userSettings : AddUserData ) => Promise < boolean > ;
36
40
updateEmail : ( userSettings : UpdateEmailData ) => Promise < boolean > ;
41
+ uploadFile : ( file : File , role : Roles ) => Promise < string | null > ;
37
42
confirmEmail : ( userSettings : ConfirmEmailData ) => Promise <
38
43
ConfirmEmailResponse & {
39
44
isError : boolean ;
@@ -57,6 +62,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
57
62
const [ isAddingUser , setIsAddingUser ] = useState ( false ) ;
58
63
const [ isUpdatingUser , setIsUpdatingUser ] = useState ( false ) ;
59
64
const [ isVerified , setIsVerified ] = useState ( false ) ;
65
+ const [ isUploadingFile , setIsUploadingFile ] = useState ( false ) ;
60
66
const { signMessageAsync } = useSignMessage ( ) ;
61
67
62
68
const atlasGqlClient = useMemo ( ( ) => {
@@ -65,7 +71,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
65
71
authorization : `Bearer ${ authToken } ` ,
66
72
}
67
73
: undefined ;
68
- return new GraphQLClient ( atlasUri , { headers } ) ;
74
+ return new GraphQLClient ( ` ${ atlasUri } /graphql` , { headers } ) ;
69
75
} , [ authToken ] ) ;
70
76
71
77
/**
@@ -131,7 +137,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
131
137
// this would change based on the fields we have and what defines a user to be existing
132
138
const userExists = useMemo ( ( ) => {
133
139
if ( ! user ) return false ;
134
- return user . email ? true : false ;
140
+ return ! isUndefined ( user . email ) ;
135
141
} , [ user ] ) ;
136
142
137
143
/**
@@ -208,6 +214,35 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
208
214
[ address , isVerified , setIsUpdatingUser , atlasGqlClient , refetchUser ]
209
215
) ;
210
216
217
+ /**
218
+ * @description upload file to ipfs
219
+ * @param {File } file - file to be uploaded
220
+ * @param {Roles } role - role for which file is being uploaded
221
+ * @returns {Promise<string | null> } A promise that resolves to the ipfs cid if file was uploaded successfully else
222
+ * null
223
+ */
224
+ const uploadFile = useCallback (
225
+ async ( file : File , role : Roles ) => {
226
+ try {
227
+ if ( ! address || ! isVerified || ! atlasUri || ! authToken ) return null ;
228
+ setIsUploadingFile ( true ) ;
229
+
230
+ const hash = await uploadToIpfs (
231
+ { baseUrl : atlasUri , authToken } ,
232
+ { file, name : file . name , role, product : Products . CourtV2 }
233
+ ) ;
234
+ return hash ? `/ipfs/${ hash } ` : null ;
235
+ } catch ( err : any ) {
236
+ // eslint-disable-next-line
237
+ console . log ( "Upload File Error : " , err ?. message ) ;
238
+ return null ;
239
+ } finally {
240
+ setIsUploadingFile ( false ) ;
241
+ }
242
+ } ,
243
+ [ address , isVerified , setIsUploadingFile , authToken ]
244
+ ) ;
245
+
211
246
/**
212
247
* @description confirms user email in atlas
213
248
* @param {ConfirmEmailData } userSettings - object containing data to be sent
@@ -244,6 +279,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
244
279
updateEmail,
245
280
isUpdatingUser,
246
281
userExists,
282
+ isUploadingFile,
283
+ uploadFile,
247
284
confirmEmail,
248
285
} ) ,
249
286
[
@@ -257,6 +294,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
257
294
updateEmail ,
258
295
isUpdatingUser ,
259
296
userExists ,
297
+ isUploadingFile ,
298
+ uploadFile ,
260
299
confirmEmail ,
261
300
]
262
301
) }
0 commit comments