Skip to content

Commit

Permalink
Always reload page on switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Sep 12, 2024
1 parent d695b6e commit a6555ff
Showing 1 changed file with 2 additions and 50 deletions.
52 changes: 2 additions & 50 deletions components/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,56 +113,8 @@ const AccountListRow = ({ account, ...props }) => {
setMeAnon(account.id === USER_ID.anon)
}

// account changes on some pages require a hard reload
// 1) anons don't have access to some pages
// ( search for 'authRequired: true' to find all such pages )
const privatePages = ['/notifications', '/territory', '/items/[id]/edit', '/referrals', '/satistics', '/settings', '/wallet', '/~/edit']
if (anonRow && privatePages.some(p => router.pathname.startsWith(p))) {
router.reload()
return
}

const authPages = ['/signup', '/login']
// 2) if we're on /signup or /login, reload so we get redirected to the callback url
if (!anonRow && authPages.some(p => router.pathname.startsWith(p))) {
router.reload()
return
}

// 3) not everyone has access to every item
if (router.asPath.startsWith('/items')) {
const itemId = router.asPath.split('/')[2]
// check if we have access to the item
const { item } = client.cache.readQuery({
query: ITEM_FULL,
variables: { id: itemId }
})

const isMine = item.userId === account.id
const isPrivate = item.invoice && item.invoice.actionState !== 'PAID'

if (!isMine && isPrivate) {
router.reload()
return
}
}

await client.refetchQueries({
include: 'active',
onQueryUpdated: async (query) => {
try {
return await query.refetch()
} catch (err) {
const code = err.graphQLErrors?.[0]?.extensions?.code
if ([E_FORBIDDEN, E_UNAUTHENTICATED, E_BAD_INPUT].includes(code)) {
return
}

// never throw but log unexpected errors
console.error(err)
}
}
})
// reload whatever page we're on to avoid any bugs due to missing authorization etc.
router.reload()
}

return (
Expand Down

0 comments on commit a6555ff

Please sign in to comment.