From 9c4d74888f367664e303d6557f182a567570bfe5 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 22 Sep 2022 15:42:04 -0500 Subject: [PATCH] add borderland --- api/resolvers/item.js | 20 +++++++++++++++++ api/typeDefs/item.js | 1 + components/items-mixed.js | 47 +++++++++++++++++++++++++++++++++++++++ fragments/items.js | 13 +++++++++++ lib/apollo.js | 13 +++++++++++ pages/borderland.js | 32 ++++++++++++++++++++++++++ pages/outlawed.js | 45 ++----------------------------------- 7 files changed, 128 insertions(+), 43 deletions(-) create mode 100644 components/items-mixed.js create mode 100644 pages/borderland.js diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 377c23c0..78a09ade 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -292,6 +292,26 @@ export default { items } }, + borderlandItems: async (parent, { cursor }, { me, models }) => { + const decodedCursor = decodeCursor(cursor) + const notMine = () => { + return me ? ` AND "userId" <> ${me.id} ` : '' + } + + const items = await models.$queryRaw(` + ${SELECT} + FROM "Item" + WHERE "Item"."weightedVotes" - "Item"."weightedDownVotes" < 0 + AND "Item"."weightedVotes" - "Item"."weightedDownVotes" > -${ITEM_FILTER_THRESHOLD} + ${notMine()} + ORDER BY created_at DESC + OFFSET $1 + LIMIT ${LIMIT}`, decodedCursor.offset) + return { + cursor: items.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, + items + } + }, moreFlatComments: async (parent, { cursor, name, sort, within }, { me, models }) => { const decodedCursor = decodeCursor(cursor) diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 1e112a68..6b3bf2bd 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -13,6 +13,7 @@ export default gql` auctionPosition(sub: String, id: ID, bid: Int!): Int! itemRepetition(parentId: ID): Int! outlawedItems(cursor: String): Items + borderlandItems(cursor: String): Items } type ItemActResult { diff --git a/components/items-mixed.js b/components/items-mixed.js new file mode 100644 index 00000000..efa3f79f --- /dev/null +++ b/components/items-mixed.js @@ -0,0 +1,47 @@ +import { useRouter } from 'next/router' +import React from 'react' +import { ignoreClick } from '../lib/clicks' +import Comment from './comment' +import Item from './item' +import ItemJob from './item-job' +import { ItemsSkeleton } from './items' +import styles from './items.module.css' +import MoreFooter from './more-footer' + +export default function MixedItems ({ rank, items, cursor, fetchMore }) { + const router = useRouter() + return ( + <> +
+ {items.map((item, i) => ( + + {item.parentId + ? ( + <>
+
{ + if (ignoreClick(e)) { + return + } + router.push({ + pathname: '/items/[id]', + query: { id: item.root.id, commentId: item.id } + }, `/items/${item.root.id}`) + }} + > + +
+ ) + : (item.maxBid + ? + : )} + + ))} +
+ } + /> + + ) +} diff --git a/fragments/items.js b/fragments/items.js index d2b92a0a..acdecb8d 100644 --- a/fragments/items.js +++ b/fragments/items.js @@ -82,6 +82,19 @@ export const OUTLAWED_ITEMS = gql` } }` +export const BORDERLAND_ITEMS = gql` + ${ITEM_FIELDS} + + query borderlandItems($cursor: String) { + borderlandItems(cursor: $cursor) { + cursor + items { + ...ItemFields + text + } + } + }` + export const POLL_FIELDS = gql` fragment PollFields on Item { poll { diff --git a/lib/apollo.js b/lib/apollo.js index ef9bb49d..8a50c84d 100644 --- a/lib/apollo.js +++ b/lib/apollo.js @@ -65,6 +65,19 @@ export default function getApolloClient () { } } }, + borderlandItems: { + keyArgs: [], + merge (existing, incoming) { + if (isFirstPage(incoming.cursor, existing?.items)) { + return incoming + } + + return { + cursor: incoming.cursor, + items: [...(existing?.items || []), ...incoming.items] + } + } + }, search: { keyArgs: ['q'], merge (existing, incoming) { diff --git a/pages/borderland.js b/pages/borderland.js new file mode 100644 index 00000000..a3d6bd2b --- /dev/null +++ b/pages/borderland.js @@ -0,0 +1,32 @@ +import Layout from '../components/layout' +import { ItemsSkeleton } from '../components/items' +import { getGetServerSideProps } from '../api/ssrApollo' +import { BORDERLAND_ITEMS } from '../fragments/items' +import { useQuery } from '@apollo/client' +import MixedItems from '../components/items-mixed' + +export const getServerSideProps = getGetServerSideProps(BORDERLAND_ITEMS) + +export default function Index ({ data: { borderlandItems: { items, cursor } } }) { + return ( + + + + ) +} + +function Items ({ rank, items, cursor }) { + const { data, fetchMore } = useQuery(BORDERLAND_ITEMS) + + if (!data && !items) { + return + } + + if (data) { + ({ borderlandItems: { items, cursor } } = data) + } + + return +} diff --git a/pages/outlawed.js b/pages/outlawed.js index 086a6158..48c62a92 100644 --- a/pages/outlawed.js +++ b/pages/outlawed.js @@ -3,14 +3,7 @@ import { ItemsSkeleton } from '../components/items' import { getGetServerSideProps } from '../api/ssrApollo' import { OUTLAWED_ITEMS } from '../fragments/items' import { useQuery } from '@apollo/client' -import React from 'react' -import styles from '../components/items.module.css' -import MoreFooter from '../components/more-footer' -import Item from '../components/item' -import ItemJob from '../components/item-job' -import Comment from '../components/comment' -import { ignoreClick } from '../lib/clicks' -import { useRouter } from 'next/router' +import MixedItems from '../components/items-mixed' export const getServerSideProps = getGetServerSideProps(OUTLAWED_ITEMS) @@ -26,7 +19,6 @@ export default function Index ({ data: { outlawedItems: { items, cursor } } }) { function Items ({ rank, items, cursor }) { const { data, fetchMore } = useQuery(OUTLAWED_ITEMS) - const router = useRouter() if (!data && !items) { return @@ -36,38 +28,5 @@ function Items ({ rank, items, cursor }) { ({ outlawedItems: { items, cursor } } = data) } - return ( - <> -
- {items.map((item, i) => ( - - {item.parentId - ? ( - <>
-
{ - if (ignoreClick(e)) { - return - } - router.push({ - pathname: '/items/[id]', - query: { id: item.root.id, commentId: item.id } - }, `/items/${item.root.id}`) - }} - > - -
- ) - : (item.maxBid - ? - : )} - - ))} -
- } - /> - - ) + return }