correção bug barberia
This commit is contained in:
@@ -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<BarberShop>) => Promise<void>;
|
||||
refreshShops: () => Promise<void>;
|
||||
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 }));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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 <div>Área exclusiva para barbearias.</div>;
|
||||
|
||||
// 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 (
|
||||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '60vh' }}>
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
<div style={{
|
||||
width: 40, height: 40, border: '4px solid #e2e8f0', borderTopColor: '#6366f1',
|
||||
borderRadius: '50%', animation: 'spin 0.8s linear infinite', margin: '0 auto 12px'
|
||||
}} />
|
||||
<p style={{ color: '#64748b', fontSize: 14 }}>A carregar painel...</p>
|
||||
<style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (!shop) return <div>Barbearia não encontrada.</div>;
|
||||
|
||||
const periodMatch = periods[period];
|
||||
|
||||
Reference in New Issue
Block a user