UNPKG

@fleetbase/fleetops-data

Version:

Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.

96 lines (80 loc) 2.87 kB
import Model, { attr, belongsTo, hasMany } from '@ember-data/model'; import { computed } from '@ember/object'; import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns'; export default class ZoneModel extends Model { /** @ids */ @attr('string') public_id; @attr('string') service_area_uuid; /** @relationships */ @belongsTo('service-area') service_area; @hasMany('custom-field-value', { async: false }) custom_field_values; /** @attributes */ @attr('string') name; @attr('string') description; @attr('string') color; @attr('string') stroke_color; @attr('string') status; @attr('polygon') border; @attr('point') center; /** @dates */ @attr('date') deleted_at; @attr('date') created_at; @attr('date') updated_at; /** @computed */ @computed('border.coordinates.[]') get coordinates() { return this.border?.coordinates[0] ?? []; } @computed('coordinates') get leafletCoordinates() { return this.coordinates.map(([longitude, latitude]) => [latitude, longitude]); } @computed('leafletCoordinates') get firstCoordinatePair() { const [latitude, longitude] = this.leafletCoordinates[0] ?? [0, 0]; return [latitude, longitude]; } /* eslint-disable no-unused-vars */ @computed('firstCoordinatePair.0') get firstCoordinatePairLatitude() { const [latitude, longitude] = this.firstCoordinatePair; return latitude; } /* eslint-disable no-unused-vars */ @computed('firstCoordinatePair.1') get firstCoordinatePairLongitude() { const [latitude, longitude] = this.firstCoordinatePair; return longitude; } @computed('updated_at') get updatedAgo() { if (!isValidDate(this.updated_at)) { return null; } return formatDistanceToNow(this.updated_at); } @computed('updated_at') get updatedAt() { if (!isValidDate(this.updated_at)) { return null; } return formatDate(this.updated_at, 'yyyy-MM-dd HH:mm'); } @computed('updated_at') get updatedAtShort() { if (!isValidDate(this.updated_at)) { return null; } return formatDate(this.updated_at, 'dd, MMM'); } @computed('created_at') get createdAgo() { if (!isValidDate(this.created_at)) { return null; } return formatDistanceToNow(this.created_at); } @computed('created_at') get createdAt() { if (!isValidDate(this.created_at)) { return null; } return formatDate(this.created_at, 'yyyy-MM-dd HH:mm'); } @computed('created_at') get createdAtShort() { if (!isValidDate(this.created_at)) { return null; } return formatDate(this.created_at, 'dd, MMM'); } }