✨ frontend add
This commit is contained in:
25
frontend/admin/src/hooks/useLocalStorage.ts
Normal file
25
frontend/admin/src/hooks/useLocalStorage.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
export const useLocalStorage = (key: string, initialValue: unknown = '') => {
|
||||
const [storeValue, setStoreValue] = useState(() => {
|
||||
try {
|
||||
const item = window.localStorage.getItem(key)
|
||||
return item ? JSON.parse(item) : initialValue
|
||||
} catch (error) {
|
||||
return initialValue
|
||||
}
|
||||
})
|
||||
|
||||
const setValue = (value: unknown) => {
|
||||
try {
|
||||
const valueToStore = value instanceof Function ? value(storeValue) : value
|
||||
|
||||
setStoreValue(valueToStore)
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore))
|
||||
} catch (error) {
|
||||
console.error(`useLocalStorage setValue error : ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
return [storeValue, setValue]
|
||||
}
|
||||
11
frontend/admin/src/hooks/useMounted.ts
Normal file
11
frontend/admin/src/hooks/useMounted.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function useMounted() {
|
||||
const [mounted, setMounted] = useState<boolean>(false)
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true)
|
||||
}, [])
|
||||
|
||||
return mounted
|
||||
}
|
||||
17
frontend/admin/src/hooks/usePage.ts
Normal file
17
frontend/admin/src/hooks/usePage.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { pageAtom, pageSelector } from '@stores'
|
||||
import { useState } from 'react'
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil'
|
||||
|
||||
export default function usePage(conditionKey: string, initPage: number = 0) {
|
||||
const pageState = useRecoilValue(pageAtom(conditionKey))
|
||||
const setValue = useSetRecoilState(pageSelector(conditionKey))
|
||||
|
||||
const [page, setPage] = useState<number>(pageState || initPage)
|
||||
|
||||
const setPageValue = (num: number) => {
|
||||
setValue(num)
|
||||
setPage(num)
|
||||
}
|
||||
|
||||
return { page, setPageValue }
|
||||
}
|
||||
12
frontend/admin/src/hooks/useSearchType.ts
Normal file
12
frontend/admin/src/hooks/useSearchType.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { IKeywordType } from '@components/Search'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function useSearchTypes(init: IKeywordType[]) {
|
||||
const [searchTypes, setSearchTypes] = useState<IKeywordType[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
setSearchTypes(init)
|
||||
}, [])
|
||||
|
||||
return searchTypes
|
||||
}
|
||||
36
frontend/admin/src/hooks/useUser.ts
Normal file
36
frontend/admin/src/hooks/useUser.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import useSWR from 'swr'
|
||||
import axios from 'axios'
|
||||
import { AUTH_USER_ID } from '@constants/env'
|
||||
import { loginSerivce } from '@service'
|
||||
|
||||
export default function useUser() {
|
||||
const { data, error, mutate } = useSWR(
|
||||
`/user-service/api/v1/users`,
|
||||
async (url: string) => {
|
||||
let userId = axios.defaults.headers.common[AUTH_USER_ID]
|
||||
if (!userId) {
|
||||
await loginSerivce.silentRefresh()
|
||||
}
|
||||
userId = axios.defaults.headers.common[AUTH_USER_ID]
|
||||
if (userId) {
|
||||
return axios.get(`${url}/${userId}`).then(res => res.data)
|
||||
} else {
|
||||
throw new Error('No User')
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const loading = !data && !error
|
||||
const isLogin = !Boolean(error) && Boolean(data)
|
||||
const loggedOut =
|
||||
error && (error.response?.status === 401 || error.response?.status === 403)
|
||||
|
||||
return {
|
||||
user: data,
|
||||
loading: loading,
|
||||
isLogin,
|
||||
error,
|
||||
mutate,
|
||||
loggedOut,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user