Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { useState, useEffect, useCallback, useRef } from 'react';
import { profileService } from '@/services/profileService';
import { searchLocation } from '@/services/eventService';
import type { FavoriteLocation } from '@/models/profile';
import type { LocationSuggestion } from '@/models/event';
import { ApiError } from '@/services/api';
import { formatEventLocation } from '@/utils/eventLocation';
import i18n from '@/i18n';
const MAX_LOCATIONS = 3;
const SEARCH_DEBOUNCE_MS = 300;
export function useFavoriteLocationsViewModel(token: string | null) {
const [locations, setLocations] = useState<FavoriteLocation[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
// Add modal state
const [showAddModal, setShowAddModal] = useState(false);
const [addName, setAddName] = useState('');
const [addQuery, setAddQuery] = useState('');
const [addSuggestions, setAddSuggestions] = useState<LocationSuggestion[]>([]);
const [selectedSuggestion, setSelectedSuggestion] = useState<LocationSuggestion | null>(null);
const [isSearching, setIsSearching] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [addError, setAddError] = useState<string | null>(null);
const [removingId, setRemovingId] = useState<string | null>(null);
const searchTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const canAddMore = locations.length < MAX_LOCATIONS;
const fetchLocations = useCallback(async () => {
if (!token) return;
setIsLoading(true);
setError(null);
try {
const data = await profileService.getFavoriteLocations(token);
setLocations(data);
} catch (err) {
setError(err instanceof ApiError ? err.message : i18n.t('errors.unexpected'));
} finally {
setIsLoading(false);
}
}, [token]);
useEffect(() => {
fetchLocations();
}, [fetchLocations]);
// Debounced location search
const handleSearchChange = useCallback((query: string) => {
setAddQuery(query);
setSelectedSuggestion(null);
if (searchTimerRef.current) clearTimeout(searchTimerRef.current);
if (query.trim().length < 2) {
setAddSuggestions([]);
return;
}
searchTimerRef.current = setTimeout(async () => {
setIsSearching(true);
try {
const results = await searchLocation(query);
setAddSuggestions(results);
} catch {
setAddSuggestions([]);
} finally {
setIsSearching(false);
}
}, SEARCH_DEBOUNCE_MS);
}, []);
const selectSuggestion = useCallback((suggestion: LocationSuggestion) => {
setSelectedSuggestion(suggestion);
setAddQuery(formatEventLocation(suggestion.display_name));
setAddSuggestions([]);
}, []);
const openAddModal = useCallback(() => {
setAddName('');
setAddQuery('');
setAddSuggestions([]);
setSelectedSuggestion(null);
setAddError(null);
setShowAddModal(true);
}, []);
const closeAddModal = useCallback(() => {
setShowAddModal(false);
if (searchTimerRef.current) clearTimeout(searchTimerRef.current);
}, []);
const handleAdd = useCallback(async () => {
if (!token || !selectedSuggestion || !addName.trim()) return;
setIsSubmitting(true);
setAddError(null);
try {
await profileService.createFavoriteLocation({
name: addName.trim(),
address: formatEventLocation(selectedSuggestion.display_name),
lat: parseFloat(selectedSuggestion.lat),
lon: parseFloat(selectedSuggestion.lon),
}, token);
await fetchLocations();
closeAddModal();
} catch (err) {
if (err instanceof ApiError) {
if (err.code === 'favorite_location_limit_exceeded') {
setAddError(i18n.t('favorites.limit_reached', { count: MAX_LOCATIONS }));
} else {
setAddError(err.message);
}
} else {
setAddError(i18n.t('errors.unexpected'));
}
} finally {
setIsSubmitting(false);
}
}, [token, selectedSuggestion, addName, fetchLocations, closeAddModal]);
const handleRemove = useCallback(async (id: string) => {
if (!token) return;
setRemovingId(id);
try {
await profileService.deleteFavoriteLocation(id, token);
setLocations((prev) => prev.filter((l) => l.id !== id));
} catch (err) {
setError(err instanceof ApiError ? err.message : i18n.t('errors.unexpected'));
} finally {
setRemovingId(null);
}
}, [token]);
return {
locations,
isLoading,
error,
canAddMore,
maxLocations: MAX_LOCATIONS,
showAddModal,
addName,
setAddName,
addQuery,
handleSearchChange,
addSuggestions,
selectedSuggestion,
selectSuggestion,
isSearching,
isSubmitting,
addError,
removingId,
openAddModal,
closeAddModal,
handleAdd,
handleRemove,
retry: fetchLocations,
};
}
|