before show

This commit is contained in:
Mikan
2025-05-31 10:57:27 +03:00
parent 90d12b57ec
commit a37118dc6a
7 changed files with 139 additions and 26 deletions

View File

@@ -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;
}