diff --git a/frontend/src/pages/User/Edit/index.tsx b/frontend/src/pages/User/Edit/index.tsx index 6d0379362..4671a861f 100644 --- a/frontend/src/pages/User/Edit/index.tsx +++ b/frontend/src/pages/User/Edit/index.tsx @@ -5,11 +5,11 @@ import { pick } from 'lodash'; import { Box, ConfirmationDialog, Container, ContentLayout, Header, Loader } from 'components'; -import { useAppSelector, useBreadcrumbs, useNotifications } from 'hooks'; +import { useAppDispatch, useAppSelector, useBreadcrumbs, useNotifications } from 'hooks'; import { ROUTES } from 'routes'; import { useGetUserQuery, useRefreshTokenMutation, useUpdateUserMutation } from 'services/user'; -import { selectUserData } from 'App/slice'; +import { selectUserData, setAuthData } from 'App/slice'; import { UserForm } from '../Form'; @@ -20,8 +20,9 @@ export const UserEdit: React.FC = () => { const userData = useAppSelector(selectUserData); const userGlobalRole = userData?.global_role ?? ''; const paramUserName = params.userName ?? ''; + const dispatch = useAppDispatch(); const [showRefreshConfirm, setShowRefreshConfirm] = useState(false); - const { isLoading, data } = useGetUserQuery({ name: paramUserName }, { skip: !params.userName }); + const { isLoading, data } = useGetUserQuery({ name: paramUserName }, { skip: !paramUserName }); const [updateUser, { isLoading: isUserUpdating }] = useUpdateUserMutation(); const [refreshToken, { isLoading: isTokenRefreshing }] = useRefreshTokenMutation(); @@ -56,7 +57,13 @@ export const UserEdit: React.FC = () => { try { await refreshToken({ username: paramUserName, - }).unwrap(); + }) + .unwrap() + .then(({ creds: { token } }) => { + if (paramUserName === userData?.username) { + dispatch(setAuthData({ token })); + } + }); pushNotification({ type: 'success', diff --git a/frontend/src/services/user.ts b/frontend/src/services/user.ts index af3a65962..001d855c1 100644 --- a/frontend/src/services/user.ts +++ b/frontend/src/services/user.ts @@ -101,7 +101,21 @@ export const userApi = createApi({ body: { username }, }), - invalidatesTags: (result, error, { username }) => [{ type: 'User' as const, id: username }], + // invalidatesTags: (result, error, { username }) => [{ type: 'User' as const, id: username }], + + async onQueryStarted({ username }, { dispatch, queryFulfilled }) { + try { + const { data } = await queryFulfilled; + + dispatch( + userApi.util.updateQueryData('getUser', { name: username }, (draft) => { + Object.assign(draft, data); + }), + ); + } catch (e) { + console.log(e); + } + }, }), deleteUsers: builder.mutation({