From 23171b0d022dbc521277bc1a3e2ebede3e609ba9 Mon Sep 17 00:00:00 2001 From: 230417 <230417@epvc.pt> Date: Tue, 3 Mar 2026 14:46:57 +0000 Subject: [PATCH] =?UTF-8?q?corre=C3=A7=C3=A3o=20bug=20barberia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/context/AppContext.tsx | 11 +++++++++-- web/src/pages/Dashboard.tsx | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index a66d9e9..fbf1a25 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -13,6 +13,7 @@ type State = { user?: User; users: User[]; shops: BarberShop[]; + shopsReady: boolean; appointments: Appointment[]; orders: Order[]; cart: CartItem[]; @@ -43,12 +44,14 @@ type AppContextValue = State & { deleteBarber: (shopId: string, barberId: string) => void; updateShopDetails: (shopId: string, payload: Partial) => Promise; refreshShops: () => Promise; + shopsReady: boolean; }; const initialState: State = { user: undefined, users: mockUsers, shops: [], + shopsReady: false, appointments: [], orders: [], cart: [], @@ -85,7 +88,10 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const refreshShops = async () => { try { const { data: shopsData, error: shopsError } = await supabase.from('shops').select('*'); - if (shopsError || !shopsData) return; + if (shopsError || !shopsData) { + setState((s) => ({ ...s, shopsReady: true })); + return; + } const { data: servicesData } = await supabase.from('services').select('*'); @@ -108,9 +114,10 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { barbers: [], })); - setState((s) => ({ ...s, shops: fetchedShops })); + setState((s) => ({ ...s, shops: fetchedShops, shopsReady: true })); } catch (err) { console.error('refreshShops error:', err); + setState((s) => ({ ...s, shopsReady: true })); } }; diff --git a/web/src/pages/Dashboard.tsx b/web/src/pages/Dashboard.tsx index f9f090a..63a86e8 100644 --- a/web/src/pages/Dashboard.tsx +++ b/web/src/pages/Dashboard.tsx @@ -76,6 +76,7 @@ export default function Dashboard() { user, users, shops, + shopsReady, appointments, orders, updateAppointmentStatus, @@ -115,6 +116,23 @@ export default function Dashboard() { const [barberSpecs, setBarberSpecs] = useState(''); if (!user || user.role !== 'barbearia') return
Área exclusiva para barbearias.
; + + // Aguarda o refreshShops terminar antes de declarar que a loja não existe + // (Evita o erro imediatamente após o registo quando os dados ainda estão a carregar) + if (!shop && !shopsReady) { + return ( +
+
+
+

A carregar painel...

+ +
+
+ ); + } if (!shop) return
Barbearia não encontrada.
; const periodMatch = periods[period];