refactor: simplify user info update logic by removing unnecessary currentProfile checks and streamline profile updates in ProfileApi
This commit is contained in:
@@ -36,16 +36,10 @@ export class ProfileApi extends BaseApiWithErrors {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateUserInfo(
|
private async updateUserInfo(fields: TProfileUpdateForm, avatarUrl?: string): Promise<void> {
|
||||||
fields: TProfileUpdateForm,
|
|
||||||
currentProfile: TProfile,
|
|
||||||
avatarUrl?: string
|
|
||||||
): Promise<void> {
|
|
||||||
const userUpdates: { name?: string; image?: string } = {};
|
const userUpdates: { name?: string; image?: string } = {};
|
||||||
|
|
||||||
if (fields.fullname !== currentProfile.fullname) {
|
userUpdates.name = fields.fullname;
|
||||||
userUpdates.name = fields.fullname;
|
|
||||||
}
|
|
||||||
if (avatarUrl) {
|
if (avatarUrl) {
|
||||||
userUpdates.image = avatarUrl;
|
userUpdates.image = avatarUrl;
|
||||||
}
|
}
|
||||||
@@ -58,23 +52,16 @@ export class ProfileApi extends BaseApiWithErrors {
|
|||||||
|
|
||||||
private buildProfileUpdates(
|
private buildProfileUpdates(
|
||||||
fields: TProfileUpdateForm,
|
fields: TProfileUpdateForm,
|
||||||
currentProfile: TProfile,
|
|
||||||
locale: TLocale | undefined,
|
locale: TLocale | undefined,
|
||||||
avatarUrl?: string
|
avatarUrl?: string
|
||||||
): Partial<TProfile> {
|
): Partial<TProfile> {
|
||||||
const profileUpdates: Partial<TProfile> = {};
|
const profileUpdates: Partial<TProfile> = {};
|
||||||
|
|
||||||
if (fields.fullname !== currentProfile.fullname) {
|
profileUpdates.fullname = fields.fullname;
|
||||||
profileUpdates.fullname = fields.fullname;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fields.username !== currentProfile.username) {
|
profileUpdates.username = fields.username;
|
||||||
profileUpdates.username = fields.username;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (locale && locale !== currentProfile.locale) {
|
profileUpdates.locale = locale;
|
||||||
profileUpdates.locale = locale;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (avatarUrl) {
|
if (avatarUrl) {
|
||||||
profileUpdates.avatarUrl = avatarUrl;
|
profileUpdates.avatarUrl = avatarUrl;
|
||||||
@@ -113,7 +100,7 @@ export class ProfileApi extends BaseApiWithErrors {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async submit(fields: TProfileUpdateForm, currentProfile: TProfile, hasLangChanged: boolean, locale?: TLocale) {
|
async submit(fields: TProfileUpdateForm, hasLangChanged: boolean, locale?: TLocale) {
|
||||||
if (!this.validateForm(fields)) {
|
if (!this.validateForm(fields)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -130,10 +117,10 @@ export class ProfileApi extends BaseApiWithErrors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update user info via Better Auth (name and image)
|
// Update user info via Better Auth (name and image)
|
||||||
await this.updateUserInfo(fields, currentProfile, avatarUrl);
|
await this.updateUserInfo(fields, avatarUrl);
|
||||||
|
|
||||||
// Update profile-specific fields via API route
|
// Update profile-specific fields via API route
|
||||||
const profileUpdates = this.buildProfileUpdates(fields, currentProfile, locale, avatarUrl);
|
const profileUpdates = this.buildProfileUpdates(fields, locale, avatarUrl);
|
||||||
await this.updateProfile(profileUpdates);
|
await this.updateProfile(profileUpdates);
|
||||||
|
|
||||||
// Handle locale change if needed
|
// Handle locale change if needed
|
||||||
|
|||||||
@@ -27,7 +27,6 @@
|
|||||||
locale,
|
locale,
|
||||||
avatar
|
avatar
|
||||||
},
|
},
|
||||||
$profile,
|
|
||||||
hasLangChanged,
|
hasLangChanged,
|
||||||
locale
|
locale
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user