From ab5cb3c55fd629d151cea91b8863df9671157b34 Mon Sep 17 00:00:00 2001 From: Moataz Nabil Date: Tue, 3 Dec 2024 21:08:50 +0100 Subject: [PATCH] add github actions --- src/App.tsx | 1 - src/database/init.ts | 25 +++++++++++++++++++++++++ src/main.tsx | 14 +++++++++----- src/store/useStore.ts | 5 ++++- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/database/init.ts diff --git a/src/App.tsx b/src/App.tsx index bb7f71c..7e623df 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,3 @@ -import React from 'react'; import { HashRouter as Router, Routes, Route } from 'react-router-dom'; import { Navbar } from './components/layout/Navbar'; import { Navigation } from './components/layout/Navigation'; diff --git a/src/database/init.ts b/src/database/init.ts new file mode 100644 index 0000000..32aa2e9 --- /dev/null +++ b/src/database/init.ts @@ -0,0 +1,25 @@ +import { UserDB } from './db'; +import { predefinedUsers } from '../data/users'; + +export async function initializeDatabase() { + try { + // Check if test user exists + const testUser = await UserDB.findByEmail('test@example.com'); + + if (!testUser) { + // Initialize predefined users if they don't exist + for (const user of predefinedUsers) { + const existingUser = await UserDB.findByEmail(user.email); + if (!existingUser) { + await UserDB.create({ + email: user.email, + password: user.password, + name: user.name + }); + } + } + } + } catch (error) { + console.error('Failed to initialize database:', error); + } +} \ No newline at end of file diff --git a/src/main.tsx b/src/main.tsx index ea9e363..63fc3b0 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,9 +2,13 @@ import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; import App from './App.tsx'; import './index.css'; +import { initializeDatabase } from './database/init'; -createRoot(document.getElementById('root')!).render( - - - -); +// Initialize database before rendering +initializeDatabase().then(() => { + createRoot(document.getElementById('root')!).render( + + + + ); +}); \ No newline at end of file diff --git a/src/store/useStore.ts b/src/store/useStore.ts index b61774a..7022f31 100644 --- a/src/store/useStore.ts +++ b/src/store/useStore.ts @@ -248,13 +248,16 @@ export const useStore = create((set, get) => ({ set((state) => ({ orders: [...state.orders, order], + cart: [], // Clear the cart after successful order })); } }, getOrders: async () => { const { auth } = get(); if (!auth.user) return []; - return await OrderDB.findByUser(auth.user.id); + const orders = await OrderDB.findByUser(auth.user.id); + set({ orders }); // Update the orders in the store + return orders; }, // Auth