@oxyhq/services
Version:
107 lines (103 loc) • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useProfileEditing = void 0;
var _react = require("react");
var _useI18n = require("./useI18n.js");
var _useAccountMutations = require("./mutations/useAccountMutations.js");
var _authStore = require("../stores/authStore.js");
/**
* Hook for managing profile editing operations
* Provides functions to update profile fields and handle saving
*/
const useProfileEditing = () => {
const {
t
} = (0, _useI18n.useI18n)();
const updateProfileMutation = (0, _useAccountMutations.useUpdateProfile)();
/**
* Save profile updates to the server using TanStack Query
*/
const saveProfile = (0, _react.useCallback)(async updates => {
// Prepare update object
const updateData = {};
if (updates.username !== undefined) {
updateData.username = updates.username;
}
if (updates.email !== undefined) {
updateData.email = updates.email;
}
if (updates.bio !== undefined) {
updateData.bio = updates.bio;
}
if (updates.location !== undefined || updates.locations !== undefined) {
updateData.location = updates.locations && updates.locations.length > 0 ? updates.locations[0].name : updates.location || '';
if (updates.locations) {
updateData.locations = updates.locations;
}
}
if (updates.links !== undefined) {
updateData.links = updates.links;
}
if (updates.linksMetadata !== undefined) {
updateData.linksMetadata = updates.linksMetadata;
}
if (updates.avatar !== undefined) {
updateData.avatar = updates.avatar;
}
// Handle name field
if (updates.displayName !== undefined || updates.lastName !== undefined) {
const currentUser = _authStore.useAuthStore.getState().user;
const currentName = currentUser?.name;
updateData.name = {
first: updates.displayName ?? (typeof currentName === 'object' ? currentName?.first : '') ?? '',
last: updates.lastName ?? (typeof currentName === 'object' ? currentName?.last : '') ?? ''
};
}
try {
await updateProfileMutation.mutateAsync(updateData);
return true;
} catch (error) {
// Error toast is handled by the mutation
return false;
}
}, [updateProfileMutation, t]);
/**
* Update a single profile field
*/
const updateField = (0, _react.useCallback)(async (field, value) => {
const updates = {};
switch (field) {
case 'displayName':
updates.displayName = value;
break;
case 'username':
updates.username = value;
break;
case 'email':
updates.email = value;
break;
case 'bio':
updates.bio = value;
break;
case 'location':
updates.locations = value;
break;
case 'links':
updates.linksMetadata = value;
updates.links = value.map(link => link.url || link);
break;
default:
return false;
}
return await saveProfile(updates);
}, [saveProfile]);
return {
saveProfile,
updateField,
isSaving: updateProfileMutation.isPending
};
};
exports.useProfileEditing = useProfileEditing;
//# sourceMappingURL=useProfileEditing.js.map