Search function

This commit is contained in:
femsci 2023-10-22 10:04:07 +02:00
parent 436fe37dd2
commit dffb9eec84
Signed by: femsci
GPG key ID: 08F7911F0E650C67

View file

@ -1,9 +1,33 @@
import { IonBackButton, IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonInfiniteScroll, IonInfiniteScrollContent, IonItem, IonLabel, IonList, IonModal, IonRouterContext, IonSearchbar, IonTitle, IonToolbar, useIonRouter } from "@ionic/react"; import {
IonBackButton,
IonButton,
IonButtons,
IonContent,
IonHeader,
IonIcon,
IonInfiniteScroll,
IonInfiniteScrollContent,
IonItem,
IonLabel,
IonList,
IonModal,
IonRouterContext,
IonSearchbar,
IonTitle,
IonToolbar,
useIonRouter,
} from "@ionic/react";
import { useContext, useEffect, useRef, useState } from "react"; import { useContext, useEffect, useRef, useState } from "react";
import styles from "./WarningsList.module.css"; import styles from "./WarningsList.module.css";
import { Redirect, Route, Router, __RouterContext, useLocation } from "react-router"; import {
Redirect,
Route,
Router,
__RouterContext,
useLocation,
} from "react-router";
import { chevronBack, closeOutline } from "ionicons/icons"; import { chevronBack, closeOutline } from "ionicons/icons";
const WarningsList: React.FC = () => { const WarningsList: React.FC = () => {
@ -13,18 +37,21 @@ const WarningsList: React.FC = () => {
const [isFirstRequest, setIsFirstRequest] = useState(true); const [isFirstRequest, setIsFirstRequest] = useState(true);
const [pageIndex, setPageIndex] = useState(1); const [pageIndex, setPageIndex] = useState(1);
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const [currentlySelectedWarning, setCurrentlySelectedWarning] = useState<object>(); const [currentlySelectedWarning, setCurrentlySelectedWarning] =
useState<object>();
const { push } = useIonRouter(); const { push } = useIonRouter();
const eventRef = useRef(); const eventRef = useRef();
const [searchValue, setSearchValue] = useState(""); const [searchValue, setSearchValue] = useState("");
useEffect(() => { useEffect(() => {
async function getDataset() { async function getDataset() {
const response = await fetch("https://api.dane.gov.pl/1.4/datasets/855/resources?page=1&per_page=20&sort=-data_date&include=institution"); const response = await fetch(
"https://api.dane.gov.pl/1.4/datasets/855/resources?page=1&per_page=20&sort=-data_date&include=institution"
);
const data = await response.json(); const data = await response.json();
setDataset(data.data); setDataset(data.data);
}; }
getDataset(); getDataset();
}, []); }, []);
@ -34,44 +61,62 @@ const WarningsList: React.FC = () => {
if (isFirstRequest === true) { if (isFirstRequest === true) {
async function getDatasetElements() { async function getDatasetElements() {
for (let datasetElement of dataset) { for (let datasetElement of dataset) {
const response = await fetch(datasetElement.relationships.tabular_data.links.related); const response = await fetch(
datasetElement.relationships.tabular_data.links.related
);
const data = await response.json(); const data = await response.json();
setWarningsContent(previousWarningsContent => [...previousWarningsContent, data.data[0].attributes]); setWarningsContent((previousWarningsContent) => [
}; ...previousWarningsContent,
}; data.data[0].attributes,
]);
}
}
getDatasetElements(); getDatasetElements();
} }
}; }
}, [dataset]); }, [dataset]);
useEffect(() => {
if (searchValue === null || searchValue.length == 0) {
return;
}
}, [searchValue]);
useEffect(() => { useEffect(() => {
if (warningsContent.length === 20) { if (warningsContent.length === 20) {
setIsFirstRequest(false); setIsFirstRequest(false);
}; }
}, [warningsContent]); }, [warningsContent]);
function onIonScroll(event: any) { function onIonScroll(event: any) {
setPageIndex(previousPageIndex => previousPageIndex + 1); setPageIndex((previousPageIndex) => previousPageIndex + 1);
setShouldFetchMoreData(true); setShouldFetchMoreData(true);
eventRef.current = event; eventRef.current = event;
}; }
useEffect(() => { useEffect(() => {
if (shouldFetchMoreData) { if (shouldFetchMoreData) {
async function fetchNewData() { async function fetchNewData() {
const response = await fetch(`https://api.dane.gov.pl/1.4/datasets/855/resources?page=${pageIndex}&per_page=20&sort=-data_date&include=institution`); const response = await fetch(
`https://api.dane.gov.pl/1.4/datasets/855/resources?page=${pageIndex}&per_page=20&sort=-data_date&include=institution`
);
const data = await response.json(); const data = await response.json();
setDataset(previousDataset => [...previousDataset, ...data.data]); setDataset((previousDataset) => [...previousDataset, ...data.data]);
for (let dataElement of data.data) { for (let dataElement of data.data) {
const response2 = await fetch(dataElement.relationships.tabular_data.links.related); const response2 = await fetch(
dataElement.relationships.tabular_data.links.related
);
const data2 = await response2.json(); const data2 = await response2.json();
setWarningsContent(previousWarningsContent => [...previousWarningsContent, data2.data[0].attributes]); setWarningsContent((previousWarningsContent) => [
}; ...previousWarningsContent,
data2.data[0].attributes,
]);
}
let id = setTimeout(() => { let id = setTimeout(() => {
setShouldFetchMoreData(false); setShouldFetchMoreData(false);
@ -91,7 +136,7 @@ const WarningsList: React.FC = () => {
setCurrentlySelectedWarning(warningContent); setCurrentlySelectedWarning(warningContent);
console.log(warningContent); console.log(warningContent);
setIsModalOpen(true); setIsModalOpen(true);
}; }
function closeModal() { function closeModal() {
setCurrentlySelectedWarning(undefined); setCurrentlySelectedWarning(undefined);
@ -100,16 +145,33 @@ const WarningsList: React.FC = () => {
function handleInputValueChange(event: any) { function handleInputValueChange(event: any) {
setSearchValue(event.detail.value); setSearchValue(event.detail.value);
}; }
return ( return (
<> <>
<IonSearchbar placeholder="Wpisz słowo kluczowe" value={searchValue} onIonChange={handleInputValueChange} /> <IonSearchbar
placeholder="Wpisz słowo kluczowe"
value={searchValue}
onIonChange={handleInputValueChange}
/>
{warningsContent.length > 0 && ( {warningsContent.length > 0 && (
<> <>
<IonList lines="full"> <IonList lines="full">
{warningsContent.map((warningContent, key) => ( {warningsContent
<IonItem detail onClick={() => openModal(warningContent)} button key={key}> .filter((x) => {
if (searchValue === null || searchValue.trim().length == 0) {
return true;
}
return ((x.col7 as any).val as string).includes(searchValue);
})
.map((warningContent, key) => (
<IonItem
detail
onClick={() => openModal(warningContent)}
button
key={key}
>
<IonLabel className="ion-text-wrap"> <IonLabel className="ion-text-wrap">
{warningContent.col7.val} {warningContent.col7.val}
</IonLabel> </IonLabel>
@ -119,9 +181,7 @@ const WarningsList: React.FC = () => {
<IonModal isOpen={isModalOpen}> <IonModal isOpen={isModalOpen}>
<IonHeader> <IonHeader>
<IonToolbar> <IonToolbar>
<IonTitle> <IonTitle>Szczegóły</IonTitle>
Szczegóły
</IonTitle>
<IonButtons slot="start"> <IonButtons slot="start">
<IonButton onClick={closeModal}> <IonButton onClick={closeModal}>
<IonIcon icon={chevronBack} /> <IonIcon icon={chevronBack} />
@ -131,56 +191,37 @@ const WarningsList: React.FC = () => {
</IonToolbar> </IonToolbar>
</IonHeader> </IonHeader>
<IonContent> <IonContent>
{currentlySelectedWarning !== undefined && currentlySelectedWarning !== null && ( {currentlySelectedWarning !== undefined &&
currentlySelectedWarning !== null && (
<IonList lines="full"> <IonList lines="full">
<IonItem> <IonItem>
<IonLabel className={`${styles.label} ion-text-wrap`}> <IonLabel className={`${styles.label} ion-text-wrap`}>
<h1> <h1>{(currentlySelectedWarning as any).col3.val}</h1>
{(currentlySelectedWarning as any).col3.val} <p>województwo</p>
</h1>
<p>
województwo
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
<IonItem> <IonItem>
<IonLabel className={`${styles.label} ion-text-wrap`}> <IonLabel className={`${styles.label} ion-text-wrap`}>
<h1> <h1>{(currentlySelectedWarning as any).col2.val}</h1>
{(currentlySelectedWarning as any).col2.val} <p>nazwa kodowa</p>
</h1>
<p>
nazwa kodowa
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
<IonItem> <IonItem>
<IonLabel className={`${styles.label} ion-text-wrap`}> <IonLabel className={`${styles.label} ion-text-wrap`}>
<h1> <h1>{(currentlySelectedWarning as any).col5.val}</h1>
{(currentlySelectedWarning as any).col5.val} <p>firma</p>
</h1>
<p>
firma
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
<IonItem> <IonItem>
<IonLabel className={`${styles.label} ion-text-wrap`}> <IonLabel className={`${styles.label} ion-text-wrap`}>
<h1> <h1>{(currentlySelectedWarning as any).col6.val}</h1>
{(currentlySelectedWarning as any).col6.val} <p>kategoria</p>
</h1>
<p>
kategoria
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
<IonItem> <IonItem>
<IonLabel className={`${styles.label} ion-text-wrap`}> <IonLabel className={`${styles.label} ion-text-wrap`}>
<h1> <h1>{(currentlySelectedWarning as any).col7.val}</h1>
{(currentlySelectedWarning as any).col7.val} <p>nazwa produktu</p>
</h1>
<p>
nazwa produktu
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
<IonItem className={`${styles.label} ion-text-wrap`}> <IonItem className={`${styles.label} ion-text-wrap`}>
@ -188,9 +229,7 @@ const WarningsList: React.FC = () => {
<h1 className="ion-text-wrap"> <h1 className="ion-text-wrap">
{(currentlySelectedWarning as any).col9.val} {(currentlySelectedWarning as any).col9.val}
</h1> </h1>
<p> <p>powód zgłoszenia</p>
powód zgłoszenia
</p>
</IonLabel> </IonLabel>
</IonItem> </IonItem>
</IonList> </IonList>