From f5e75f4fe63e006b35b7abc424ff6590cf44f949 Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 16 Aug 2022 10:09:36 +1000 Subject: [PATCH] wants wip" --- api/src/api.ts | 12 ++++++++++-- api/src/pg.ts | 7 +++++++ client/src/actions.ts | 4 ++++ client/src/app.tsx | 5 +---- client/src/components/header.tsx | 2 +- client/src/routes/search.tsx | 2 +- client/src/routes/user.tsx | 13 +++++++++---- 7 files changed, 33 insertions(+), 12 deletions(-) diff --git a/api/src/api.ts b/api/src/api.ts index 2f2e056..a9d3639 100644 --- a/api/src/api.ts +++ b/api/src/api.ts @@ -147,7 +147,7 @@ export default function createApi(log: Log, pg: Pg, discogs) { ctx.response.body = listings; }); - router.post('/want', async function wantPost(ctx, next) { + router.get('/wants', async function wantPost(ctx, next) { const { userId, releaseId } = ctx.request.body; const user = await pg.userFindId(userId); const release = await discogs.release(releaseId); @@ -155,7 +155,15 @@ export default function createApi(log: Log, pg: Pg, discogs) { ctx.response.body = want; }); - router.post('/want/search', async function wantPost(ctx, next) { + router.get('/wants', async function wantGet(ctx, next) { + ctx.assert(ctx.isAuthenticated(), 401, 'log in required'); + log.warn(ctx.state.user); + const user = ctx.state.user; + const wants = await pg.wantFind(user); + ctx.response.body = wants; + }); + + router.post('/wants/search', async function wantPost(ctx, next) { const { artist, title } = ctx.request.body; const results = await discogs.search(artist, title); ctx.response.body = results; diff --git a/api/src/pg.ts b/api/src/pg.ts index 8e11092..24abb88 100644 --- a/api/src/pg.ts +++ b/api/src/pg.ts @@ -17,6 +17,7 @@ export interface Pg { userWants: (user: User) => Promise, wantUpsert: (want: Want, user: User) => Promise, + wantFind: (user: User) => Promise, listingUpsert: (listing: Listing, want: Want) => Promise, listingFind: (id: DiscogsListing) => Promise, listingRmUnlisted: (want: Want, ids: DiscogsListing[]) => Promise, @@ -53,6 +54,12 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { }); } + async function wantList(user: User) { + return prisma.want.findMany({ + where: { userId: user.id }, + }); + } + async function wantUpsert(dcWant, user) { const values: { userId: UUID, release: DiscogsListing, data: any } = { data: dcWant, diff --git a/client/src/actions.ts b/client/src/actions.ts index 4039a5f..0ce1214 100644 --- a/client/src/actions.ts +++ b/client/src/actions.ts @@ -48,6 +48,10 @@ const actions = { return api.get(`/user/${userId}/offers`); }, + getWants: async function getWants() { + return api.get('/wants'); + }, + getLogin: async function getLogin() { return api.get('/auth/login') }, diff --git a/client/src/app.tsx b/client/src/app.tsx index 1107ced..5c32bc3 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -2,7 +2,6 @@ import { h } from 'preact'; import { useState, useEffect } from 'preact/hooks'; import { Route, Router, route } from 'preact-router'; -// import mcratesStore from './store'; import actions from './actions'; import Header from './components/header'; @@ -12,8 +11,6 @@ import Login from './routes/login'; import Search from './routes/search'; import User from './routes/user'; -// const store = mcratesStore(); - function App() { const [user, setUser] = useState(null); @@ -41,7 +38,7 @@ function App() { - + ); diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 42c8226..58e689d 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -5,8 +5,8 @@ const Header = ({ user }) => { const nav = () => ( ); diff --git a/client/src/routes/search.tsx b/client/src/routes/search.tsx index de1e405..16a2889 100644 --- a/client/src/routes/search.tsx +++ b/client/src/routes/search.tsx @@ -24,7 +24,7 @@ const User = () => { const [noResults, setNoResults] = useState(false); async function fetchResults() { - const { data } = await axios.post(`http://localhost:41337/want/search`, { + const { data } = await axios.post(`http://localhost:41337/wants/search`, { artist, title, }); diff --git a/client/src/routes/user.tsx b/client/src/routes/user.tsx index 474b93b..561aaea 100644 --- a/client/src/routes/user.tsx +++ b/client/src/routes/user.tsx @@ -6,17 +6,22 @@ import actions from './../actions'; import Bundle from './../components/bundle'; interface Props { - userId: string, + user: any, offers: any[], getUserOffers: (userId: string) => void, } -const User = ({ userId }: Props) => { +const User = ({ user }: Props) => { const [offers, setOffers] = useState([]); useEffect(() => { - actions.getOffers(userId).then(setOffers); - }, [userId]); + async function getWants() { + const [error, wants] = await actions.getWants(); + if (error) return console.error(error); + setOffers(wants); + } + getWants(); + }, []); if (!offers.length) {