import React, { useEffect, useRef, useState } from "react"; import { Button, Card, Container, Form, FormControl, FormGroup, FormLabel, Row, Spinner } from "react-bootstrap"; import client from "./ApiClient"; import { Band, Option } from "./Types"; import ModalBand from "./ModalBand"; import { useHistory, useParams } from "react-router-dom"; function App() { const searchRef = useRef(null); const [bands, setBands] = useState>(null); const [selectedBand, setSelectedBand] = useState>(null); const params = useParams(); const bandQuery = decodeURIComponent(params.query); const history = useHistory(); useEffect(() => { if (bandQuery) { client.searchBand(bandQuery).then((res) => setBands(res)); } }, [bandQuery]); const search = (e: any) => { const input = searchRef.current?.value; if (!input) { return; } history.push(`/${encodeURIComponent(input)}`); setBands(null); e.preventDefault(); }; return (
Search Band
{selectedBand && setSelectedBand(null)} band={selectedBand} />} {bands ? ( bands.map((band) => ( setSelectedBand(band)} style={{ width: "18rem" }} key={band.uid}> {band.name} {getBioText(band, 200)} )) ) : bandQuery ? ( ) : ( <> )}
); } export function getBioText(band: Band, maxLen: number): string { const bio = band.biographies.find((bio) => bio.lang === "de") || band.biographies[0]; return limit(bio?.description || "Keine Biographie angegeben.", maxLen); } function limit(str: string, max: number): string { if (str.length > max) { return str.substr(0, max - 3) + "..."; } return str; } export default App;