Geopicker field saves correct values
This commit is contained in:
parent
8473d0d4d6
commit
26eb75646b
@ -1,4 +1,5 @@
|
|||||||
import { CollectionConfig } from 'payload/types';
|
import { CollectionConfig } from 'payload/types';
|
||||||
|
import { geoPickerField } from "../customFields/geoPicker/field";
|
||||||
|
|
||||||
const Makers: CollectionConfig = {
|
const Makers: CollectionConfig = {
|
||||||
slug: 'makers',
|
slug: 'makers',
|
||||||
@ -14,11 +15,7 @@ const Makers: CollectionConfig = {
|
|||||||
type: 'text', // required
|
type: 'text', // required
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
geoPickerField,
|
||||||
name: 'location',
|
|
||||||
type: 'point',
|
|
||||||
label: 'Location',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'products', // required
|
name: 'products', // required
|
||||||
type: 'relationship', // required
|
type: 'relationship', // required
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
import { Field } from 'payload/types';
|
|
||||||
import { GeoPicker} from './GeoPicker';
|
|
||||||
|
|
||||||
export const GeoPickerField: Field = {
|
|
||||||
name: 'GeoPicker',
|
|
||||||
type: 'point',
|
|
||||||
admin: {
|
|
||||||
components: {
|
|
||||||
Field: GeoPicker,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +1,26 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useField } from 'payload/components/forms';
|
import { useField, Label, TextInput } from 'payload/components/forms';
|
||||||
|
|
||||||
export const GeoPicker: React.FC<{ path: string }> = ({ path }) => {
|
export const GeoPicker: React.FC<{ path: string }> = ({ path }) => {
|
||||||
const { value, setValue } = useField<string>({ path });
|
const { value, setValue } = useField<string>({ path });
|
||||||
const [latitude, setLatitude] = React.useState(null);
|
const [latitude, setLatitude] = React.useState("");
|
||||||
const [longitude, setLongitude] = React.useState(null);
|
const [longitude, setLongitude] = React.useState("");
|
||||||
|
|
||||||
const handleKeyPress = async (e) => {
|
const handleCityEnter = async (e) => {
|
||||||
|
console.log("Handle key")
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
|
console.log("Enter: " + e.target.value)
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`https://nominatim.openstreetmap.org/search?format=json&q=${value}`
|
`https://nominatim.openstreetmap.org/search?format=json&q=${e.target.value}`
|
||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data && data.length > 0) {
|
if (data && data.length > 0) {
|
||||||
setLatitude(parseFloat(data[0].lat));
|
const { lat, lon } = data[0];
|
||||||
setLongitude(parseFloat(data[0].lon));
|
setLatitude(lat);
|
||||||
|
setLongitude(lon);
|
||||||
|
setValue([lon, lat]);
|
||||||
|
console.log(data)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching geolocation:', error);
|
console.error('Error fetching geolocation:', error);
|
||||||
@ -23,6 +28,14 @@ export const GeoPicker: React.FC<{ path: string }> = ({ path }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleLatitudeChange = (e) => {
|
||||||
|
setLatitude(e.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLongitudeChange = (e) => {
|
||||||
|
setLongitude(e.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className='field-type text'>
|
<div className='field-type text'>
|
||||||
@ -32,9 +45,7 @@ export const GeoPicker: React.FC<{ path: string }> = ({ path }) => {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
className='field-name'
|
className='field-name'
|
||||||
value={value}
|
onKeyDown={handleCityEnter}
|
||||||
onChange={(e) => setValue(e.target.value)}
|
|
||||||
onKeyDown={handleKeyPress}
|
|
||||||
placeholder="Enter city to get coordinates"
|
placeholder="Enter city to get coordinates"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -42,11 +53,11 @@ export const GeoPicker: React.FC<{ path: string }> = ({ path }) => {
|
|||||||
<ul className='point__wrap'>
|
<ul className='point__wrap'>
|
||||||
<li>
|
<li>
|
||||||
<label className='field-label'>Location - Longitude</label>
|
<label className='field-label'>Location - Longitude</label>
|
||||||
<input id="field-longitude-location" type="number" name="location.longitude" value={longitude}></input>
|
<input id="field-longitude-location" type="number" name="location.longitude" value={longitude} onChange={handleLongitudeChange}></input>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<label className='field-label'>Location - Latitude</label>
|
<label className='field-label'>Location - Latitude</label>
|
||||||
<input id="field-latitude-latitude" type="number" name="location.latitude" value={latitude}></input>
|
<input id="field-latitude-latitude" type="number" name="location.latitude" value={latitude} onChange={handleLatitudeChange}></input>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
12
src/customFields/geoPicker/field.ts
Normal file
12
src/customFields/geoPicker/field.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { PointField } from 'payload/types';
|
||||||
|
import { GeoPicker } from './GeoPicker';
|
||||||
|
|
||||||
|
export const geoPickerField: PointField = {
|
||||||
|
name: 'Location',
|
||||||
|
type: 'point',
|
||||||
|
admin: {
|
||||||
|
components: {
|
||||||
|
Field: GeoPicker,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user