From c6bf50f963537ee7b42ed3c7055857a547de68bd Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 7 Feb 2023 22:11:38 +1100 Subject: [PATCH] nicer toasts and fix some discogs sync problems --- api/prisma/schema.prisma | 6 +++--- api/src/api.ts | 4 +++- api/src/discogs.ts | 12 ++++-------- client/src/routes/search.tsx | 30 +++++++++++++++++------------- client/src/style/index.less | 1 + client/src/toast.tsx | 15 +++++++++++++++ client/src/ws.ts | 8 +++++++- 7 files changed, 50 insertions(+), 26 deletions(-) diff --git a/api/prisma/schema.prisma b/api/prisma/schema.prisma index 2098e28..1325587 100644 --- a/api/prisma/schema.prisma +++ b/api/prisma/schema.prisma @@ -26,7 +26,7 @@ model Listing { offers Offer[] createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + updatedAt DateTime @default(now()) @@index([seller]) @@map("listings") @@ -38,7 +38,7 @@ model User { password String createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + updatedAt DateTime @default(now()) wants Want[] offers Offer[] @@ -55,7 +55,7 @@ model Want { user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade) createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + updatedAt DateTime @default(now()) listings Listing[] offers Offer[] diff --git a/api/src/api.ts b/api/src/api.ts index a413364..d17823f 100644 --- a/api/src/api.ts +++ b/api/src/api.ts @@ -29,8 +29,9 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of const app = koaWebsocket(new koa()); + // @ts-ignore - ts don't know wtf it talking about BigInt.prototype.toJSON = function bigIntStringify() { - return this.toString(); + return this.toString(); }; app.use(async (ctx, next) => { @@ -71,6 +72,7 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of router.post('/want/discogs', async function wantListPost(ctx, next) { ctx.assert(ctx.isAuthenticated(), 401, 'log in required'); const { username } = ctx.request.body; + ctx.assert(username, 'no username supplied'); const user = await pg.userFindId(ctx.state.user.id); const wants = await discogs.wantListSync(username, user); events.emit(Events.WantsAdded, wants); diff --git a/api/src/discogs.ts b/api/src/discogs.ts index 7edf3ae..bda39bc 100644 --- a/api/src/discogs.ts +++ b/api/src/discogs.ts @@ -109,14 +109,10 @@ export default function createDiscogs(log: Log, pg: Pg) { const listing = await pg.listingFind(id); if (listing) return listing; - try { - log.debug('fetching listing', id); - const res = await api.get(`${apiBase}/marketplace/listings/${id}`); - log.debug(res.data); - return pg.listingUpsert(res.data, want); - } catch (e) { - return handleApiError(e); - } + log.debug('fetching listing', id); + const res = await api.get(`${apiBase}/marketplace/listings/${id}`); + log.debug(res.data); + return pg.listingUpsert(res.data, want); } return { diff --git a/client/src/routes/search.tsx b/client/src/routes/search.tsx index 430e324..47d6e82 100644 --- a/client/src/routes/search.tsx +++ b/client/src/routes/search.tsx @@ -4,6 +4,7 @@ import { ExternalLink, Check } from 'react-feather'; import { useSelector } from 'react-redux' import { State } from './../store'; +import { info, error as toastError } from './../toast'; import http from '../http'; @@ -36,10 +37,8 @@ function Result(r, wants, ws) { 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); const ws = useSelector((state: State) => state.ws); @@ -47,23 +46,33 @@ const User = () => { async function fetchResults() { const [error, results] = await http.postWantSearch(query); - if (results.length === 0) { + if (error) { setResults([]); - return setInfoText('No results'); + toastError('Error', error); + } + + if (results.length === 0) { + setResults([]); + return info('No Results', 'No results returned from search'); } - setInfoText(''); setResults(results); } async function submitWantlist() { const [error, wants] = await http.postDiscogs(username); - if (wants.length === 0) { + + if (error) { setResults([]); - return setInfoText('No new additions'); + toastError('Error', error); } - setInfoText(`Added ${wants.length} items to your list.`); + if (wants.length === 0) { + setResults([]); + return info('Success', 'No new additions'); + } + + info('Success', `Added ${wants.length} items to your list.`); } const searchRef = useRef(); @@ -87,10 +96,6 @@ const User = () => { ? : null; - const infoEl = infoText - ?
{infoText}
- : null; - return (