mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
added support for geo-points
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
import { projectScope } from "@src/atoms/projectScope";
|
||||
import { FieldType } from "@src/constants/fields";
|
||||
import { firebaseDbAtom } from "@src/sources/ProjectSourceFirebase";
|
||||
import { doc, DocumentReference as Reference } from "firebase/firestore";
|
||||
import {
|
||||
doc,
|
||||
DocumentReference as Reference,
|
||||
GeoPoint,
|
||||
} from "firebase/firestore";
|
||||
import { useAtom } from "jotai";
|
||||
|
||||
const needsConverter = (type: FieldType) =>
|
||||
[FieldType.image, FieldType.reference, FieldType.file].includes(type);
|
||||
[
|
||||
FieldType.image,
|
||||
FieldType.reference,
|
||||
FieldType.file,
|
||||
FieldType.geoPoint,
|
||||
].includes(type);
|
||||
|
||||
export default function useConverter() {
|
||||
const [firebaseDb] = useAtom(firebaseDbAtom, projectScope);
|
||||
@@ -40,6 +49,41 @@ export default function useConverter() {
|
||||
return [];
|
||||
};
|
||||
|
||||
const geoPointConverter = (value: any) => {
|
||||
if (!value) return null;
|
||||
if (typeof value === "string") {
|
||||
console.log("value", value);
|
||||
let latitude, longitude;
|
||||
// covered cases:
|
||||
// [3.2, 32.3]
|
||||
// {latitude: 3.2, longitude: 32.3}
|
||||
// "3.2, 32.3"
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (Array.isArray(parsed)) {
|
||||
[latitude, longitude] = parsed;
|
||||
} else {
|
||||
latitude = parsed.latitude;
|
||||
longitude = parsed.longitude;
|
||||
}
|
||||
|
||||
if (latitude && longitude) {
|
||||
latitude = parseFloat(latitude);
|
||||
longitude = parseFloat(longitude);
|
||||
}
|
||||
} catch (e) {
|
||||
[latitude, longitude] = value
|
||||
.split(",")
|
||||
.map((val) => parseFloat(val.trim()));
|
||||
}
|
||||
|
||||
if (latitude && longitude) {
|
||||
return new GeoPoint(latitude, longitude);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const getConverter = (type: FieldType) => {
|
||||
switch (type) {
|
||||
case FieldType.image:
|
||||
@@ -47,6 +91,8 @@ export default function useConverter() {
|
||||
return imageOrFileConverter;
|
||||
case FieldType.reference:
|
||||
return referenceConverter;
|
||||
case FieldType.geoPoint:
|
||||
return geoPointConverter;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ export const SELECTABLE_TYPES = [
|
||||
FieldType.json,
|
||||
FieldType.code,
|
||||
|
||||
FieldType.geoPoint,
|
||||
|
||||
FieldType.color,
|
||||
FieldType.slider,
|
||||
|
||||
|
||||
@@ -25,17 +25,5 @@ export const config: IFieldConfig = {
|
||||
popoverProps: { PaperProps: { sx: { p: 1, pt: 0 } } },
|
||||
}),
|
||||
SideDrawerField,
|
||||
csvImportParser: (value: string) => {
|
||||
try {
|
||||
const { latitude, longitude } = JSON.parse(value);
|
||||
if (latitude && longitude) {
|
||||
return new GeoPoint(latitude, longitude);
|
||||
}
|
||||
throw new Error();
|
||||
} catch (e) {
|
||||
console.error("Invalid GeoPoint value");
|
||||
return null;
|
||||
}
|
||||
},
|
||||
};
|
||||
export default config;
|
||||
|
||||
Reference in New Issue
Block a user