From 15a84d6eeacb55caaa7e05a79499b2215c9a87c6 Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 29 Oct 2022 12:41:46 +1100 Subject: [PATCH] offer groups with status --- api/index.ts | 2 +- api/src/offers.ts | 15 ++++++++- api/src/pg.ts | 32 +++---------------- .../{want.offers.tsx => offer.group.tsx} | 14 ++++---- client/src/routes/offers.tsx | 8 ++--- client/src/store.tsx | 2 +- 6 files changed, 29 insertions(+), 44 deletions(-) rename client/src/components/{want.offers.tsx => offer.group.tsx} (80%) diff --git a/api/index.ts b/api/index.ts index 0c70153..4d12a0c 100644 --- a/api/index.ts +++ b/api/index.ts @@ -28,7 +28,7 @@ async function start() { events, } = createServices(log); - events.emit(Events.StartupSync); + // events.emit(Events.StartupSync); // events.emit(Events.ListingsUpdated); api.listen(); diff --git a/api/src/offers.ts b/api/src/offers.ts index c9fba86..4caa2f7 100644 --- a/api/src/offers.ts +++ b/api/src/offers.ts @@ -149,7 +149,20 @@ export default function createOffers(log: Log, pg: Pg) { async function userOffers(user: User) { const offers = await pg.userOffers(user); - return groupBy(offers, (o) => o.wantId); + const groups = Object.values(groupBy(offers, (o) => o.wantId)) + .map(g => { + const offers = sortBy(g, sortOffers); + const status = offers[0].status; + const details = offers[0].want.data; + + return { + details, + offers, + status, + }; + }); + + return groups; } return { diff --git a/api/src/pg.ts b/api/src/pg.ts index c8a436f..be0bd97 100644 --- a/api/src/pg.ts +++ b/api/src/pg.ts @@ -11,33 +11,7 @@ export interface UserCreateParams { password: string, } -export interface Pg { - userFindId: (id: UUID) => Promise, - userFindEmail: (email: Email) => Promise, - userCreate: (params: UserCreateParams) => Promise, - userWants: (user: User) => Promise, - userOffers: (user: User) => Promise, - userOfferFind: (user: User, id: UUID) => Promise, - - listingUpsert: (listing: Listing, want: Want) => Promise, - listingFind: (id: DiscogsListing) => Promise, - listingByIds: (ids: DiscogsListing[]) => Promise, - listingRmUnlisted: (want: Want, ids: DiscogsListing[]) => Promise, - - wantCreate: (discogsWant: any, user: User) => Promise, - wantList: (user: User) => Promise, - wantsToSync: () => Promise, - wantRm: (id: UUID) => Promise, - - bundleCandidates: () => Promise, - - offersCreate: (offers: Offer[]) => Promise, - offerStatusSet: (id: UUID, status: Status) => Promise, - offersAdmin: () => Promise, - offerFindId: (id: UUID) => Promise, -} - -export default function createPg(log: Log, prisma: PrismaClient): Pg { +export default function createPg(log: Log, prisma: PrismaClient) { async function userFindId(id: UUID) { return prisma.user.findUnique({ where: { id }}); } @@ -324,4 +298,6 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { offersAdmin, offerFindId, }; -} \ No newline at end of file +} + +export type Pg = ReturnType; diff --git a/client/src/components/want.offers.tsx b/client/src/components/offer.group.tsx similarity index 80% rename from client/src/components/want.offers.tsx rename to client/src/components/offer.group.tsx index 88b9da8..092dcd6 100644 --- a/client/src/components/want.offers.tsx +++ b/client/src/components/offer.group.tsx @@ -55,23 +55,21 @@ function OfferPriceBtn(offer) { } -export default function WantOffers(wantOffers, i) { +export default function OfferGroup(group, i) { const ws = useSelector((state: State) => state.ws); - const details = wantOffers[0].want.data; - return (
-

{details.artists_sort} - {details.title}

+

{group.details.artists_sort} - {group.details.title}

- {details.year} - {details.formats.map(f => f.descriptions.join(' ')).join(' ')} + {group.details.year} - {group.details.formats.map(f => f.descriptions.join(' ')).join(' ')}
-
Status: {wantOffers[0].status}
+
Status: {group.status}
- +
- {wantOffers.map(o => OfferPriceBtn(o))} + {group.offers.map(o => OfferPriceBtn(o))}
) diff --git a/client/src/routes/offers.tsx b/client/src/routes/offers.tsx index ce379b3..d1ac1ae 100644 --- a/client/src/routes/offers.tsx +++ b/client/src/routes/offers.tsx @@ -6,21 +6,19 @@ import { useSelector, useDispatch } from 'react-redux' import http from './../http'; import { State, offersState } from './../store'; -import WantOffers from './../components/want.offers'; +import OfferGroup from './../components/offer.group'; function Home() { const dispatch = useDispatch() const offers = useSelector((state: State) => state.offers); - const groups = Object.values(offers); - - if (offers && !groups.length) { + if (offers && offers.length === 0) { route('/search'); } return (
- {groups.map(WantOffers)} + {offers.map(OfferGroup)}
); } diff --git a/client/src/store.tsx b/client/src/store.tsx index c846e73..a25dd5d 100644 --- a/client/src/store.tsx +++ b/client/src/store.tsx @@ -11,7 +11,7 @@ export const userState = createSlice({ export const offersState = createSlice({ name: 'offers', - initialState: {}, + initialState: [], reducers: { set: (state, action) => action.payload },