import { h } from 'preact'; import { useState, useRef, useEffect } from 'preact/hooks'; import { ExternalLink, Check } from 'react-feather'; import http from '../http'; function Result(r) { const [added, setAdded] = useState(false) async function onClick() { const [error, want] = await http.postWant(r.id); if (error) return false; setAdded(true); } const classes = `${added ? 'added' : ''}`; const btnText = added ? : '+'; return ( {btnText} {r.title} {r.year} {r.country} {r.format.join(' ')} ); } const User = () => { const [artist, setArtist] = useState('') const [title, setTitle] = useState(''); const [username, setUsername] = useState(''); const [results, setResults] = useState([]); const [infoText, setInfoText] = useState(''); async function fetchResults() { const [error, results] = await http.postWantSearch(artist, title); if (results.length === 0) { setResults([]); return setInfoText('No results'); } setInfoText(''); setResults(results); } async function submitWantlist() { const [error, wants] = await http.postDiscogs(username); if (wants.length === 0) { setResults([]); return setInfoText('No new additions'); } setInfoText(`Added ${wants.length} items to your list.`); } const searchRef = useRef() useEffect(() => { searchRef.current.focus() }, []) function handleSubmit(e) { e.preventDefault(); fetchResults(); } function handleDiscogsSubmit(e) { e.preventDefault(); submitWantlist(); } const topEl = results.length ? window.scrollTo(0, 0)}> top : null; const infoEl = infoText ? {infoText} : null; return ( Artist setArtist((e.target as HTMLInputElement).value) } required /> Title setTitle((e.target as HTMLInputElement).value)} required /> Discogs Username setUsername((e.target as HTMLInputElement).value) } required /> Data provided by Discogs. {infoEl} {results.map(Result)} {topEl} ); }; export default User;
Data provided by Discogs.