before show
This commit is contained in:
@@ -50,7 +50,14 @@ export async function getFetch(path:string) {
|
||||
}
|
||||
|
||||
|
||||
export function useDataPage<T>(application:string, endpoint: string) {
|
||||
const createQueryString = (params: Record<string, string | number | boolean | undefined>): string => {
|
||||
const filteredParams = Object.entries(params)
|
||||
.filter(([_, value]) => value !== undefined && value !== null)
|
||||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
||||
return filteredParams.join('&');
|
||||
};
|
||||
|
||||
export function useDataPage<T>(application:string, endpoint: string, params:{[key:string]:string|number}={}) {
|
||||
const [data, setData] = useState<T[]|null>(null);
|
||||
// const [previousUrl, setPreviousUrl] = useState<string|undefined>(undefined)
|
||||
// const [nextUrl, setNextUrl] = useState<string|undefined>(undefined)
|
||||
@@ -61,7 +68,7 @@ export function useDataPage<T>(application:string, endpoint: string) {
|
||||
useEffect(
|
||||
()=>{
|
||||
if (data === null){
|
||||
getFetch(`/${application}/${endpoint}/`).then((r)=>{
|
||||
getFetch(`/${application}/${endpoint}/?${createQueryString(params)}`).then((r)=>{
|
||||
if (r.success){
|
||||
const pageData = r.body as PageResponse<T>;
|
||||
|
||||
@@ -111,3 +118,29 @@ export function useDataPage<T>(application:string, endpoint: string) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
export function useData<T>(application:string, endpoint: string, id: string|number) {
|
||||
|
||||
const [data, setData] = useState<T | null | undefined>(null);
|
||||
|
||||
useEffect(
|
||||
()=>{
|
||||
if (data === null){
|
||||
getFetch(`/${application}/${endpoint}/${id}`).then((r)=>{
|
||||
if (r.success){
|
||||
const pageData = r.body as T;
|
||||
setData(pageData);
|
||||
}
|
||||
else {
|
||||
setData(null);
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
[data, application, endpoint, id]
|
||||
)
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user