@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
169 lines (148 loc) • 5.06 kB
JavaScript
import Model, { attr } from '@ember-data/model';
import { tracked } from '@glimmer/tracking';
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 config from 'ember-get-config';
export default class PlaceModel extends Model {
/** @ids */
public_id;
company_uuid;
vendor_uuid;
/** @attributes */
name;
phone;
type;
avatar_url;
avatar_value;
address;
address_html;
street1;
street2;
city;
province;
postal_code;
neighborhood;
district;
building;
security_access_code;
country;
country_name;
vendor_name;
_import_id;
eta;
location;
meta;
/** @dates */
deleted_at;
created_at;
updated_at;
/** @tracked */
selected = false;
/** @methods */
toJSON() {
return {
uuid: this.id,
vendor_uuid: this.vendor_uuid,
name: this.name,
phone: this.phone,
type: this.type,
address: this.address,
address_html: this.address_html,
street1: this.street1,
street2: this.street2,
city: this.city,
province: this.province,
postal_code: this.postal_code,
neighborhood: this.neighborhood,
district: this.district,
building: this.building,
security_access_code: this.security_access_code,
country: this.country,
country_name: this.country_name,
vendor_name: this.vendor_name,
location: this.location,
meta: this.meta,
created_at: this.created_at,
updated_at: this.updated_at,
};
}
/** @computed */
get longitude() {
return get(this.location, 'coordinates.0');
}
get latitude() {
return get(this.location, 'coordinates.1');
}
get coordinates() {
// eslint-disable-next-line ember/no-get
return [get(this, 'latitude'), get(this, 'longitude')];
}
get positionString() {
// eslint-disable-next-line ember/no-get
return `${get(this, 'latitude')} ${get(this, 'longitude')}`;
}
get latlng() {
return {
// eslint-disable-next-line ember/no-get
lat: get(this, 'latitude'),
// eslint-disable-next-line ember/no-get
lng: get(this, 'longitude'),
};
}
get latitudelongitude() {
return {
// eslint-disable-next-line ember/no-get
latitude: get(this, 'latitude'),
// eslint-disable-next-line ember/no-get
longitude: get(this, 'longitude'),
};
}
get hasValidCoordinates() {
if (this.longitude === 0 || this.latitude === 0) {
return false;
}
return isValidCoordinates(this.coordinates);
}
hasInvalidCoordinates;
get updatedAgo() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDate(this.updated_at, 'PPP p');
}
get updatedAtShort() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDate(this.updated_at, 'dd, MMM');
}
get createdAgo() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDistanceToNow(this.created_at);
}
get createdAt() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'PPP p');
}
get createdAtShort() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'dd, MMM');
}
}