diff --git a/src/context/AppContext.tsx b/src/context/AppContext.tsx index 3822687..f7eeda8 100644 --- a/src/context/AppContext.tsx +++ b/src/context/AppContext.tsx @@ -104,12 +104,22 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { return; } - // Associar serviços e barbeiros às respetivas shops, simulando um INNER JOIN nativo do SQL + // Query 4: Obtém a listagem global de Produtos (tabela 'products') + const { data: productsData, error: productsError } = await supabase + .from('products') + .select('*'); + + if (productsError) { + console.error("Erro ao buscar products:", productsError); + return; + } + + // Associar serviços, barbeiros e produtos às respetivas shops, simulando um INNER JOIN nativo do SQL const shopsWithServices = shopsData.map((shop) => ({ ...shop, // Relaciona a 'foreign key' (shop_id) com o resgistro primário (shop.id) services: servicesData.filter((s) => s.shop_id === shop.id), - products: [], + products: productsData.filter((p) => p.shop_id === shop.id), barbers: barbersData.filter((b) => b.shop_id === shop.id), })); diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index 06728a2..c4d3f61 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -95,6 +95,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const { data: servicesData } = await supabase.from('services').select('*'); const { data: barbersData } = await supabase.from('barbers').select('*'); + const { data: productsData } = await supabase.from('products').select('*'); const fetchedShops: BarberShop[] = shopsData.map((shop) => ({ id: shop.id, @@ -111,7 +112,14 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { duration: s.duration ?? 30, barberIds: s.barber_ids ?? [], })), - products: [], + products: (productsData ?? []) + .filter((p) => p.shop_id === shop.id) + .map((p) => ({ + id: p.id, + name: p.name, + price: p.price ?? 0, + stock: p.stock ?? 0, + })), barbers: (barbersData ?? []) .filter((b) => b.shop_id === shop.id) .map((b) => ({