nicer toasts and fix some discogs sync problems

This commit is contained in:
ntr committed 2023-02-07 22:11:38 +11:00
1 parent 1ca9a7225c
commit c6bf50f963
7 files changed
+50 -26

No files matched your search

+3 -3
View File
@@ -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[]
+3 -1
View File
@@ -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);
+4 -8
View File
@@ -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 {
+17 -13
View File
@@ -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<HTMLInputElement>();
@@ -87,10 +96,6 @@ const User = () => {
? <button onClick={() => resultsRef.current.scrollTo(0, 0)}> top </button>
: null;
const infoEl = infoText
? <article><div /><div>{infoText}</div></article>
: null;
return (
<div class='search'>
<div>
@@ -111,7 +116,6 @@ const User = () => {
<p>Data provided by Discogs.</p>
</div>
<div class='results' ref={resultsRef}>
{infoEl}
{results.map(r => Result(r, wants, ws))}
{topEl}
</div>
+1
View File
@@ -75,6 +75,7 @@ html {
--color-blue: #475977;
--color-green: #476d4a;
--color-red: #ae4442;
--color-accepted: var(--color-blue);
--color-confirmed: var(--color-green);
+15
View File
@@ -13,3 +13,18 @@ export function info(title, message) {
theme: 'dark',
});
}
export function error(title, message) {
toast.error({
title,
message: message || '',
position: 'bottomCenter',
color: '#ae4442',
titleColor: '#F4F1D0',
messageColor: '#F4F1D0',
iconColor: '#F4F1D0',
icon: '',
theme: 'dark',
});
}
+7 -1
View File
@@ -1,4 +1,4 @@
import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';
import { wantsState, userState, offersState, acpOffersState } from './store'
import * as toast from './toast';
@@ -77,7 +77,13 @@ export default function createSocket(dispatch) {
dispatch(wantsState.actions.set(wants));
}
const offersNotification = throttle((offers) => {
if (!offers.length) return false;
toast.info('Offers Updated', 'New offers available.')
}, 60 * 1000);
function onOffers(offers) {
offersNotification(offers);
dispatch(offersState.actions.set(offers));
}