Skip to content

Commit db8fcf7

Browse files
committed
fix new freebie bug and first page detection
1 parent f23da43 commit db8fcf7

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

lib/apollo.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { ApolloClient, InMemoryCache, from, HttpLink } from '@apollo/client'
2-
import { isFirstPage } from './cursor'
2+
import { decodeCursor, LIMIT } from './cursor'
33
import { RetryLink } from '@apollo/client/link/retry'
44

55
const additiveLink = from([
66
new RetryLink(),
77
new HttpLink({ uri: '/api/graphql' })
88
])
99

10+
function isFirstPage (cursor, existing) {
11+
if (cursor) {
12+
const decursor = decodeCursor(cursor)
13+
return decursor.offset === LIMIT
14+
} else {
15+
// we don't have anything cached, or our existing items are less than
16+
// or equal to a full page TODO test for off by one
17+
return !existing || !existing.items || existing.items.length < LIMIT
18+
}
19+
}
20+
1021
export default new ApolloClient({
1122
uri: '/api/graphql',
1223
link: additiveLink,
@@ -17,7 +28,7 @@ export default new ApolloClient({
1728
moreItems: {
1829
keyArgs: ['sort', 'userId'],
1930
merge (existing, incoming) {
20-
if (incoming.cursor && isFirstPage(incoming.cursor)) {
31+
if (isFirstPage(incoming.cursor, existing)) {
2132
return incoming
2233
}
2334

@@ -30,7 +41,7 @@ export default new ApolloClient({
3041
moreFlatComments: {
3142
keyArgs: ['userId'],
3243
merge (existing, incoming) {
33-
if (incoming.cursor && isFirstPage(incoming.cursor)) {
44+
if (isFirstPage(incoming.cursor, existing)) {
3445
return incoming
3546
}
3647

@@ -43,7 +54,7 @@ export default new ApolloClient({
4354
notifications: {
4455
keyArgs: false,
4556
merge (existing, incoming) {
46-
if (incoming.cursor && isFirstPage(incoming.cursor)) {
57+
if (isFirstPage(incoming.cursor, existing)) {
4758
return incoming
4859
}
4960

lib/cursor.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,3 @@ export function nextCursorEncoded (cursor) {
1414
cursor.offset += LIMIT
1515
return Buffer.from(JSON.stringify(cursor)).toString('base64')
1616
}
17-
18-
export function isFirstPage (cursor) {
19-
const decursor = decodeCursor(cursor)
20-
return decursor.offset === LIMIT
21-
}

prisma/migrations/20210908193444_tips/migration.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ DECLARE
106106
BEGIN
107107
PERFORM ASSERT_SERIALIZED();
108108

109-
SELECT (msats / 1000), id, "freePosts", "freeComments"
109+
SELECT (msats / 1000), "freePosts", "freeComments"
110110
INTO user_sats, free_posts, free_comments
111111
FROM users WHERE id = user_id;
112112

@@ -119,7 +119,7 @@ BEGIN
119119
INSERT INTO "Item" (title, url, text, "userId", "parentId", created_at, updated_at)
120120
VALUES (title, url, text, user_id, parent_id, now_utc(), now_utc()) RETURNING * INTO item;
121121

122-
IF freebie THEN
122+
IF freebie = true THEN
123123
IF parent_id IS NULL THEN
124124
UPDATE users SET "freePosts" = "freePosts" - 1 WHERE id = user_id;
125125
ELSE

0 commit comments

Comments
 (0)