1
1
<script setup lang="ts">
2
2
import { computed , ref , watch } from ' vue' ;
3
3
4
+ import { useQueryClient } from ' @tanstack/vue-query' ;
5
+
4
6
import {
5
7
PButtonModal , PScopedNotification ,
6
8
} from ' @cloudforet/mirinae' ;
7
9
8
10
import { MFA_STATE } from ' @/api-clients/identity/user-profile/schema/constant' ;
9
11
import type { MultiFactorAuthType } from ' @/api-clients/identity/user-profile/schema/type' ;
12
+ import { useUserApi } from ' @/api-clients/identity/user/composables/use-user-api' ;
10
13
import type { UserUpdateParameters } from ' @/api-clients/identity/user/schema/api-verbs/update' ;
14
+ import { useServiceQueryKey } from ' @/query/core/query-key/use-service-query-key' ;
11
15
import { i18n } from ' @/translations' ;
12
16
13
17
import { useUserStore } from ' @/store/user/user-store' ;
@@ -17,7 +21,6 @@ import { showErrorMessage, showSuccessMessage } from '@/lib/helper/notice-alert-
17
21
import ErrorHandler from ' @/common/composables/error/errorHandler' ;
18
22
19
23
import UserMFASettingFormLayout from ' @/services/iam/components/mfa/UserMFASettingFormLayout.vue' ;
20
- import { useUserUpdateMutation } from ' @/services/iam/composables/mutations/use-user-update-mutation' ;
21
24
import { useUserListQuery } from ' @/services/iam/composables/use-user-list-query' ;
22
25
import { USER_MODAL_MAP } from ' @/services/iam/constants/modal.constant' ;
23
26
import { MULTI_FACTOR_AUTH_ITEMS } from ' @/services/iam/constants/user-constant' ;
@@ -56,13 +59,21 @@ const isIncludedExternalAuthTypeUser = computed<boolean>(() => selectedUsers.val
56
59
// Store failed user IDs
57
60
const failedUserIds = new Set <string >();
58
61
59
- const { mutateAsync : updateUser, isPending : isUpdateUserPending } = useUserUpdateMutation ({
60
- onError : (error : any , variables : UserUpdateParameters ) => {
61
- // Store failed user IDs for logging failed users
62
- failedUserIds .add (variables .user_id );
62
+ const queryClient = useQueryClient ();
63
+ const { key : userListKey } = useServiceQueryKey (' identity' , ' user' , ' list' );
64
+
65
+ const { userAPI } = useUserApi ();
66
+ const isUpdateUserPending = ref (false );
67
+
68
+ const updateUser = async (params : UserUpdateParameters ) => {
69
+ try {
70
+ const result = await userAPI .update (params );
71
+ return result ;
72
+ } catch (error : any ) {
73
+ failedUserIds .add (params .user_id );
63
74
throw new Error (error .message );
64
- },
65
- }) ;
75
+ }
76
+ };
66
77
67
78
/* Utils */
68
79
const closeModal = () => {
@@ -82,6 +93,8 @@ const handleClose = () => {
82
93
const handleConfirm = async () => {
83
94
if (isUpdateUserPending .value ) return ;
84
95
96
+ isUpdateUserPending .value = true ;
97
+
85
98
// Update MFA Promise for each user (Bulk)
86
99
const userUpdatePromises = selectedMFAControllableUsers .value .map (async (user ) => {
87
100
if (! user .user_id ) {
@@ -95,21 +108,29 @@ const handleConfirm = async () => {
95
108
else throw new Error (' [User MFA Setting] Something went wrong! Try again later. If the problem persists, please contact support.' );
96
109
}
97
110
98
- return updateUser ( {
111
+ const updateParams : UserUpdateParameters = {
99
112
user_id: user .user_id ,
100
113
enforce_mfa_state: isRequiredMfa .value ? MFA_STATE .ENABLED : MFA_STATE .DISABLED ,
101
- enforce_mfa_type: selectedMfaType .value ,
102
- });
114
+ };
115
+
116
+ if (isRequiredMfa .value ) {
117
+ updateParams .enforce_mfa_type = selectedMfaType .value ;
118
+ }
119
+
120
+ return updateUser (updateParams );
103
121
});
104
122
105
123
if (userUpdatePromises .length === 0 ) {
106
124
showErrorMessage (' [User MFA Setting] There are no users to update.' , ' ' );
125
+ isUpdateUserPending .value = false ;
107
126
return ;
108
127
}
109
128
110
129
try {
111
130
const results = await Promise .allSettled (userUpdatePromises );
112
131
if (results .every ((result ) => result .status === ' fulfilled' )) {
132
+ await queryClient .invalidateQueries ({ queryKey: userListKey .value });
133
+
113
134
showSuccessMessage (i18n .t (' IAM.USER.MAIN.MODAL.MFA.SET_MFA_SUCCESS_MESSAGE' ), ' ' );
114
135
closeModal ();
115
136
emit (' confirm' );
@@ -125,6 +146,7 @@ const handleConfirm = async () => {
125
146
} finally {
126
147
// Clear failed user IDs
127
148
failedUserIds .clear ();
149
+ isUpdateUserPending .value = false ;
128
150
}
129
151
};
130
152
@@ -134,6 +156,12 @@ watch(() => userStore.state.mfa, (mfa) => {
134
156
selectedMfaType .value = mfa .mfa_type || MULTI_FACTOR_AUTH_ITEMS [0 ].type ;
135
157
}
136
158
}, { immediate: true });
159
+
160
+ watch (() => userPageModalState .bulkMfaSettingModalVisible , async (visible ) => {
161
+ if (visible ) {
162
+ await userStore .getUserInfo ();
163
+ }
164
+ });
137
165
</script >
138
166
139
167
<template >
0 commit comments