109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import React from 'react';
|
|
import { FlatList, Pressable, RefreshControl, StyleSheet, Text, View } from 'react-native';
|
|
import { Ionicons } from '@expo/vector-icons';
|
|
import { useRouter } from 'expo-router';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { api, Property } from '@/api';
|
|
import { useT } from '@/i18n';
|
|
import { AppBar } from '@/components/AppBar';
|
|
import { Button, EmptyState, IconButton, Loading } from '@/components/ui';
|
|
import { PropertyCard } from '@/components/domain';
|
|
import { colors, fontSize, radii, spacing } from '@/theme';
|
|
|
|
export default function PropertiesScreen() {
|
|
const router = useRouter();
|
|
const t = useT();
|
|
|
|
// One request: homes.php returns the per-property counters itself.
|
|
const {
|
|
data: properties,
|
|
isLoading,
|
|
isError,
|
|
error,
|
|
refetch,
|
|
isRefetching,
|
|
} = useQuery({ queryKey: ['properties'], queryFn: () => api.properties.list() });
|
|
|
|
return (
|
|
<View style={{ flex: 1, backgroundColor: colors.bgApp }}>
|
|
<AppBar
|
|
title={t('properties.title')}
|
|
subtitle={properties ? t('properties.count', { count: properties.length }) : undefined}
|
|
large
|
|
right={<IconButton name="search" onPress={() => router.push('/search')} />}
|
|
/>
|
|
|
|
{isLoading ? (
|
|
<Loading label={t('properties.loading')} />
|
|
) : isError ? (
|
|
<EmptyState
|
|
icon="cloud-offline-outline"
|
|
title={t('properties.loadError')}
|
|
subtitle={(error as Error)?.message}
|
|
action={<Button title={t('common.retry')} variant="ghost" onPress={() => refetch()} />}
|
|
/>
|
|
) : (
|
|
<FlatList
|
|
data={properties as Property[]}
|
|
keyExtractor={(p) => String(p.id)}
|
|
contentContainerStyle={styles.list}
|
|
refreshControl={
|
|
<RefreshControl
|
|
refreshing={isRefetching}
|
|
onRefresh={refetch}
|
|
tintColor={colors.teal600}
|
|
/>
|
|
}
|
|
ListHeaderComponent={
|
|
<View style={{ gap: spacing[3], marginBottom: spacing[4] }}>
|
|
<Pressable style={styles.searchBar} onPress={() => router.push('/search')}>
|
|
<Ionicons name="search" size={18} color={colors.slate400} />
|
|
<Text style={styles.searchPlaceholder}>{t('properties.searchPlaceholder')}</Text>
|
|
</Pressable>
|
|
<Button
|
|
title={t('properties.add')}
|
|
variant="ghost"
|
|
icon="add"
|
|
dashed
|
|
block
|
|
onPress={() => router.push('/property/add')}
|
|
/>
|
|
</View>
|
|
}
|
|
renderItem={({ item }) => (
|
|
<View style={{ marginBottom: spacing[3] }}>
|
|
<PropertyCard property={item} onPress={() => router.push(`/property/${item.id}`)} />
|
|
</View>
|
|
)}
|
|
ListEmptyComponent={
|
|
<EmptyState
|
|
icon="home-outline"
|
|
title={t('properties.emptyTitle')}
|
|
subtitle={t('properties.emptySubtitle')}
|
|
action={
|
|
<Button title={t('properties.add')} icon="add" onPress={() => router.push('/property/add')} />
|
|
}
|
|
/>
|
|
}
|
|
/>
|
|
)}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
list: { padding: spacing[4], paddingBottom: spacing[9] },
|
|
searchBar: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: 10,
|
|
backgroundColor: colors.surface,
|
|
borderWidth: 1,
|
|
borderColor: colors.border,
|
|
borderRadius: radii.md,
|
|
paddingHorizontal: spacing[4],
|
|
height: 48,
|
|
},
|
|
searchPlaceholder: { color: colors.textMuted2, fontSize: fontSize.sm, flex: 1 },
|
|
});
|