Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | import { useIsFetching, useIsMutating, useMutationState, useQuery, useQueryClient } from '@tanstack/react-query'; import { keyBy, last } from 'lodash-es'; import { useMemo } from 'react'; import { useIntl } from 'react-intl'; import { useSnackbars } from '@allshares/studio-design-system'; import { useFeedback } from '@amalia/ext/react/hooks'; import { useMutation } from '@amalia/ext/react-query'; import { assert, toError } from '@amalia/ext/typescript'; import { type UpdateUserProfileRequest, type UserProfile } from '@amalia/tenants/users/profile/types'; import { UserApiClient } from '@amalia/tenants/users/state'; import { UserProfileApiClient } from './api-client/user-profile.api-client'; import { userProfileMutationKeys, userProfileQueryKeys } from './queries.keys'; export const useAuthorizedProfiles = (options?: { enabled?: boolean }) => { const { data, ...rest } = useQuery({ queryKey: userProfileQueryKeys.list(), queryFn: () => UserProfileApiClient.getAuthorizedProfiles(), ...options, }); return { ...rest, data: useMemo(() => data ?? [], [data]), }; }; export const useAuthorizedProfilesMap = ({ enabled = true }: { enabled?: boolean } = {}) => { const { data, ...rest } = useAuthorizedProfiles({ enabled }); const usersMap = useMemo(() => keyBy(data, 'id'), [data]); return { ...rest, data: usersMap }; }; export const useUserProfile = (userId?: UserProfile['id']) => useQuery({ queryKey: userProfileQueryKeys.details(userId!), queryFn: () => { // enabled should have made sure userId is defined. assert(userId, 'userId is required'); return UserProfileApiClient.getUserProfile(userId); }, enabled: !!userId, }); export const useUserEmploymentHistory = (userId?: UserProfile['id'], options?: { enabled?: boolean }) => useQuery({ queryKey: userProfileQueryKeys.editionHistory(userId!), queryFn: () => { assert(userId, 'userId is required'); return UserProfileApiClient.getEditionHistory(userId); }, enabled: !!userId && (options?.enabled ?? true), }); export const useUpdateUserInfo = ( userId: UserProfile['id'], { shouldInviteToReload = () => false }: { shouldInviteToReload?: (updatedUser: UserProfile) => boolean } = {}, ) => { const { formatMessage } = useIntl(); return useMutation({ mutationKey: userProfileMutationKeys.updateUserInfo(userId), mutationFn: (user: UpdateUserProfileRequest) => UserProfileApiClient.updateUserInfo(user), meta: { successMessage: (updatedUser) => shouldInviteToReload(updatedUser) ? formatMessage({ defaultMessage: 'Your language has been successfully updated. Please refresh the page to apply the changes.', }) : formatMessage( { defaultMessage: "{firstName} {lastName}'s information has been successfully updated." }, { firstName: updatedUser.firstName, lastName: updatedUser.lastName }, ), invalidateQueries: [userProfileQueryKeys.all()], errorMessage: (e) => toError(e).message, }, }); }; export const useToggleUserDeactivation = (userId: UserProfile['id']) => { const queryClient = useQueryClient(); const { formatMessage } = useIntl(); const { snackSuccess, snackError } = useSnackbars(); return useMutation({ mutationKey: userProfileMutationKeys.toggleDeactivation(userId), mutationFn: () => UserProfileApiClient.toggleUserDeactivation(userId), onSuccess: async (updatedUser: UserProfile) => { if (updatedUser.clearedAt) { snackSuccess( formatMessage( { defaultMessage: '{firstName} {lastName} has been successfully deactivated.' }, { firstName: updatedUser.firstName, lastName: updatedUser.lastName, }, ), ); } else { snackSuccess( formatMessage( { defaultMessage: '{firstName} {lastName} has been successfully reactivated.' }, { firstName: updatedUser.firstName, lastName: updatedUser.lastName, }, ), ); } await queryClient.invalidateQueries({ queryKey: userProfileQueryKeys.all() }); }, onError: (e) => { snackError(toError(e).message); }, }); }; export const useInviteUser = (user: Pick<UserProfile, 'firstName' | 'id' | 'lastName'>) => { const queryClient = useQueryClient(); const { snackSuccess, snackError } = useSnackbars(); const { formatMessage } = useIntl(); const [isSuccess, setSuccess] = useFeedback(); const mutation = useMutation({ mutationKey: userProfileMutationKeys.inviteUser(user.id), mutationFn: () => UserProfileApiClient.inviteUser(user.id), onSuccess: async () => { setSuccess(true); snackSuccess( formatMessage( { defaultMessage: 'Invitation has been successfully sent to {firstName} {lastName}' }, { firstName: user.firstName, lastName: user.lastName }, ), ); await queryClient.invalidateQueries({ queryKey: userProfileQueryKeys.all() }); }, onError: (e) => { snackError( formatMessage( { defaultMessage: 'Invitation could not be sent to {firstName} {lastName} due to: {message}. Please try again', }, { firstName: user.firstName, lastName: user.lastName, message: toError(e).message }, ), ); }, }); return { ...mutation, isSuccess }; }; export const useInviteUsers = (userIds: string[]) => { const queryClient = useQueryClient(); const { snackSuccess } = useSnackbars(); const { formatMessage } = useIntl(); const [isSuccess, setSuccess] = useFeedback(); const mutation = useMutation({ mutationKey: userProfileMutationKeys.inviteUsers(userIds), mutationFn: () => UserProfileApiClient.inviteUsers(userIds), onSuccess: async () => { setSuccess(true); snackSuccess( formatMessage( { defaultMessage: 'Invitation has been successfully sent to {count, plural, one {# user} other {# users}}', }, { count: userIds.length }, ), ); await queryClient.invalidateQueries({ queryKey: userProfileQueryKeys.all() }); }, onError: async (error: unknown) => { await queryClient.invalidateQueries({ queryKey: userProfileQueryKeys.all() }); if (!UserProfileApiClient.isBulkInvitationError(error)) { return; } const { errors } = error.response.data; snackSuccess( formatMessage( { defaultMessage: 'Invitation has been successfully sent to {count, plural, one {# user} other {# users}}', }, { count: userIds.length - errors.length }, ), ); }, }); return { ...mutation, isSuccess }; }; export const useInviteUsersState = () => last( useMutationState({ filters: { mutationKey: userProfileMutationKeys.anyInviteUsers() }, select: (mutation) => mutation.state, }).toSorted((a, b) => /* ASC */ a.submittedAt - b.submittedAt), ); export const useIsUpdatingUserInfo = () => useIsMutating({ mutationKey: userProfileMutationKeys.all() }) > 0; export const useIsFetchingUserProfile = (userId?: UserProfile['id']) => useIsFetching({ queryKey: userId ? userProfileQueryKeys.details(userId) : userProfileQueryKeys.all() }) > 0; export const useSyncUsers = () => { const { formatMessage } = useIntl(); return useMutation({ mutationKey: userProfileMutationKeys.sync(), mutationFn: UserApiClient.bulkSyncUsers, meta: { successMessage: formatMessage({ defaultMessage: 'Users have been created successfully.' }), invalidateQueries: [userProfileQueryKeys.all()], errorMessage: (err) => formatMessage({ defaultMessage: 'Users could not be created: {errorMessage}.' }, { errorMessage: err.message }), }, }); }; |