diff --git a/addon/models/place.js b/addon/models/place.js index 6b82378..cb0de50 100644 --- a/addon/models/place.js +++ b/addon/models/place.js @@ -4,7 +4,6 @@ import { computed, get } from '@ember/object'; import { not } from '@ember/object/computed'; import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns'; import isValidCoordinates from '@fleetbase/ember-core/utils/is-valid-coordinates'; -import extractCoordinates from '@fleetbase/ember-core/utils/extract-coordinates'; export default class PlaceModel extends Model { /** @ids */ @@ -85,19 +84,6 @@ export default class PlaceModel extends Model { return [get(this, 'latitude'), get(this, 'longitude')]; } - @computed('coordinates') get extractedCoordinates() { - return extractCoordinates(this.coordinates); - } - - @computed('extractedCoordinates') get extractedLatLng() { - const [latitude, longitude] = this.extractedCoordinates; - - return { - lat: latitude, - lng: longitude, - }; - } - @computed('latitude', 'longitude') get latlng() { return { // eslint-disable-next-line ember/no-get diff --git a/addon/models/service-area.js b/addon/models/service-area.js index 7334335..db1098e 100644 --- a/addon/models/service-area.js +++ b/addon/models/service-area.js @@ -1,7 +1,6 @@ import Model, { attr, hasMany } from '@ember-data/model'; import { computed } from '@ember/object'; import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns'; -import extractCoordinates from '@fleetbase/ember-core/utils/extract-coordinates'; import getWithDefault from '@fleetbase/ember-core/utils/get-with-default'; import first from '@fleetbase/ember-core/utils/first'; @@ -22,6 +21,7 @@ export default class ServiceAreaModel extends Model { @attr('string') stroke_color; @attr('string') status; @attr('multi-polygon') border; + @attr('point') center; /** @dates */ @attr('date') deleted_at; @@ -34,26 +34,17 @@ export default class ServiceAreaModel extends Model { const coordinates = getWithDefault(polygon, 'coordinates', []); const bounds = first(coordinates); - return bounds.map((coord) => extractCoordinates(coord)); + return bounds.map((coord) => { + let [longitude, latitude] = coord; + + return [latitude, longitude]; + }); } @computed('border.coordinates.[]') get boundaries() { return getWithDefault(this.border, 'coordinates', []); } - @computed('bounds') get firstCoordinatePair() { - return first(this.bounds) ?? [0, 0]; - } - - @computed('bounds') get centerCoordinates() { - const x = this.bounds.map((xy) => xy[0]); - const y = this.bounds.map((xy) => xy[1]); - const cx = (Math.min(...x) + Math.max(...x)) / 2; - const cy = (Math.min(...y) + Math.max(...y)) / 2; - - return [cx, cy]; - } - @computed('updated_at') get updatedAgo() { if (!isValidDate(this.updated_at)) { return null; diff --git a/addon/models/zone.js b/addon/models/zone.js index aa69fbb..1577424 100644 --- a/addon/models/zone.js +++ b/addon/models/zone.js @@ -20,7 +20,7 @@ export default class ZoneModel extends Model { @attr('string') stroke_color; @attr('string') status; @attr('polygon') border; - @attr('array') coordinates; + @attr('point') center; /** @dates */ @attr('date') deleted_at; @@ -28,6 +28,25 @@ export default class ZoneModel extends Model { @attr('date') updated_at; /** @computed */ + @computed('border.coordinates', 'isNew') get locations() { + let coordinates = getWithDefault(this.border, 'coordinates', []); + let isCoordinatesWrapped = isArray(coordinates) && isArray(coordinates[0]) && coordinates[0].length > 2; + // hotfix patch when coordinates are wrapped in array + if (isCoordinatesWrapped) { + coordinates = first(coordinates); + } + + if (this.isNew) { + return coordinates; + } + + return coordinates.map((coord) => { + let [longitude, latitude] = coord; + + return [latitude, longitude]; + }); + } + @computed('updated_at') get updatedAgo() { if (!isValidDate(this.updated_at)) { return null; @@ -69,32 +88,4 @@ export default class ZoneModel extends Model { } return formatDate(this.created_at, 'PP'); } - - @computed('border.coordinates', 'isNew') get locations() { - let coordinates = getWithDefault(this.border, 'coordinates', []); - - // hotfix patch when coordinates are wrapped in array - if (isArray(coordinates) && isArray(coordinates[0]) && coordinates[0].length > 2) { - coordinates = first(coordinates); - } - - if (this.isNew) { - return coordinates; - } - - return coordinates.map((coord) => coord.reverse()); - } - - @computed('bounds') get firstCoordinatePair() { - return first(this.bounds) ?? [0, 0]; - } - - @computed('locations') get centerCoordinates() { - const x = this.locations.map((xy) => xy[0]); - const y = this.locations.map((xy) => xy[1]); - const cx = (Math.min(...x) + Math.max(...x)) / 2; - const cy = (Math.min(...y) + Math.max(...y)) / 2; - - return [cx, cy]; - } } diff --git a/package.json b/package.json index 964c18c..0f75821 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/fleetops-data", - "version": "0.1.6", + "version": "0.1.7", "description": "Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.", "keywords": [ "fleetbase-data",