2025-06-01 20:55:44 +03:00
|
|
|
import {useEffect, useMemo, useState} from "react";
|
2025-05-30 19:32:58 +03:00
|
|
|
import {DEBUG_MODE} from "../utils/common";
|
|
|
|
|
import {ApiResponse, PageResponse} from "../types/api.ts";
|
|
|
|
|
import {PageControl} from "../types/common.ts";
|
2025-06-01 20:55:44 +03:00
|
|
|
import {Institution} from "../types/survey.ts";
|
2025-05-30 19:32:58 +03:00
|
|
|
|
|
|
|
|
export function hostUrl(url: string){
|
|
|
|
|
return DEBUG_MODE? "http://localhost:8000"+url: url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function apiUrl(url: string) {
|
|
|
|
|
return hostUrl("/api/v1"+url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getFetch(path:string) {
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
apiUrl(path), {
|
|
|
|
|
method: "GET",
|
|
|
|
|
credentials: "include"
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok){
|
|
|
|
|
const body = await response.json();
|
|
|
|
|
|
|
|
|
|
console.error('Error fetching:', 'Network response was not ok', body);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
body: body,
|
|
|
|
|
success: false
|
|
|
|
|
} as ApiResponse;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
body: await response.json(),
|
|
|
|
|
success: true,
|
|
|
|
|
} as ApiResponse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch {
|
|
|
|
|
return {
|
|
|
|
|
success: false
|
|
|
|
|
} as ApiResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-05-31 10:57:27 +03:00
|
|
|
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('&');
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-01 00:52:20 +03:00
|
|
|
export function useDataPage<T>(application:string, endpoint: string, params:{[key:string]:string|number}={}, needLoad:boolean=true):PageControl<T> {
|
2025-05-30 19:32:58 +03:00
|
|
|
const [data, setData] = useState<T[]|null>(null);
|
|
|
|
|
// const [previousUrl, setPreviousUrl] = useState<string|undefined>(undefined)
|
|
|
|
|
// const [nextUrl, setNextUrl] = useState<string|undefined>(undefined)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
()=>{
|
2025-06-01 00:52:20 +03:00
|
|
|
if (data === null && needLoad){
|
2025-05-31 10:57:27 +03:00
|
|
|
getFetch(`/${application}/${endpoint}/?${createQueryString(params)}`).then((r)=>{
|
2025-05-30 19:32:58 +03:00
|
|
|
if (r.success){
|
|
|
|
|
const pageData = r.body as PageResponse<T>;
|
|
|
|
|
|
|
|
|
|
setData(pageData.results);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
setData([]);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-06-01 16:17:09 +03:00
|
|
|
[data, application, endpoint, needLoad]
|
2025-05-30 19:32:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// function changePage(pageUrl: string|undefined) {
|
|
|
|
|
// if (pageUrl){ //TODO CHANGING PAGES
|
|
|
|
|
// getFetch(`/${application}/${endpoint}/`).then((r)=>{
|
|
|
|
|
// if (r.success){
|
|
|
|
|
// const pageData = r.body as PageResponse<T>;
|
|
|
|
|
//
|
|
|
|
|
// setData(pageData.results);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
|
// changePage(nextUrl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function previousPage() {
|
|
|
|
|
// changePage(previousUrl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
items: data,
|
2025-06-01 00:52:20 +03:00
|
|
|
hasPrevious: false,//previousUrl !== undefined,
|
|
|
|
|
hasNext: false,//nextUrl !== undefined,
|
2025-05-30 19:32:58 +03:00
|
|
|
nextPage,
|
|
|
|
|
previousPage
|
|
|
|
|
} as PageControl<T>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-05-31 10:57:27 +03:00
|
|
|
|
2025-06-01 00:52:20 +03:00
|
|
|
export function useData<T>(application:string, endpoint: string, id: string|number, needLoad:boolean=true): T|null|undefined {
|
2025-05-31 10:57:27 +03:00
|
|
|
|
|
|
|
|
const [data, setData] = useState<T | null | undefined>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
()=>{
|
2025-06-01 00:52:20 +03:00
|
|
|
if (data === null && needLoad){
|
2025-05-31 10:57:27 +03:00
|
|
|
getFetch(`/${application}/${endpoint}/${id}`).then((r)=>{
|
|
|
|
|
if (r.success){
|
|
|
|
|
const pageData = r.body as T;
|
|
|
|
|
setData(pageData);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-06-01 00:52:20 +03:00
|
|
|
setData(undefined);
|
2025-05-31 10:57:27 +03:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-06-01 00:52:20 +03:00
|
|
|
[data, application, endpoint, id, needLoad]
|
2025-05-31 10:57:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-01 20:55:44 +03:00
|
|
|
export function useCachedData<T>(application: string, endpoint: string) {
|
|
|
|
|
const [cache, setCache] = useState<Map<string | number, T | undefined>>(new Map());
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Очистка кеша или другие побочные эффекты при необходимости
|
|
|
|
|
return () => {
|
|
|
|
|
setCache(new Map());
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (id: string | number, needLoad: boolean = true): T | undefined | null => {
|
|
|
|
|
const cachedData = cache.has(id)?cache.get(id):null;
|
|
|
|
|
|
|
|
|
|
if (cachedData !== null) {
|
|
|
|
|
return cachedData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (needLoad) {
|
|
|
|
|
getFetch(`/${application}/${endpoint}/${id}`).then((r) => {
|
|
|
|
|
if (r.success) {
|
|
|
|
|
const data = r.body as T;
|
|
|
|
|
setCache((prevCache) => {
|
|
|
|
|
const newCache = new Map(prevCache);
|
|
|
|
|
newCache.set(id, data);
|
|
|
|
|
return newCache;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setCache((prevCache) => {
|
|
|
|
|
const newCache = new Map(prevCache);
|
|
|
|
|
newCache.set(id, undefined);
|
|
|
|
|
return newCache;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|