From a1d842c7a3283a35c2c0de56f8917198aca55ae9 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 20 May 2021 16:32:59 -0500 Subject: [PATCH] fund error on upvote --- components/fund-error.js | 47 ++++++++++++++++++++++++++++++++++++++++ components/upvote.js | 13 +++++++++-- pages/_app.js | 10 ++++++--- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 components/fund-error.js diff --git a/components/fund-error.js b/components/fund-error.js new file mode 100644 index 00000000..3ffa4026 --- /dev/null +++ b/components/fund-error.js @@ -0,0 +1,47 @@ +import { Button, Modal } from 'react-bootstrap' +import React, { useState, useCallback, useContext } from 'react' +import Link from 'next/link' + +export const FundErrorContext = React.createContext({ + error: null, + toggleError: () => {} +}) + +export function FundErrorProvider ({ children }) { + const [error, setError] = useState(false) + + const contextValue = { + error, + setError: useCallback(e => setError(e), []) + } + + return ( + + {children} + + ) +} + +export function useFundError () { + const { error, setError } = useContext(FundErrorContext) + return { error, setError } +} + +export function FundErrorModal () { + const { error, setError } = useFundError() + return ( + setError(false)} + > + +

you're out of sats!

+
+ + + +
+
+
+ ) +} diff --git a/components/upvote.js b/components/upvote.js index 8b97d68f..3f98fe25 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -3,9 +3,11 @@ import UpArrow from '../svgs/lightning-arrow.svg' import styles from './upvote.module.css' import { gql, useMutation } from '@apollo/client' import { signIn, useSession } from 'next-auth/client' +import { useFundError } from './fund-error' export default function UpVote ({ itemId, meSats, className }) { const [session] = useSession() + const { setError } = useFundError() const [vote] = useMutation( gql` mutation vote($id: ID!, $sats: Int!) { @@ -45,8 +47,15 @@ export default function UpVote ({ itemId, meSats, className }) { session ? async () => { if (!itemId) return - const { error } = await vote({ variables: { id: itemId, sats: 1 } }) - if (error) { + try { + await vote({ variables: { id: itemId, sats: 1 } }) + } catch (error) { + console.log(error.toString()) + if (error.toString().includes('insufficient funds')) { + console.log('hjo') + setError(true) + return + } throw new Error({ message: error.toString() }) } diff --git a/pages/_app.js b/pages/_app.js index cda7b201..08e185e3 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -1,6 +1,7 @@ import '../styles/globals.scss' import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client' import { Provider } from 'next-auth/client' +import { FundErrorModal, FundErrorProvider } from '../components/fund-error' const client = new ApolloClient({ uri: '/api/graphql', @@ -10,9 +11,12 @@ const client = new ApolloClient({ function MyApp ({ Component, pageProps }) { return ( - - - + + + + + + ) }