Skip to content

Commit

Permalink
added supabase realtime functionality to update the balance in realti…
Browse files Browse the repository at this point in the history
…me and edited payto route to be only visible when session exists
  • Loading branch information
NamitBhutani committed May 13, 2023
1 parent 6279f97 commit e21bfe4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const actions: Actions = {
} //updating the balance of the vendor
};

//Loading logged in User's Balance to show in the form
//Loading logged in User's or Vendor's Balance to show in the form

export const load: PageServerLoad = async ({ locals }) => {
const session = await locals.getSession();
Expand All @@ -38,7 +38,7 @@ export const load: PageServerLoad = async ({ locals }) => {
.select('balance')
.eq('email', session?.user.email)
.single();
if (err) console.log(err + 'this error is from 2');
if (err) console.log(JSON.stringify(err) + 'this error is from 2');
else
return {
userBalance: loadData
Expand Down
18 changes: 14 additions & 4 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { supabase } from '$lib/supabaseClient';
import type { PageData } from './$types';
import QRCode from 'qrcode';
import { onMount } from 'svelte';
Expand All @@ -7,6 +8,7 @@
let qrDataURL: string;
let uuid: string;
let showParagraph: boolean = false;
let userBalanceVar: any;
onMount(async () => {
if (data?.session?.user.user_metadata.isVendor == true) {
uuid = data.session.user.id;
Expand All @@ -16,13 +18,21 @@
const toggleBalanceView = () => {
showParagraph = !showParagraph;
};
const userBalance = supabase
.channel('getUserBalance')
.on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'profiles' }, (payload) => {
userBalanceVar = payload.new.balance;
//console.log('Changes Received', userBalanceVar);
})
.subscribe();
</script>

<h1>Welcome to E-Coupons!</h1>
{#if data.session && data.session.user.user_metadata.isVendor === true}
{#if data.session !== null && data.session.user.user_metadata.isVendor === true}
<p>Welcome to the Vendor Screen, {data.session.user.user_metadata.username}!</p>
{#if showParagraph}
<p>Current Balance: {data.userBalance?.balance}</p>
<p>Current Balance: {userBalanceVar || data.userBalance?.balance}</p>
{/if}
<button on:click={toggleBalanceView}
>{#if showParagraph} Hide Balance {:else} Show Balance{/if}</button
Expand All @@ -33,10 +43,10 @@
<form method="POST" action="/logout">
<button type="submit"> Logout </button>
</form>
{:else if data.session && data.session.user.user_metadata.name != null}
{:else if data.session !== null && data.session.user.user_metadata.name !== null}
<p>Welcome to the User Screen, {data.session.user.user_metadata.name}!</p>
{#if showParagraph}
<p>Current Balance: {data.userBalance?.balance}</p>
<p>Current Balance: {userBalanceVar || data.userBalance?.balance}</p>
{/if}
<button on:click={toggleBalanceView}
>{#if showParagraph} Hide Balance {:else} Show Balance{/if}</button
Expand Down
5 changes: 4 additions & 1 deletion src/routes/payto/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { PageData } from './$types';
</script>

{#if data.vendorData[0].raw_user_meta_data.isVendor === true}
{#if data.session && data.vendorData[0].raw_user_meta_data.isVendor === true}
<form action="?/payto" method="POST">
<div class="grid">
<div>
Expand All @@ -18,6 +18,9 @@
</div>
<button type="submit">Pay</button>
</form>
{:else if !data.session}
<h1>Not Logged In</h1>
<a href="/login" role="button">Login</a>
{:else}
<h1>Invalid Vendor</h1>
<p>Vendor does not exist</p>
Expand Down

1 comment on commit e21bfe4

@vercel
Copy link

@vercel vercel bot commented on e21bfe4 May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

e-coupons-swd – ./

e-coupons-swd-intellomaniac.vercel.app
e-coupons-swd-git-main-intellomaniac.vercel.app
ecoupons.vercel.app

Please sign in to comment.