diff --git a/app/composables/api/profile/useProfile.api.ts b/app/composables/api/profile/useProfile.api.ts index 0712a7f74..4b1b9185c 100644 --- a/app/composables/api/profile/useProfile.api.ts +++ b/app/composables/api/profile/useProfile.api.ts @@ -4,6 +4,7 @@ import type { ProfileDTO, EditProfileDTO, DeleteProfileDTO, + ChangePasswordDTO, } from '@/types' const loadingGetItemById = ref(false) @@ -11,11 +12,13 @@ const loadingEditItem = ref(false) const loadingDeleteItem = ref(false) const loadingCancelDeleteItem = ref(false) const loadingChangeGroup = ref(false) +const loadingChangePassword = ref(false) const NAME = 'Profile' export const useProfile = () => { const { $toast } = useNuxtApp() const auth = useAuth() + const { handleApiResponseError, handleApiCatchError, createApiFailure } = useApiErrorHandler() const getItemById = async (id: string) => { loadingGetItemById.value = true @@ -304,7 +307,33 @@ export const useProfile = () => { } } + const changePassword = async (item: ChangePasswordDTO) => { + try { + loadingChangePassword.value = true + const response = await useApiService.put< + ApiResult + >('/api/v1/users/password', { ...item }) + + if (response.status == 1) { + $toast.success('Password changed successfully') + } + else { + handleApiResponseError(response) + } + + return response + } + catch (err: unknown) { + handleApiCatchError(err) + + return createApiFailure(err, false) + } + finally { + loadingChangePassword.value = false + } + } + return { - getItemById, loadingGetItemById, editItem, loadingEditItem, deleteItem, loadingDeleteItem, cancelDeleteItem, loadingCancelDeleteItem, changeGroup, loadingChangeGroup, + getItemById, loadingGetItemById, editItem, loadingEditItem, deleteItem, loadingDeleteItem, cancelDeleteItem, loadingCancelDeleteItem, changeGroup, loadingChangeGroup, changePassword, loadingChangePassword, } } diff --git a/app/composables/useValidationRules.ts b/app/composables/useValidationRules.ts index f32eaf089..e403abbac 100644 --- a/app/composables/useValidationRules.ts +++ b/app/composables/useValidationRules.ts @@ -107,11 +107,11 @@ export const useValidationRules = () => { || 'Password must contain uppercase, lowercase, number and special character' // Confirmation validation - const matches = (target: Ref | string, fieldName = 'field') => + const matches = (target: Ref | string, fieldName = 'field', message?: string) => (v: string) => { const targetValue = typeof target === 'string' ? target : target.value - return v === targetValue || `Must match ${fieldName}` + return v === targetValue || message || `Must match ${fieldName}` } // Array validation diff --git a/app/pages/user/edit-pass.vue b/app/pages/user/edit-pass.vue index f6a6dfe0d..349c8feb7 100644 --- a/app/pages/user/edit-pass.vue +++ b/app/pages/user/edit-pass.vue @@ -3,7 +3,7 @@ Submit @@ -146,17 +146,6 @@ diff --git a/app/pages/user/profile/index.vue b/app/pages/user/profile/index.vue index 5b29ea386..414e387c7 100644 --- a/app/pages/user/profile/index.vue +++ b/app/pages/user/profile/index.vue @@ -1,313 +1,357 @@