-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]: get user avatar to have it in user state for components
- Loading branch information
1 parent
1fc45e6
commit 933adf0
Showing
17 changed files
with
193 additions
and
180 deletions.
There are no files selected for viewing
46 changes: 34 additions & 12 deletions
46
expo-boomboomapp/src/api/ProfileApiService/ProfileApiMockService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
import { singleton } from "tsyringe"; | ||
import { AuthApiInterface } from "@swagger/api"; | ||
import { Configuration } from "@swagger/configuration"; | ||
import { buildApiRequester } from "@utils/api.utils"; | ||
import { inject, singleton } from "tsyringe"; | ||
|
||
import { | ||
CreateProfileBody, | ||
EditProfileBody, | ||
ProfileApiServiceI, | ||
} from "./ProfileApiServiceI"; | ||
import { EditProfileBody, ProfileApiServiceI } from "./ProfileApiServiceI"; | ||
import { user_yohan } from "../../mocks/mokes"; | ||
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService"; | ||
import StorageService from "../../services/StorageService/StorageService"; | ||
import ServiceInterface from "../../tsyringe/ServiceInterface"; | ||
import { buildAxiosMockResponse } from "../utils"; | ||
|
||
@singleton() | ||
export class ProfileApiMockService implements ProfileApiServiceI { | ||
async createProfile(createProfileBody: CreateProfileBody) { | ||
return Promise.resolve(user_yohan); | ||
export class ProfileApiMockService | ||
extends ProfileApiServiceI | ||
implements AuthApiInterface | ||
{ | ||
constructor( | ||
@inject(ServiceInterface.ConfigurationService) | ||
protected configurationService: ConfigurationService, | ||
@inject(ServiceInterface.StorageServiceI) | ||
protected storageService: StorageService, | ||
) { | ||
const baseUrl = configurationService.getWiremockApiUrl(); | ||
super(new Configuration(), baseUrl, buildApiRequester(baseUrl), () => | ||
this.storageService.getAuthenticateToken(), | ||
); | ||
} | ||
|
||
async getProfile() { | ||
return Promise.resolve(user_yohan); | ||
override createProfile() { | ||
return Promise.resolve(buildAxiosMockResponse(user_yohan)); | ||
} | ||
|
||
override getProfile() { | ||
return Promise.resolve(buildAxiosMockResponse(user_yohan)); | ||
} | ||
|
||
async editProfile(editedProfileBody: EditProfileBody) { | ||
return Promise.resolve({ ...user_yohan, ...editedProfileBody }); | ||
} | ||
|
||
async uploadAvatar(uri: string): Promise<void> {} | ||
async uploadAvatarByUri(uri: string): Promise<void> {} | ||
|
||
async getBlobedAvatar() { | ||
return Promise.resolve(require("@assets/mokes/yohan.png")); | ||
} | ||
} |
73 changes: 12 additions & 61 deletions
73
expo-boomboomapp/src/api/ProfileApiService/ProfileApiService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,31 @@ | ||
import * as FileSystem from "expo-file-system"; | ||
import { AuthApiInterface } from "@swagger/api"; | ||
import { Configuration } from "@swagger/configuration"; | ||
import { buildApiRequester } from "@utils/api.utils"; | ||
import { inject, singleton } from "tsyringe"; | ||
|
||
import { | ||
CreateProfileBody, | ||
EditProfileBody, | ||
ProfileApiServiceI, | ||
ProfileI, | ||
} from "./ProfileApiServiceI"; | ||
import { EditProfileBody, ProfileApiServiceI } from "./ProfileApiServiceI"; | ||
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService"; | ||
import ErrorService from "../../services/ErrorService/ErrorService"; | ||
import StorageService from "../../services/StorageService/StorageService"; | ||
import ServiceInterface from "../../tsyringe/ServiceInterface"; | ||
import { ApiService } from "../ApiService"; | ||
|
||
@singleton() | ||
export class ProfileApiService | ||
extends ApiService | ||
implements ProfileApiServiceI | ||
extends ProfileApiServiceI | ||
implements AuthApiInterface | ||
{ | ||
constructor( | ||
@inject(ServiceInterface.StorageServiceI) | ||
protected storageService: StorageService, | ||
@inject(ServiceInterface.ConfigurationService) | ||
protected configurationService: ConfigurationService, | ||
@inject(ServiceInterface.ErrorService) | ||
protected errorService: ErrorService, | ||
@inject(ServiceInterface.StorageServiceI) | ||
protected storageService: StorageService, | ||
) { | ||
super("auth/profile", storageService, configurationService, errorService); | ||
} | ||
|
||
async createProfile(createProfileBody: CreateProfileBody) { | ||
const res = await this.apiRequester.post<ProfileI>("/", createProfileBody); | ||
return res.data; | ||
} | ||
|
||
async getProfile() { | ||
const res = await this.apiRequester.get<ProfileI>("/"); | ||
return res.data; | ||
const baseUrl = configurationService.getApiUrl().replace("/api", ""); | ||
super(new Configuration(), baseUrl, buildApiRequester(baseUrl), () => | ||
this.storageService.getAuthenticateToken(), | ||
); | ||
} | ||
|
||
async editProfile(editedProfileBody: EditProfileBody) { | ||
// TODO url | ||
const res = await this.apiRequester.put<ProfileI>( | ||
"/TODO", | ||
editedProfileBody, | ||
); | ||
return res.data; | ||
} | ||
|
||
async uploadAvatar(uri: string): Promise<void> { | ||
try { | ||
const response = await FileSystem.uploadAsync( | ||
`${this.apiRequester.getUri()}/avatar`, | ||
uri, | ||
{ | ||
httpMethod: "POST", | ||
fieldName: "avatar", | ||
uploadType: FileSystem.FileSystemUploadType.MULTIPART, | ||
headers: { | ||
Authorization: `Bearer ${await this.storageService.getAuthenticateToken()}`, | ||
}, | ||
}, | ||
); | ||
this.handleFileSystemUploadAsyncResponse(response); | ||
} catch (error) { | ||
// TODO handle error | ||
console.error("Error uploading image:", error); | ||
} | ||
} | ||
|
||
private handleFileSystemUploadAsyncResponse( | ||
response: FileSystem.FileSystemUploadResult, | ||
) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return response; | ||
} | ||
throw response.body; | ||
} | ||
} |
83 changes: 53 additions & 30 deletions
83
expo-boomboomapp/src/api/ProfileApiService/ProfileApiServiceI.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,59 @@ | ||
import { Gender } from "../../services/UserService/userServiceI"; | ||
import { AuthApi, AuthApiInterface, CreateProfileRequest } from "@swagger/api"; | ||
import { Configuration } from "@swagger/configuration"; | ||
import { AxiosInstance } from "axios/index"; | ||
import * as FileSystem from "expo-file-system"; | ||
|
||
export abstract class ProfileI { | ||
abstract user_id: string; | ||
abstract date_of_birth: string; | ||
abstract description: string; | ||
abstract avatar: string; | ||
abstract prefered_gender_id: Gender; | ||
abstract userId: string; | ||
// TODO name & trackIds is not send my backend in getProfile endpoint | ||
abstract name: string; | ||
abstract trackIds: string[]; | ||
} | ||
|
||
export type CreateProfileBody = { | ||
dateOfBirth: string; | ||
description: string; | ||
preferedGenderId: Gender; | ||
genderId: Gender; | ||
trackIds: string[]; | ||
name: string; | ||
}; | ||
|
||
export type EditProfileBody = Partial<CreateProfileBody>; | ||
export type EditProfileBody = Partial<CreateProfileRequest>; | ||
|
||
export abstract class ProfileApiServiceI { | ||
abstract createProfile( | ||
createProfileBody: CreateProfileBody, | ||
): Promise<ProfileI>; | ||
export abstract class ProfileApiServiceI | ||
extends AuthApi | ||
implements AuthApiInterface | ||
{ | ||
constructor( | ||
configuration: Configuration, | ||
basePath: string, | ||
axios: AxiosInstance, | ||
private authTokenGetter: () => Promise<string | null>, | ||
) { | ||
super(configuration, basePath, axios); | ||
} | ||
|
||
abstract getProfile(): Promise<ProfileI>; | ||
async uploadAvatarByUri(uri: string): Promise<void> { | ||
try { | ||
const response = await FileSystem.uploadAsync( | ||
`${this.basePath}/api/auth/profile/avatar`, | ||
uri, | ||
{ | ||
httpMethod: "POST", | ||
fieldName: "avatar", | ||
uploadType: FileSystem.FileSystemUploadType.MULTIPART, | ||
headers: { | ||
Authorization: `Bearer ${await this.authTokenGetter()}`, | ||
}, | ||
}, | ||
); | ||
this.handleFileSystemUploadAsyncResponse(response); | ||
} catch (error) { | ||
// TODO handle error | ||
console.error("Error uploading image:", error); | ||
} | ||
} | ||
|
||
abstract editProfile(editedProfileBody: EditProfileBody): Promise<ProfileI>; | ||
private handleFileSystemUploadAsyncResponse( | ||
response: FileSystem.FileSystemUploadResult, | ||
) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return response; | ||
} | ||
throw response.body; | ||
} | ||
|
||
abstract uploadAvatar(uri: string): Promise<void>; | ||
async getBlobedAvatar() { | ||
const response = await this.getAvatar({ responseType: "blob" }); | ||
const blob = new Blob([response.data]); | ||
// localUrl is redundant, | ||
// but funny thing, if we use a direct return the image will not load on the component | ||
const localUrl = URL.createObjectURL(blob); | ||
return localUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InternalAxiosRequestConfig } from "axios"; | ||
|
||
export function buildAxiosMockResponse<T>(data: T) { | ||
return { | ||
data, | ||
status: 200, | ||
statusText: "statusText", | ||
headers: {}, | ||
config: {} as InternalAxiosRequestConfig<any>, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.