From 8fca8723820d6e34e7f94b5e7416a2baef39a803 Mon Sep 17 00:00:00 2001 From: Kawsar Ahmed Date: Fri, 11 Oct 2024 07:10:05 +0600 Subject: [PATCH] Refactor and improve mixpanel (#10) * Improve code and mixpanel * Fix * data preload --- package.json | 2 +- src/lib/elements/Footer.svelte | 70 +++++++++++++++++ src/lib/utils/mixpanel.ts | 10 +++ src/routes/(app)/+layout.svelte | 86 +++------------------ src/routes/(app)/+page.svelte | 16 ++-- src/routes/(app)/history/+page.svelte | 3 +- src/routes/(app)/history/trash/+page.svelte | 2 +- src/routes/auth/+layout.svelte | 10 --- 8 files changed, 106 insertions(+), 93 deletions(-) create mode 100644 src/lib/elements/Footer.svelte create mode 100644 src/lib/utils/mixpanel.ts diff --git a/package.json b/package.json index 7fb03dd..d883a98 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "@supabase/ssr": "^0.5.0", "@supabase/supabase-js": "^2.44.3", "dayjs": "^1.11.11", - "mixpanel-browser": "^2.53.0", + "mixpanel-browser": "^2.55.1", "svelte-persisted-store": "^0.11.0", "svelte-sonner": "^0.3.27", "tippy.js": "^6.3.7" diff --git a/src/lib/elements/Footer.svelte b/src/lib/elements/Footer.svelte new file mode 100644 index 0000000..c4c47fa --- /dev/null +++ b/src/lib/elements/Footer.svelte @@ -0,0 +1,70 @@ + + + diff --git a/src/lib/utils/mixpanel.ts b/src/lib/utils/mixpanel.ts new file mode 100644 index 0000000..9e67c51 --- /dev/null +++ b/src/lib/utils/mixpanel.ts @@ -0,0 +1,10 @@ +import { browser, dev } from '$app/environment' +import mixpanel from 'mixpanel-browser' + +mixpanel.init('4f9140ba92bf9817723887501ce35853', { + track_pageview: true, + persistence: 'localStorage' +}) +if (dev) mixpanel.disable() + +export default mixpanel diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index 3d6ed05..92bc789 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -1,36 +1,28 @@
@@ -54,68 +46,14 @@ -
- - Home - - - Cost History - - - {#if showSettings} -
- -
{ - loading = true - return async ({ update }) => { - await update() - loading = false - } - }} - > - -
-
- {/if} -
+
diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 16109cd..7b6818b 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -6,7 +6,7 @@ import { MAX_HISTORY, MAX_PAPER, paperFields } from '$lib/utils/constants' import { addHistory, calculateCost, getTotalHistory, type Paper } from '$lib/utils/services' import { makeid } from '$lib/utils/tools' - import mixpanel from 'mixpanel-browser' + import mixpanel from '$lib/utils/mixpanel' import { onMount, tick } from 'svelte' import { toast } from 'svelte-sonner' @@ -18,7 +18,7 @@ let inputs: NodeListOf | null let inputGroupRef: HTMLDivElement let focusedIndex = 0 - let customer_name = '' + let product_name = '' let isSavingHistory = false const addPaper = async () => { @@ -61,10 +61,14 @@ } const saveHistory = async () => { + if (!product_name) { + toast.warning('Product name is required!') + return + } if ($totalHistoryStore < MAX_HISTORY) { isSavingHistory = true const response = await addHistory({ - name: customer_name, + name: product_name, final_price: finalPrice, papers: paperCount, user: data.user.id @@ -85,7 +89,7 @@ paperCount = [{ ...paperFields, id: makeid(5) }] finalPrice = 0 focusedIndex = 0 - customer_name = '' + product_name = '' perPaperResult.clear() getAllInputs() setFocus() @@ -118,7 +122,7 @@ .findIndex((id) => focusedInputID == id) } - function handleKeyDown(event: KeyboardEvent) { + const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter' && inputs) { event.preventDefault() focusedIndex++ @@ -158,7 +162,7 @@
diff --git a/src/routes/(app)/history/trash/+page.svelte b/src/routes/(app)/history/trash/+page.svelte index abe04be..98de136 100644 --- a/src/routes/(app)/history/trash/+page.svelte +++ b/src/routes/(app)/history/trash/+page.svelte @@ -5,7 +5,7 @@ import { deleteHistory, emptyTrashData, restoreHistory } from '$lib/utils/services.js' import { sortedByCreatedAt } from '$lib/utils/tools.js' import Icon from '@iconify/svelte' - import mixpanel from 'mixpanel-browser' + import mixpanel from '$lib/utils/mixpanel' import { toast } from 'svelte-sonner' export let data diff --git a/src/routes/auth/+layout.svelte b/src/routes/auth/+layout.svelte index dbabc40..a371db5 100644 --- a/src/routes/auth/+layout.svelte +++ b/src/routes/auth/+layout.svelte @@ -1,17 +1,7 @@