fix new freebie bug and first page detection

This commit is contained in:
keyan 2021-09-09 11:44:01 -05:00
parent f23da4397f
commit db8fcf78be
3 changed files with 17 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { ApolloClient, InMemoryCache, from, HttpLink } from '@apollo/client'
import { isFirstPage } from './cursor'
import { decodeCursor, LIMIT } from './cursor'
import { RetryLink } from '@apollo/client/link/retry'
const additiveLink = from([
@ -7,6 +7,17 @@ const additiveLink = from([
new HttpLink({ uri: '/api/graphql' })
])
function isFirstPage (cursor, existing) {
if (cursor) {
const decursor = decodeCursor(cursor)
return decursor.offset === LIMIT
} else {
// we don't have anything cached, or our existing items are less than
// or equal to a full page TODO test for off by one
return !existing || !existing.items || existing.items.length < LIMIT
}
}
export default new ApolloClient({
uri: '/api/graphql',
link: additiveLink,
@ -17,7 +28,7 @@ export default new ApolloClient({
moreItems: {
keyArgs: ['sort', 'userId'],
merge (existing, incoming) {
if (incoming.cursor && isFirstPage(incoming.cursor)) {
if (isFirstPage(incoming.cursor, existing)) {
return incoming
}
@ -30,7 +41,7 @@ export default new ApolloClient({
moreFlatComments: {
keyArgs: ['userId'],
merge (existing, incoming) {
if (incoming.cursor && isFirstPage(incoming.cursor)) {
if (isFirstPage(incoming.cursor, existing)) {
return incoming
}
@ -43,7 +54,7 @@ export default new ApolloClient({
notifications: {
keyArgs: false,
merge (existing, incoming) {
if (incoming.cursor && isFirstPage(incoming.cursor)) {
if (isFirstPage(incoming.cursor, existing)) {
return incoming
}

View File

@ -14,8 +14,3 @@ export function nextCursorEncoded (cursor) {
cursor.offset += LIMIT
return Buffer.from(JSON.stringify(cursor)).toString('base64')
}
export function isFirstPage (cursor) {
const decursor = decodeCursor(cursor)
return decursor.offset === LIMIT
}

View File

@ -106,7 +106,7 @@ DECLARE
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT (msats / 1000), id, "freePosts", "freeComments"
SELECT (msats / 1000), "freePosts", "freeComments"
INTO user_sats, free_posts, free_comments
FROM users WHERE id = user_id;
@ -119,7 +119,7 @@ BEGIN
INSERT INTO "Item" (title, url, text, "userId", "parentId", created_at, updated_at)
VALUES (title, url, text, user_id, parent_id, now_utc(), now_utc()) RETURNING * INTO item;
IF freebie THEN
IF freebie = true THEN
IF parent_id IS NULL THEN
UPDATE users SET "freePosts" = "freePosts" - 1 WHERE id = user_id;
ELSE