Skip to content

Commit

Permalink
Save history with user id and shows email
Browse files Browse the repository at this point in the history
  • Loading branch information
prokawsar committed Aug 29, 2024
1 parent 75acd35 commit 2e3035b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/lib/elements/About.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<script lang="ts">
import { version } from '$app/environment'
import type { User } from '@supabase/supabase-js'
export let user: User | null = null
</script>

<div class="flex flex-col text-center items-center">
<img class="h-10 w-10 rounded-full" src="/logo.jpeg" alt="logo" />
<p class="text-lg font-bold">Paper Cost</p>

<p class="text-sm">
Logged in as:
<span class="text-teal-500">
{user?.email}
</span>
</p>
<p class="mt-5">Developed by</p>
<div class="text-sm flex flex-col">
<p>Sheba Queue</p>
Expand Down
6 changes: 4 additions & 2 deletions src/lib/utils/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type CostHistoryType = {
name: string
papers: Paper[]
final_price: number
user: string
created_at?: string
}

Expand All @@ -37,8 +38,9 @@ export const get40Percent = (cost: number) => {
export const addHistory = async (history: CostHistoryType) => {
const { data, error } = await supabase.from('history').insert({
name: history.name || '',
papers: history.papers || [],
final_price: history.final_price || 100
papers: history.papers,
final_price: history.final_price,
user: history.user
})
if (!error) {
return data
Expand Down
3 changes: 2 additions & 1 deletion src/routes/(app)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { LayoutServerLoad } from './$types'

export const load: LayoutServerLoad = async ({ cookies }) => {
export const load: LayoutServerLoad = async ({ locals: { user }, cookies }) => {
return {
user,
cookies: cookies.getAll()
}
}
4 changes: 3 additions & 1 deletion src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import BrandTitle from '$lib/elements/BrandTitle.svelte'
import About from '$lib/elements/About.svelte'
export let data
//Import Mixpanel SDK
mixpanel.init(PUBLIC_MIX_TOKEN, {
debug: false,
Expand All @@ -38,7 +40,7 @@
{/if}
{#if showAbout}
<Modal bind:show={showAbout}>
<About />
<About user={data.user} />
</Modal>
{/if}
<div class="flex flex-col h-[92%]">
Expand Down
8 changes: 4 additions & 4 deletions src/routes/(app)/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const load: LayoutLoad = async ({ fetch, data, depends }) => {
// const {
// data: { session }
// } = await supabase.auth.getSession()
// const {
// data: { user }
// } = await supabase.auth.getUser()
const {
data: { user }
} = await supabase.auth.getUser()

return { supabase }
return { supabase, user }
}
6 changes: 6 additions & 0 deletions src/routes/(app)/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { redirect, type Actions } from '@sveltejs/kit'

export const load = ({ locals: { user } }) => {
return {
user
}
}

export const actions: Actions = {
logout: async ({ locals: { supabase } }) => {
const { error } = await supabase.auth.signOut()
Expand Down
5 changes: 4 additions & 1 deletion src/routes/(app)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import { onMount, tick } from 'svelte'
import { toast } from 'svelte-sonner'
export let data
let paperCount: Paper[] = [{ ...paperFields, id: makeid(5) }]
let perPaperResult: Map<string, number> = new Map()
let finalPrice: number = 0
Expand Down Expand Up @@ -64,7 +66,8 @@
const response = await addHistory({
name: customer_name,
final_price: finalPrice,
papers: paperCount
papers: paperCount,
user: data.user.id
})
if (response && response?.message.indexOf('TypeError') != -1) {
Expand Down
10 changes: 7 additions & 3 deletions src/routes/(app)/history/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { supabase } from '$lib/db/supabaseClient'
export async function load({ locals: { user, supabase } }) {
if (!user) throw new Error('Unautherized request')

export async function load() {
const { data } = await supabase.from('history').select().is('deleted_at', null)
const { data } = await supabase
.from('history')
.select()
.eq('user', user.id)
.is('deleted_at', null)
return {
histories: data ?? []
}
Expand Down
10 changes: 7 additions & 3 deletions src/routes/(app)/history/trash/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { supabase } from '$lib/db/supabaseClient'
export async function load({ locals: { user, supabase } }) {
if (!user) throw new Error('Unautherized request')

export async function load() {
const { data } = await supabase.from('history').select().not('deleted_at', 'is', null)
const { data } = await supabase
.from('history')
.select()
.eq('user', user.id)
.not('deleted_at', 'is', null)
return {
histories: data ?? []
}
Expand Down

0 comments on commit 2e3035b

Please sign in to comment.