app
This commit is contained in:
@@ -115,15 +115,18 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
}));
|
||||
|
||||
setState((s) => {
|
||||
// Preservar lojas criadas localmente que ainda não apareceram no fetch
|
||||
const existingLocalShops = s.shops.filter(
|
||||
(localShop) => !fetchedShops.some((fs) => fs.id === localShop.id)
|
||||
);
|
||||
return {
|
||||
...s,
|
||||
shops: [...fetchedShops, ...existingLocalShops],
|
||||
shopsReady: true
|
||||
};
|
||||
// Substituir completamente com dados do Supabase.
|
||||
// Para a loja do utilizador atual (se for barbearia), preservar dados locais mais recentes
|
||||
// para não sobrepor com dados antigos da BD durante a mesma sessão.
|
||||
const mergedShops = fetchedShops.map((fetchedShop) => {
|
||||
const localVersion = s.shops.find(ls => ls.id === fetchedShop.id);
|
||||
// Se o utilizador atual é o dono desta loja, preferir a versão local (mais atualizada)
|
||||
if (localVersion && s.user?.shopId === fetchedShop.id) {
|
||||
return { ...fetchedShop, ...localVersion };
|
||||
}
|
||||
return fetchedShop;
|
||||
});
|
||||
return { ...s, shops: mergedShops, shopsReady: true };
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('refreshShops error:', err);
|
||||
@@ -253,8 +256,10 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
setState((s) => ({ ...s, user: undefined }));
|
||||
supabase.auth.signOut().catch(console.error);
|
||||
setState((s) => ({ ...s, user: undefined, shops: [], shopsReady: false }));
|
||||
supabase.auth.signOut()
|
||||
.then(() => refreshShops())
|
||||
.catch(console.error);
|
||||
};
|
||||
|
||||
const toggleFavorite = (shopId: string) => {
|
||||
|
||||
Reference in New Issue
Block a user