Skip to content

Commit

Permalink
Add wallet limit banner
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Dec 20, 2023
1 parent 9f7f093 commit 62a647f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ export default {

await models.user.update({ where: { id: me.id }, data: { hideWelcomeBanner: true } })
return true
},
hideWalletLimitBanner: async (parent, data, { me, models }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
}

await models.user.update({ where: { id: me.id }, data: { hideWalletLimitBanner: true } })
return true
}
},

Expand Down
2 changes: 2 additions & 0 deletions api/typeDefs/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default gql`
unlinkAuth(authType: String!): AuthMethods!
linkUnverifiedEmail(email: String!): Boolean
hideWelcomeBanner: Boolean
hideWalletLimitBanner: Boolean
subscribeUserPosts(id: ID): User
subscribeUserComments(id: ID): User
toggleMute(id: ID): User
Expand Down Expand Up @@ -103,6 +104,7 @@ export default gql`
"""
lastCheckedJobs: String
hideWelcomeBanner: Boolean!
hideWalletLimitBanner: Boolean!
tipPopover: Boolean!
upvotePopover: Boolean!
hasInvites: Boolean!
Expand Down
51 changes: 50 additions & 1 deletion components/banners.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import styles from './banners.module.css'
import { useEffect, useState } from 'react'
import { useMe } from '../components/me'
import { useMutation } from '@apollo/client'
import { WELCOME_BANNER_MUTATION } from '../fragments/users'
import { WALLET_LIMIT_BANNER_MUTATION, WELCOME_BANNER_MUTATION } from '../fragments/users'
import { useToast } from '../components/toast'
import { BALANCE_LIMIT_MSATS } from '../lib/constants'
import { msatsToSats, numWithUnits } from '../lib/format'

export default function WelcomeBanner () {
const me = useMe()
Expand Down Expand Up @@ -65,3 +67,50 @@ export default function WelcomeBanner () {
</Alert>
)
}

export function WalletLimitBanner ({ dismissible }) {
// TODO refactor this since it's mostly the same as the code for the welcome banner
const me = useMe()
const toaster = useToast()
const [hidden, setHidden] = useState(true)
const handleClose = async () => {
window.localStorage.setItem('hideWalletLimitBanner', true)
setHidden(true)
if (me) {
try {
await hideWalletLimitBanner()
} catch (err) {
console.log(err)
toaster.danger('mutation failed')
}
}
}
const [hideWalletLimitBanner] = useMutation(WALLET_LIMIT_BANNER_MUTATION, {
update (cache) {
cache.modify({
id: `User:${me.id}`,
fields: {
hideWalletLimitBanner () {
return true
}
}
})
}
})
useEffect(() => {
setHidden(me?.privates?.hideWalletLimitBanner || (!me && window.localStorage.getItem('hideWalletLimitBanner')))
}, [me?.privates?.hideWalletLimitBanner])

const limitReached = me.privates?.sats >= BALANCE_LIMIT_MSATS
if (!limitReached || (hidden && dismissible)) return

return (
<Alert className={styles.banner} key='info' variant='warning' onClose={handleClose} dismissible={dismissible}>
<p>
Your wallet is over the current limit ({numWithUnits(msatsToSats(BALANCE_LIMIT_MSATS))}).<br />
You will not be able to deposit any more funds or receive sats from outside of SN.<br />
Please withdraw sats to restore full wallet functionality.
</p>
</Alert>
)
}
7 changes: 7 additions & 0 deletions fragments/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ gql`
}
`

export const WALLET_LIMIT_BANNER_MUTATION =
gql`
mutation hideWalletLimitBanner {
hideWalletLimitBanner
}
`

export const USER_SUGGESTIONS =
gql`
query userSuggestions($q: String!, $limit: Limit) {
Expand Down
3 changes: 3 additions & 0 deletions pages/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import CameraIcon from '../svgs/camera-line.svg'
import { useShowModal } from '../components/modal'
import { useField } from 'formik'
import { useToast } from '../components/toast'
import { WalletLimitBanner } from '../components/banners'

export const getServerSideProps = getGetServerSideProps({ authRequired: true })

Expand All @@ -49,6 +50,7 @@ export default function Wallet () {
return (
<CenterLayout>
<YouHaveSats />
<WalletLimitBanner />
<WalletForm />
<WalletHistory />
</CenterLayout>
Expand Down Expand Up @@ -116,6 +118,7 @@ export function FundForm () {
<>
<YouHaveSats />
<div className='w-100 py-5'>
<WalletLimitBanner />
{me && showAlert &&
<Alert
variant='success' dismissible onClose={() => {
Expand Down
3 changes: 2 additions & 1 deletion pages/~/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Items from '../../components/items'
import Layout from '../../components/layout'
import { SUB_FULL, SUB_ITEMS } from '../../fragments/subs'
import Snl from '../../components/snl'
import WelcomeBanner from '../../components/banners'
import WelcomeBanner, { WalletLimitBanner } from '../../components/banners'
import { AccordianCard } from '../../components/accordian-item'
import Text from '../../components/text'
import { useMe } from '../../components/me'
Expand Down Expand Up @@ -72,6 +72,7 @@ export default function Sub ({ ssrData }) {
<>
<Snl />
<WelcomeBanner />
<WalletLimitBanner dismissible />
</>)}
<Items ssrData={ssrData} variables={variables} />
</Layout>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "users" ADD COLUMN "hideWalletLimitBanner" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ model User {
followers UserSubscription[] @relation("follower")
followees UserSubscription[] @relation("followee")
hideWelcomeBanner Boolean @default(false)
hideWalletLimitBanner Boolean @default(false)
diagnostics Boolean @default(false)
hideIsContributor Boolean @default(false)
muters Mute[] @relation("muter")
Expand Down

0 comments on commit 62a647f

Please sign in to comment.