Skip to content

Commit de010de

Browse files
committed
fix: outdated dependencies + invalid coordinates + remove logs
1 parent b09d6cf commit de010de

File tree

6 files changed

+375
-432
lines changed

6 files changed

+375
-432
lines changed

app/components/AutocompletePlacesInput.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Loader as GoogleMapsApiLoader } from '@googlemaps/js-api-loader';
44

55
import { type InputProps, default as Input } from './Input';
66
import type { Search } from '~/schemas/search';
7+
import { useField } from 'remix-validated-form';
78

89
interface AutocompletePlacesInput extends InputProps {
910
autocompleteApiKey: string;
@@ -23,6 +24,7 @@ export default function AutocompletePlacesInput({
2324
const [isAutocompleteInitialized, setIsAutocompleteInitialized] = useState(false);
2425

2526
const [coordinates, setCoordinates] = useState<Search['coordinates']>();
27+
const { error, getInputProps } = useField('coordinates');
2628

2729
const initializeAutocomplete = useCallback(async () => {
2830
if (!AddressInputRef.current || isAutocompleteInitialized) {
@@ -46,15 +48,10 @@ export default function AutocompletePlacesInput({
4648
autocomplete.addListener('place_changed', () => {
4749
const place = autocomplete.getPlace();
4850

49-
const Address = place?.address_components?.find(component =>
50-
component.types.includes('postal_code')
51-
)?.long_name;
52-
53-
const parsedZipCode = Address?.match(/\d+/)?.[0];
5451
const latitude = place?.geometry?.location?.lat();
5552
const longitude = place?.geometry?.location?.lng();
5653

57-
if (parsedZipCode && latitude && longitude) {
54+
if (latitude && longitude) {
5855
setCoordinates({ latitude, longitude });
5956
google.maps?.event.clearInstanceListeners(autocomplete);
6057
}
@@ -68,8 +65,9 @@ export default function AutocompletePlacesInput({
6865
});
6966

7067
return (
71-
<div>
68+
<div className="flex flex-col">
7269
<Input
70+
className="!mb-0"
7371
forwardedRef={AddressInputRef}
7472
name={name}
7573
label={label}
@@ -79,7 +77,12 @@ export default function AutocompletePlacesInput({
7977
defaultValue={defaultValue}
8078
disabled={disabled}
8179
/>
82-
<input type="hidden" name="coordinates" value={JSON.stringify(coordinates)} />
80+
<input
81+
type="hidden"
82+
name="coordinates"
83+
{...getInputProps({ value: JSON.stringify(coordinates) })}
84+
/>
85+
{error && <span className="mt-1 text-xs text-red-500">{error}</span>}
8386
</div>
8487
);
8588
}

app/components/Logs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function Logs({ logs }: LogsProps) {
1717

1818
return (
1919
<div className="mb-4 flex flex-col justify-center gap-4">
20-
<span className="divider" />
20+
<hr />
2121
<div className="flex place-content-start items-center gap-2">
2222
<h3 className="cursor-pointer text-lg">Search logs</h3>
2323
{isExpanded && (

app/routes/_main.search.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const loader = async ({ request, context }: LoaderFunctionArgs) => {
4747
const jobId = url.searchParams.get('id');
4848

4949
const searchJob = jobId ? await getKVRecord<SearchJobSerialized>(context, jobId) : null;
50-
const autocompleteApiKey = context.cloudflare.env.PLACES_API_KEY;
50+
const autocompleteApiKey = context.cloudflare.env.AUTOCOMPLETE_API_KEY;
5151

5252
return { jobId, searchJob, autocompleteApiKey };
5353
};
@@ -65,7 +65,6 @@ export default function SearchPage() {
6565
const eventSource = new EventSource(`/search/job/${jobId}`);
6666

6767
eventSource.onmessage = event => {
68-
console.log('event', event);
6968
const [time, percentage, message] = event.data.split(',');
7069

7170
if (message === DONE_JOB_MESSAGE) {
@@ -87,15 +86,13 @@ export default function SearchPage() {
8786

8887
useEffect(() => {
8988
startSearchJob();
90-
});
91-
92-
console.log('search', searchJob, 'jobState', jobState);
89+
}, [startSearchJob]);
9390

9491
return (
9592
<div className="flex flex-col gap-6">
9693
<ValidatedForm validator={validator} method="post" className="flex flex-col gap-6">
9794
<div className="rounded-lg border bg-base-200/30 p-4 md:p-6">
98-
<div className="flex gap-4 [&>div]:flex-1">
95+
<div className="flex flex-wrap gap-4 [&>div]:min-w-52 [&>div]:flex-1">
9996
<Input
10097
name="favoriteMealName"
10198
label="Meal/Dish/Food"

app/schemas/search.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ export const SearchSchema = z.object({
77
.max(30, { message: 'Too long' }),
88
address: z.string().min(1, { message: 'Required field' }),
99
coordinates: z.preprocess(
10-
val => JSON.parse(val && typeof val === 'string' ? val : '{}'),
11-
z.object({
12-
latitude: z.number().min(-90).max(90),
13-
longitude: z.number().min(-180).max(180),
14-
})
10+
val => JSON.parse(val && typeof val === 'string' ? val : 'null'),
11+
z.object(
12+
{
13+
latitude: z.number().min(-90).max(90),
14+
longitude: z.number().min(-180).max(180),
15+
},
16+
{
17+
required_error: 'Required field',
18+
invalid_type_error: 'Invalid address, select another location',
19+
}
20+
)
1521
),
1622
});
1723

0 commit comments

Comments
 (0)