fix new freebie bug and first page detection
This commit is contained in:
parent
f23da4397f
commit
db8fcf78be
|
@ -1,5 +1,5 @@
|
||||||
import { ApolloClient, InMemoryCache, from, HttpLink } from '@apollo/client'
|
import { ApolloClient, InMemoryCache, from, HttpLink } from '@apollo/client'
|
||||||
import { isFirstPage } from './cursor'
|
import { decodeCursor, LIMIT } from './cursor'
|
||||||
import { RetryLink } from '@apollo/client/link/retry'
|
import { RetryLink } from '@apollo/client/link/retry'
|
||||||
|
|
||||||
const additiveLink = from([
|
const additiveLink = from([
|
||||||
|
@ -7,6 +7,17 @@ const additiveLink = from([
|
||||||
new HttpLink({ uri: '/api/graphql' })
|
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({
|
export default new ApolloClient({
|
||||||
uri: '/api/graphql',
|
uri: '/api/graphql',
|
||||||
link: additiveLink,
|
link: additiveLink,
|
||||||
|
@ -17,7 +28,7 @@ export default new ApolloClient({
|
||||||
moreItems: {
|
moreItems: {
|
||||||
keyArgs: ['sort', 'userId'],
|
keyArgs: ['sort', 'userId'],
|
||||||
merge (existing, incoming) {
|
merge (existing, incoming) {
|
||||||
if (incoming.cursor && isFirstPage(incoming.cursor)) {
|
if (isFirstPage(incoming.cursor, existing)) {
|
||||||
return incoming
|
return incoming
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +41,7 @@ export default new ApolloClient({
|
||||||
moreFlatComments: {
|
moreFlatComments: {
|
||||||
keyArgs: ['userId'],
|
keyArgs: ['userId'],
|
||||||
merge (existing, incoming) {
|
merge (existing, incoming) {
|
||||||
if (incoming.cursor && isFirstPage(incoming.cursor)) {
|
if (isFirstPage(incoming.cursor, existing)) {
|
||||||
return incoming
|
return incoming
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +54,7 @@ export default new ApolloClient({
|
||||||
notifications: {
|
notifications: {
|
||||||
keyArgs: false,
|
keyArgs: false,
|
||||||
merge (existing, incoming) {
|
merge (existing, incoming) {
|
||||||
if (incoming.cursor && isFirstPage(incoming.cursor)) {
|
if (isFirstPage(incoming.cursor, existing)) {
|
||||||
return incoming
|
return incoming
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,3 @@ export function nextCursorEncoded (cursor) {
|
||||||
cursor.offset += LIMIT
|
cursor.offset += LIMIT
|
||||||
return Buffer.from(JSON.stringify(cursor)).toString('base64')
|
return Buffer.from(JSON.stringify(cursor)).toString('base64')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isFirstPage (cursor) {
|
|
||||||
const decursor = decodeCursor(cursor)
|
|
||||||
return decursor.offset === LIMIT
|
|
||||||
}
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ DECLARE
|
||||||
BEGIN
|
BEGIN
|
||||||
PERFORM ASSERT_SERIALIZED();
|
PERFORM ASSERT_SERIALIZED();
|
||||||
|
|
||||||
SELECT (msats / 1000), id, "freePosts", "freeComments"
|
SELECT (msats / 1000), "freePosts", "freeComments"
|
||||||
INTO user_sats, free_posts, free_comments
|
INTO user_sats, free_posts, free_comments
|
||||||
FROM users WHERE id = user_id;
|
FROM users WHERE id = user_id;
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ BEGIN
|
||||||
INSERT INTO "Item" (title, url, text, "userId", "parentId", created_at, updated_at)
|
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;
|
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
|
IF parent_id IS NULL THEN
|
||||||
UPDATE users SET "freePosts" = "freePosts" - 1 WHERE id = user_id;
|
UPDATE users SET "freePosts" = "freePosts" - 1 WHERE id = user_id;
|
||||||
ELSE
|
ELSE
|
||||||
|
|
Loading…
Reference in New Issue