From 6fad85056c0ae82eca7e6eb4f3cc6f36c8574b86 Mon Sep 17 00:00:00 2001 From: Pinx0 Date: Fri, 8 Nov 2024 19:30:36 +0100 Subject: [PATCH 01/15] refactor: swich to TS --- src/app/punto-recogida/page.tsx | 4 +- src/app/puntos-entrega/page.tsx | 4 +- ...utocomplete.js => AddressAutocomplete.tsx} | 70 ++++++++++++++----- 3 files changed, 57 insertions(+), 21 deletions(-) rename src/components/{AddressAutocomplete.js => AddressAutocomplete.tsx} (74%) diff --git a/src/app/punto-recogida/page.tsx b/src/app/punto-recogida/page.tsx index c0122674..c2b4942b 100644 --- a/src/app/punto-recogida/page.tsx +++ b/src/app/punto-recogida/page.tsx @@ -3,7 +3,7 @@ import { useState, useCallback, useEffect, FormEvent } from 'react'; import { supabase } from '@/lib/supabase/client'; import { MapPin, Phone, Package, House, Contact, Megaphone } from 'lucide-react'; -import AddressAutocomplete from '@/components/AddressAutocomplete'; +import AddressAutocomplete, { AddressDetails } from '@/components/AddressAutocomplete'; import { isValidPhone } from '@/helpers/utils'; import { PhoneInput } from '@/components/PhoneInput'; import { CollectionPointData, CollectionPointInsert } from '@/types/DataPoints'; @@ -220,7 +220,7 @@ export default function PuntosRecogida() {
{ + onSelect={(address: AddressDetails) => { setFormData((prev) => ({ ...prev, location: address.fullAddress, diff --git a/src/app/puntos-entrega/page.tsx b/src/app/puntos-entrega/page.tsx index 5ea18b2b..822e7b68 100644 --- a/src/app/puntos-entrega/page.tsx +++ b/src/app/puntos-entrega/page.tsx @@ -3,7 +3,7 @@ import { useState, useEffect, FormEvent } from 'react'; import { supabase } from '@/lib/supabase/client'; import { Truck, MapPin, Phone, Mail, Calendar, Package } from 'lucide-react'; -import AddressAutocomplete from '@/components/AddressAutocomplete'; +import AddressAutocomplete, { AddressDetails } from '@/components/AddressAutocomplete'; import { DeliveryPointData, DeliveryPointInsert, isCoordinates } from '@/types/DataPoints'; export default function PuntosEntrega() { @@ -226,7 +226,7 @@ export default function PuntosEntrega() {
{ + onSelect={(address: AddressDetails) => { setFormData((prev) => ({ ...prev, location: address.fullAddress, diff --git a/src/components/AddressAutocomplete.js b/src/components/AddressAutocomplete.tsx similarity index 74% rename from src/components/AddressAutocomplete.js rename to src/components/AddressAutocomplete.tsx index 2cabb82c..4bd52871 100644 --- a/src/components/AddressAutocomplete.js +++ b/src/components/AddressAutocomplete.tsx @@ -1,24 +1,60 @@ 'use client'; -import { useState, useEffect, useRef, useCallback } from 'react'; +import React, { useState, useEffect, useRef, useCallback } from 'react'; import { MapPin } from 'lucide-react'; +type Address = { + road: string; + house_number: string; + postcode: string; + city: string; + state: string; +}; + +type Suggestion = { + display_name: string; + full_address: string; + formatted_address: string; + formatted_locality: string; + lat: number; + lon: number; + address: Address; +}; + +type Coordinates = { + lat: number; + lon: number; +}; + +export type AddressDetails = { + fullAddress: string; + coordinates: Coordinates | null; + details: Address; +}; + +type AddressAutocompleteProps = { + onSelect: (details: AddressDetails) => void; + placeholder?: string; + initialValue?: string; + required?: boolean; +}; + export default function AddressAutocomplete({ onSelect, placeholder = 'Buscar dirección...', initialValue = '', required = false, -}) { - const [query, setQuery] = useState(initialValue); - const [suggestions, setSuggestions] = useState([]); - const [isLoading, setIsLoading] = useState(false); - const [showSuggestions, setShowSuggestions] = useState(false); - const wrapperRef = useRef(null); - const debounceRef = useRef(null); +}: AddressAutocompleteProps) { + const [query, setQuery] = useState(initialValue); + const [suggestions, setSuggestions] = useState([]); + const [isLoading, setIsLoading] = useState(false); + const [showSuggestions, setShowSuggestions] = useState(false); + const wrapperRef = useRef(null); + const debounceRef = useRef | null>(null); useEffect(() => { - function handleClickOutside(event) { - if (wrapperRef.current && !wrapperRef.current.contains(event.target)) { + function handleClickOutside(event: MouseEvent) { + if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) { setShowSuggestions(false); } } @@ -26,7 +62,7 @@ export default function AddressAutocomplete({ return () => document.removeEventListener('mousedown', handleClickOutside); }, []); - const searchAddress = useCallback(async (searchQuery) => { + const searchAddress = useCallback(async (searchQuery: string) => { if (!searchQuery || searchQuery.length < 3) { setSuggestions([]); return; @@ -39,12 +75,12 @@ export default function AddressAutocomplete({ ); const data = await response.json(); - const formattedSuggestions = data.map((item) => { - const addressParts = []; + const formattedSuggestions: Suggestion[] = data.map((item: any) => { + const addressParts: string[] = []; if (item.address?.road) addressParts.push(item.address.road); if (item.address?.house_number) addressParts.push(item.address.house_number); - const localityParts = []; + const localityParts: string[] = []; if (item.address?.postcode) localityParts.push(item.address.postcode); if (item.address?.city || item.address?.town || item.address?.village) { localityParts.push(item.address?.city || item.address?.town || item.address?.village); @@ -80,7 +116,7 @@ export default function AddressAutocomplete({ } }, []); - const handleInputChange = (e) => { + const handleInputChange = (e: React.ChangeEvent) => { const value = e.target.value; setQuery(value); @@ -119,7 +155,7 @@ export default function AddressAutocomplete({ }; }, []); - const handleSelect = (suggestion) => { + const handleSelect = (suggestion: Suggestion) => { setQuery(suggestion.full_address); setShowSuggestions(false); onSelect({ @@ -140,6 +176,7 @@ export default function AddressAutocomplete({ value={query || ''} onChange={handleInputChange} placeholder={placeholder} + required={required} className="w-full p-2 pr-10 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500" />
@@ -155,7 +192,6 @@ export default function AddressAutocomplete({
{suggestions.map((suggestion, index) => (