Добавлено обновление данных пользователя, если они отсутствуют в хранилище

This commit is contained in:
Александр Сироткин 2022-10-24 12:18:38 +05:00
parent 0797c20ff4
commit 44543fec31
4 changed files with 22 additions and 17 deletions

View File

@ -23,7 +23,8 @@ const Cluster = lazy(() => import('@pages/Cluster'))
const Well = lazy(() => import('@asb/pages/Well'))
//OpenAPI.BASE = 'http://localhost:3000'
OpenAPI.TOKEN = async () => getUser().token ?? ''
// TODO: Удалить взятие из 'token' в следующем релизе, вставлено для совместимости
OpenAPI.TOKEN = async () => getUser().token || localStorage.getItem('token') || ''
OpenAPI.HEADERS = { 'Content-Type': 'application/json' }
export const App = memo(() => (

View File

@ -2,9 +2,9 @@ import { memo, useCallback, useMemo, useState } from 'react'
import { Rule } from 'antd/lib/form'
import { Form, Input, Modal, FormProps } from 'antd'
import { useUser } from '@asb/context'
import { AuthService, UserDto } from '@api'
import { passwordRules, createPasswordRules } from '@utils/validationRules'
import { getUser } from '@utils'
import LoaderPortal from './LoaderPortal'
import { invokeWebApiWrapperAsync } from './factory'
@ -31,7 +31,8 @@ export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCanc
const [showLoader, setShowLoader] = useState<boolean>(false)
const [isDisabled, setIsDisabled] = useState(true)
const userData = useMemo(() => user ?? getUser(), [user])
const userContext = useUser()
const userData = useMemo(() => user ?? userContext, [user])
const [form] = Form.useForm()

View File

@ -1,11 +1,24 @@
import { memo, useMemo } from 'react'
import { memo, useEffect, useState } from 'react'
import { Outlet } from 'react-router-dom'
import { UserContext } from '@asb/context'
import { getUser } from '@utils'
import { invokeWebApiWrapperAsync } from '@components/factory'
import { getUser, setUser as setStorageUser } from '@utils'
import { AuthService, UserTokenDto } from '@api'
export const UserOutlet = memo(() => {
const user = useMemo(() => getUser() ?? {}, [])
const [user, setUser] = useState<UserTokenDto>({})
useEffect(() => {
invokeWebApiWrapperAsync(async () => {
let user = getUser()
if (!user.id) {
user = await AuthService.refresh()
setStorageUser(user)
}
setUser(user)
})
}, [])
return (
<UserContext.Provider value={user}>

View File

@ -42,15 +42,7 @@ export const setJSON = <T,>(name: StorageNames, data: T | null): boolean => {
return false
}
export const getUser = (): UserTokenDto => {
const stored = getJSON(StorageNames.user)
if (stored) return stored
return { // TODO: Удалить в следующем релизе, вставлено для совместимости
id: Number(localStorage.getItem(StorageNames.userId)) ?? undefined,
login: localStorage.getItem(StorageNames.login) || undefined,
token: localStorage.getItem(StorageNames.token) || undefined,
}
}
export const getUser = (): UserTokenDto => getJSON(StorageNames.user) || {}
export const getUserPermissions = (): Permission[] | null => {
let permissions = getUser()?.permissions?.map((perm) => perm.name as string)
@ -62,8 +54,6 @@ export const getUserPermissions = (): Permission[] | null => {
export const setUser = (user: UserTokenDto) => {
OpenAPI.TOKEN = user.token ?? undefined
localStorage.setItem(StorageNames.user, JSON.stringify(user))
localStorage.setItem(StorageNames.token, String(user.token))
}
export const removeUser = () => {