From acf7e54be9d30422ba390abc99cac2834bac9d88 Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 16 Sep 2022 21:48:14 +1000 Subject: [PATCH] remove unused api methods --- api/src/api.ts | 42 ----------------------- api/src/events.ts | 12 ------- api/src/rpc.ts | 14 ++++++-- client/src/app.tsx | 11 +++--- client/src/components/bundle.tsx | 58 -------------------------------- client/src/components/header.tsx | 1 - client/src/routes/bundles.tsx | 35 ------------------- client/src/routes/home.tsx | 41 ---------------------- client/src/routes/list.tsx | 3 -- client/src/ws.ts | 7 ++++ 10 files changed, 23 insertions(+), 201 deletions(-) delete mode 100644 client/src/components/bundle.tsx delete mode 100644 client/src/routes/bundles.tsx delete mode 100644 client/src/routes/home.tsx diff --git a/api/src/api.ts b/api/src/api.ts index 510fe6e..d94f2ca 100644 --- a/api/src/api.ts +++ b/api/src/api.ts @@ -59,48 +59,6 @@ export default function createApi(log: Log, pg: Pg, discogs, offers, events) { ctx.status = 200; }); - router.get('/acp/bundles', async function bundlesGet(ctx, next) { - const bundles = await offers.bundles(); - ctx.response.body = bundles; - }) - - router.get('/acp/offers', async function acpOffers(ctx, next) { - const offers = await pg.offersAdmin(); - - const grouped = groupBy(offers, o => o.listing.data.seller.id); - - const bundles = sortBy(Object.values(grouped).map(offers => { - const shipping = offers[0]?.listing.data.seller.shipping; - const bundle = { - username: offers[0]?.listing.data.seller.username, - seller: offers[0]?.listing.data.seller.id, - shipsFrom: offers[0]?.listing.data.ships_from, - shipping, - offers, - mentionsAustralia: shipping.toLowerCase().includes('australia'), - mentionsWorldwide: shipping.toLowerCase().includes('world'), - } - - return bundle; - }), bundle => -bundle.offers.length); - - ctx.response.body = bundles; - }) - - router.post('/offers', async function postOffers(ctx, next) { - const { listingIds, shipping, baseShipping } = ctx.request.body; - - ctx.assert(listingIds.length, 400, 'listingIds required'); - ctx.assert([listingIds, shipping, baseShipping].every(i => i), 400, 'listingIds, shipping, baseShipping required'); - - ctx.response.body = await offers.generate(listingIds, shipping, baseShipping); - }) - - router.post('/offer/accept', async function postOffers(ctx, next) { - ctx.assert(ctx.isAuthenticated(), 401, 'log in required'); - ctx.response.body = await offers.accept(ctx); - }) - router.get('/user/offers', async function userOffersGet(ctx, next) { ctx.assert(ctx.isAuthenticated(), 401, 'log in required'); const user = ctx.state.user; diff --git a/api/src/events.ts b/api/src/events.ts index 7f1650d..e7f356d 100644 --- a/api/src/events.ts +++ b/api/src/events.ts @@ -62,18 +62,6 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) { const bundles = await offers.bundles(); log.info({ event: Events.ListingsUpdated, bundles: bundles.length }); - // const bundle = { - // username: b[0]?.data.seller.username, - // seller: b[0]?.data.seller.id, - // shipsFrom: b[0]?.data.ships_from, - // total, - // currency: b[0]?.data.price.currency, - // shipping: '...', - // items, - // mentionsAustralia: shipping.toLowerCase().includes('australia'), - // mentionsWorldwide: shipping.toLowerCase().includes('world'), - // } - for (const bundle of bundles) { log.info({ seller: bundle.seller, items: bundle.items.length }, 'generating offers...'); const listingIds = bundle.items.map(i => i.id); diff --git a/api/src/rpc.ts b/api/src/rpc.ts index 7eb81ad..2d8e9ae 100644 --- a/api/src/rpc.ts +++ b/api/src/rpc.ts @@ -23,6 +23,10 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off return [RpcMessageType.Pong, []]; } + async function userOffers(): Promise { + return [RpcMessageType.Offers, await pg.userOffers(user)]; + } + async function offerStatusSet(params: OfferStatusSetParams): Promise { const { id, status } = params; assert(id, 'no id supplied'); @@ -70,14 +74,20 @@ export default function createRpc(log: Log, pg: Pg, user: User, ws, discogs, off switch (msgType) { case RpcMessageType.Ping: return ping(); + + case RpcMessageType.Offers: + return userOffers(); case RpcMessageType.OfferStatusSet: return offerStatusSet(params); + + case RpcMessageType.WantRm: + return wantRm(params); + case RpcMessageType.AcpOfferStatusSet: return acpOfferStatusSet(params); case RpcMessageType.AcpOffers: return acpOffers(); - case RpcMessageType.WantRm: - return wantRm(params); + default: throw new Error(`Message type ${msgType} is known but unhandled`); } diff --git a/client/src/app.tsx b/client/src/app.tsx index 1882010..6d3c5a4 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -5,13 +5,11 @@ import { Route, Router, route } from 'preact-router'; import { Provider } from 'react-redux' import createWs from './ws'; -import http from './http'; import Header from './components/header'; -import Home from './routes/home'; -import Bundles from './routes/bundles'; -import Admin from './routes/acp'; +import Offers from './routes/offers'; +import Acp from './routes/acp'; import Login from './routes/login'; import Search from './routes/search'; import List from './routes/list'; @@ -33,15 +31,14 @@ const Main = () => {
- + {/* ACP */} - - +
); diff --git a/client/src/components/bundle.tsx b/client/src/components/bundle.tsx deleted file mode 100644 index 09d8981..0000000 --- a/client/src/components/bundle.tsx +++ /dev/null @@ -1,58 +0,0 @@ -import { h } from 'preact'; -import { Square, CheckSquare } from 'react-feather'; -import { useState, useReducer } from 'preact/hooks'; - -import http from '../http'; - -function Item(item, state, dispatch) { - const icon = state.checkedIds.includes(item.id) - ? dispatch({ id: item.id})} /> - : dispatch({ id: item.id})} />; - - return ( -
- -

{item.description}

-
-
{item.price.toFixed(2)} {item.currency}
- {icon} -
- ); -} - -const reducer = (state, action) => { - if (state.checkedIds.includes(action.id)) { - return { - ...state, - checkedIds: state.checkedIds.filter(id => id !== action.id) - } - } - - return { - ...state, - checkedIds: [ - ...state.checkedIds, - action.id - ] - } -} - -export default function Bundle(bundle) { - const initialState = { checkedIds: bundle.items.map(i => i.id) } - const [state, dispatch] = useReducer(reducer, initialState) - - const items = bundle.items.map(item => Item(item, state, dispatch)); - - return ( -
-
-

{bundle.username} - {bundle.seller}

-

{bundle.items.length} items - {bundle.total.toFixed(2)} {bundle.currency}

-

Ships: {bundle.mentionsAustralia && 'AU'} {bundle.mentionsWorldwide && 'WORLD'}

-
-
- {items} -
-
- ) -} diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 1708f61..e006dfb 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -37,7 +37,6 @@ const Header = () => { const adminNav = () => ( ) diff --git a/client/src/routes/bundles.tsx b/client/src/routes/bundles.tsx deleted file mode 100644 index 713fa40..0000000 --- a/client/src/routes/bundles.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { h } from 'preact'; -import { useState, useEffect } from 'preact/hooks'; - -import http from './../http'; -import Bundle from './../components/bundle'; - -function Bundles() { - const [bundles, setBundles] = useState([]); - - useEffect(() => { - async function getBundles() { - const [error, bundles] = await http.getBundles(); - if (error) return console.error(error); - setBundles(bundles); - } - getBundles(); - }, []); - - if (!bundles.length) { - return ( -
- loading... -
- ); - } - - - return ( -
- {bundles.map(Bundle)} -
- ); -} - -export default Bundles; diff --git a/client/src/routes/home.tsx b/client/src/routes/home.tsx deleted file mode 100644 index 3c556ea..0000000 --- a/client/src/routes/home.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { h } from 'preact'; -import { useState, useEffect } from 'preact/hooks'; -import { route } from 'preact-router'; -import { useSelector, useDispatch } from 'react-redux' - -import http from './../http'; -import { State, offersState } from './../store'; - -import Offer from './../components/offer'; - -function Home() { - const dispatch = useDispatch() - const offers = useSelector((state: State) => state.offers); - - useEffect(() => { - async function getOffers() { - const [error, offers] = await http.getOffers(); - if (error) return console.error(error); - dispatch(offersState.actions.set(offers)); - if (offers.length === 0) route('/search'); - } - getOffers(); - }, []); - - if (!offers.length) { - return ( -
- loading... -
- ); - } - - - return ( -
- {offers.map(Offer)} -
- ); -} - -export default Home; diff --git a/client/src/routes/list.tsx b/client/src/routes/list.tsx index d050068..9d4ab3b 100644 --- a/client/src/routes/list.tsx +++ b/client/src/routes/list.tsx @@ -1,11 +1,8 @@ import { h } from 'preact'; -import { useState, useEffect } from 'preact/hooks'; import { useSelector } from 'react-redux' import { State } from './../store' -// import style from './style.less'; -import http from './../http'; import Want from './../components/want'; interface Props { diff --git a/client/src/ws.ts b/client/src/ws.ts index 4f57f4f..fbc9f89 100644 --- a/client/src/ws.ts +++ b/client/src/ws.ts @@ -34,6 +34,10 @@ export default function createSocket(dispatch) { send(['WantRm', { id }]); } + function sendOffers() { + send(['Offers', {}]); + } + function sendAcpOffers() { send(['AcpOffers', {}]); } @@ -104,6 +108,7 @@ export default function createSocket(dispatch) { console.log('websocket connected', SOCKET_URL) sendPing(); sendAcpOffers(); + sendOffers(); } function onError(event) { @@ -143,6 +148,8 @@ export default function createSocket(dispatch) { sendOfferStatusSet, sendWantRm, + sendOffers, + sendAcpOffers, sendAcpOfferStatusSet, };