import { Link } from 'expo-router';
import { useMemo, useState } from 'react';
import {
ActivityIndicator,
FlatList,
Pressable,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import { useOffices } from '@/features/offices/hooks/useOffices';
import { useFavoriteOffices } from '@/features/offices/hooks/useFavoriteOffices';
import { StatusMessage } from '@/shared/ui/StatusMessage';
export default function OfficesScreen() {
const { offices, loading, error, reload } = useOffices();
const {
favoriteOfficeIdsSet,
isLoadingFavorites,
favoritesError,
pendingOfficeId,
toggleFavorite,
} = useFavoriteOffices();
const [search, setSearch] = useState('');
const [minimumCapacity, setMinimumCapacity] = useState('');
const [showFavoritesOnly, setShowFavoritesOnly] = useState(false);
const filteredOffices = useMemo(() => {
const normalizedSearch = search.trim().toLowerCase();
const parsedCapacity = Number(minimumCapacity);
const hasCapacityFilter = minimumCapacity.trim().length > 0 && !Number.isNaN(parsedCapacity);
return offices.filter((office) => {
const matchesSearch =
normalizedSearch.length === 0 ||
office.name.toLowerCase().includes(normalizedSearch);
const matchesCapacity =
!hasCapacityFilter || office.capacity >= parsedCapacity;
const matchesFavorites =
!showFavoritesOnly || favoriteOfficeIdsSet.has(office.id);
return matchesSearch && matchesCapacity && matchesFavorites;
});
}, [favoriteOfficeIdsSet, minimumCapacity, offices, search, showFavoritesOnly]);
const clearFilters = () => {
setSearch('');
setMinimumCapacity('');
setShowFavoritesOnly(false);
};
if (loading || isLoadingFavorites) {
return (
);
}
if (error) {
return (
{error}
Reintentar
);
}
return (
Espacios
Buscar y filtrar
setMinimumCapacity(value.replace(/\D/g, ''))}
keyboardType="number-pad"
/>
setShowFavoritesOnly((current) => !current)}
>
{showFavoritesOnly ? 'Viendo favoritos' : 'Solo favoritos'}
Limpiar filtros
{favoritesError ? : null}
item.id}
contentContainerStyle={styles.list}
ListEmptyComponent={
{offices.length === 0
? 'No hay espacios disponibles'
: showFavoritesOnly
? 'No tienes oficinas favoritas que coincidan con los filtros'
: 'No hay espacios que coincidan con los filtros'}
}
renderItem={({ item }) => (
{item.name}
{
void toggleFavorite(item.id);
}}
disabled={pendingOfficeId === item.id}
>
{pendingOfficeId === item.id
? '...'
: favoriteOfficeIdsSet.has(item.id)
? '★'
: '☆'}
{item.location}
Capacidad: {item.capacity}
{item.description ? {item.description} : null}
)}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
center: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 24,
},
title: {
fontSize: 28,
fontWeight: '700',
marginBottom: 16,
},
filtersBox: {
borderWidth: 1,
borderColor: '#d8e0ea',
backgroundColor: '#f7f9fc',
borderRadius: 12,
padding: 12,
gap: 10,
marginBottom: 16,
},
filtersTitle: {
fontSize: 16,
fontWeight: '700',
color: '#22354a',
},
input: {
borderWidth: 1,
borderColor: '#ccd5df',
borderRadius: 10,
paddingHorizontal: 14,
paddingVertical: 12,
backgroundColor: '#fff',
},
filterActions: {
flexDirection: 'row',
gap: 10,
},
list: {
gap: 12,
},
card: {
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 12,
padding: 16,
gap: 10,
},
cardHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: 12,
},
cardTitle: {
fontSize: 18,
fontWeight: '600',
flex: 1,
},
cardBody: {
gap: 6,
},
error: {
color: 'red',
marginBottom: 12,
},
button: {
backgroundColor: '#1f6feb',
paddingHorizontal: 16,
paddingVertical: 12,
borderRadius: 10,
},
secondaryButton: {
backgroundColor: '#5d6b79',
flex: 1,
paddingHorizontal: 16,
paddingVertical: 12,
borderRadius: 10,
alignItems: 'center',
},
toggleButton: {
backgroundColor: '#1f6feb',
flex: 1,
paddingHorizontal: 16,
paddingVertical: 12,
borderRadius: 10,
alignItems: 'center',
},
toggleButtonActive: {
backgroundColor: '#174ea6',
},
favoriteButton: {
width: 40,
height: 40,
borderRadius: 20,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#eef2f6',
},
favoriteButtonActive: {
backgroundColor: '#ffe8a3',
},
favoriteButtonText: {
fontSize: 20,
color: '#8a6700',
},
buttonText: {
color: '#fff',
fontWeight: '600',
},
empty: {
color: '#666',
},
});