summaryrefslogtreecommitdiffstats
path: root/src/app/(protected)/(tabs)/offices/index.tsx
blob: 9040fbddf8182b6265b94ad492b7f2a18b149216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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 (
      <View style={styles.center}>
        <ActivityIndicator size="large" />
      </View>
    );
  }

  if (error) {
    return (
      <View style={styles.center}>
        <Text style={styles.error}>{error}</Text>
        <Pressable style={styles.button} onPress={reload}>
          <Text style={styles.buttonText}>Reintentar</Text>
        </Pressable>
      </View>
    );
  }

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Espacios</Text>

      <View style={styles.filtersBox}>
        <Text style={styles.filtersTitle}>Buscar y filtrar</Text>

        <TextInput
          style={styles.input}
          placeholder="Buscar por nombre"
          value={search}
          onChangeText={setSearch}
        />

        <TextInput
          style={styles.input}
          placeholder="Capacidad mínima"
          value={minimumCapacity}
          onChangeText={(value) => setMinimumCapacity(value.replace(/\D/g, ''))}
          keyboardType="number-pad"
        />

        <View style={styles.filterActions}>
          <Pressable
            style={[
              styles.toggleButton,
              showFavoritesOnly && styles.toggleButtonActive,
            ]}
            onPress={() => setShowFavoritesOnly((current) => !current)}
          >
            <Text style={styles.buttonText}>
              {showFavoritesOnly ? 'Viendo favoritos' : 'Solo favoritos'}
            </Text>
          </Pressable>

          <Pressable style={styles.secondaryButton} onPress={clearFilters}>
            <Text style={styles.buttonText}>Limpiar filtros</Text>
          </Pressable>
        </View>
      </View>

      {favoritesError ? <StatusMessage message={favoritesError} /> : null}

      <FlatList
        data={filteredOffices}
        keyExtractor={(item) => item.id}
        contentContainerStyle={styles.list}
        ListEmptyComponent={
          <Text style={styles.empty}>
            {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'}
          </Text>
        }
        renderItem={({ item }) => (
          <View style={styles.card}>
            <View style={styles.cardHeader}>
              <Text style={styles.cardTitle}>{item.name}</Text>
              <Pressable
                style={[
                  styles.favoriteButton,
                  favoriteOfficeIdsSet.has(item.id) && styles.favoriteButtonActive,
                ]}
                onPress={() => {
                  void toggleFavorite(item.id);
                }}
                disabled={pendingOfficeId === item.id}
              >
                <Text style={styles.favoriteButtonText}>
                  {pendingOfficeId === item.id
                    ? '...'
                    : favoriteOfficeIdsSet.has(item.id)
                      ? '★'
                      : '☆'}
                </Text>
              </Pressable>
            </View>

            <Link href={`/(protected)/offices/${item.id}`} asChild>
              <Pressable style={styles.cardBody}>
                <Text>{item.location}</Text>
                <Text>Capacidad: {item.capacity}</Text>
                {item.description ? <Text>{item.description}</Text> : null}
              </Pressable>
            </Link>
          </View>
        )}
      />
    </View>
  );
}

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',
  },
});