@@ -14,11 +14,14 @@ import {
1414 fetchUser ,
1515 updateEmail as updateEmailInAtlas ,
1616 confirmEmail as confirmEmailInAtlas ,
17+ uploadToIpfs ,
1718 type User ,
1819 type AddUserData ,
1920 type UpdateEmailData ,
2021 type ConfirmEmailData ,
2122 type ConfirmEmailResponse ,
23+ Roles ,
24+ Products ,
2225} from "utils/atlas" ;
2326
2427import { isUndefined } from "src/utils" ;
@@ -29,11 +32,13 @@ interface IAtlasProvider {
2932 isAddingUser : boolean ;
3033 isFetchingUser : boolean ;
3134 isUpdatingUser : boolean ;
35+ isUploadingFile : boolean ;
3236 user : User | undefined ;
3337 userExists : boolean ;
3438 authoriseUser : ( ) => void ;
3539 addUser : ( userSettings : AddUserData ) => Promise < boolean > ;
3640 updateEmail : ( userSettings : UpdateEmailData ) => Promise < boolean > ;
41+ uploadFile : ( file : File , role : Roles ) => Promise < string | null > ;
3742 confirmEmail : ( userSettings : ConfirmEmailData ) => Promise <
3843 ConfirmEmailResponse & {
3944 isError : boolean ;
@@ -57,6 +62,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
5762 const [ isAddingUser , setIsAddingUser ] = useState ( false ) ;
5863 const [ isUpdatingUser , setIsUpdatingUser ] = useState ( false ) ;
5964 const [ isVerified , setIsVerified ] = useState ( false ) ;
65+ const [ isUploadingFile , setIsUploadingFile ] = useState ( false ) ;
6066 const { signMessageAsync } = useSignMessage ( ) ;
6167
6268 const atlasGqlClient = useMemo ( ( ) => {
@@ -65,7 +71,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
6571 authorization : `Bearer ${ authToken } ` ,
6672 }
6773 : undefined ;
68- return new GraphQLClient ( atlasUri , { headers } ) ;
74+ return new GraphQLClient ( ` ${ atlasUri } /graphql` , { headers } ) ;
6975 } , [ authToken ] ) ;
7076
7177 /**
@@ -131,7 +137,7 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
131137 // this would change based on the fields we have and what defines a user to be existing
132138 const userExists = useMemo ( ( ) => {
133139 if ( ! user ) return false ;
134- return user . email ? true : false ;
140+ return ! isUndefined ( user . email ) ;
135141 } , [ user ] ) ;
136142
137143 /**
@@ -208,6 +214,35 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
208214 [ address , isVerified , setIsUpdatingUser , atlasGqlClient , refetchUser ]
209215 ) ;
210216
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+
211246 /**
212247 * @description confirms user email in atlas
213248 * @param {ConfirmEmailData } userSettings - object containing data to be sent
@@ -244,6 +279,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
244279 updateEmail,
245280 isUpdatingUser,
246281 userExists,
282+ isUploadingFile,
283+ uploadFile,
247284 confirmEmail,
248285 } ) ,
249286 [
@@ -257,6 +294,8 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
257294 updateEmail ,
258295 isUpdatingUser ,
259296 userExists ,
297+ isUploadingFile ,
298+ uploadFile ,
260299 confirmEmail ,
261300 ]
262301 ) }
0 commit comments