import { h } from 'preact'; import { useState, useRef, useEffect } from 'preact/hooks'; import { ExternalLink, Check } from 'react-feather'; import { useSelector } from 'react-redux' import { State } from './../store'; import http from '../http'; function Result(r, wants) { const [added, setAdded] = useState(false) async function onClick() { const [error, want] = await http.postWant(r.id); if (error) return false; setAdded(true); } const wanted = wants.some(w => w.release === r.id); const classes = `${added || wanted ? 'added' : ''}`; const btnText = added ? : '+'; return ( {btnText} {r.title} {r.year} {r.country} {r.format.join(' ')} ); } const User = () => { const [query, setQuery] = useState('') const [title, setTitle] = useState(''); const [username, setUsername] = useState(''); const [results, setResults] = useState([]); const [infoText, setInfoText] = useState(''); const wants = useSelector((state: State) => state.wants); async function fetchResults() { const [error, results] = await http.postWantSearch(query); 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 ( Query setQuery((e.target as HTMLInputElement).value) } required /> Discogs Username setUsername((e.target as HTMLInputElement).value) } required /> Data provided by Discogs. {infoEl} {results.map(r => Result(r, wants))} {topEl} ); }; export default User;
Data provided by Discogs.