@lipagas/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
138 lines (119 loc) • 3.96 kB
JavaScript
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { getOwner } from '@ember/application';
import { tracked } from '@glimmer/tracking';
import { computed } from '@ember/object';
import { isArray } from '@ember/array';
import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns';
export default class StoreModel extends Model {
/** @ids */
created_by_uuid;
company_uuid;
logo_uuid;
backdrop_uuid;
/** @relationships */
notification_channels;
gateways;
category;
logo;
backdrop;
files;
/** @attributes */
name;
description;
website;
email;
phone;
facebook;
instagram;
twitter;
public_id;
key;
online;
currency;
timezone;
pod_method;
options;
translations;
tags;
alertable;
logo_url;
backdrop_url;
slug;
/** @dates */
created_at;
updated_at;
/** @tracked */
isLoadingFiles = false;
/** @computed */
get tagsList() {
if (isArray(this.tags)) {
this.tags.join(', ');
}
return '';
}
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, 'PP');
}
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, 'PP');
}
/** @methods */
toJSON() {
return {
name: this.name,
description: this.description,
currency: this.currency,
timezone: this.timezone,
options: this.options,
};
}
loadFiles() {
const owner = getOwner(this);
const store = owner.lookup(`service:store`);
this.isLoadingFiles = true;
return new Promise((resolve) => {
return store
.query('file', { subject_uuid: this.id, type: 'storefront_store_media' })
.then((files) => {
this.files = files.toArray();
this.isLoadingFiles = false;
resolve(files);
})
.catch((error) => {
this.isLoadingFiles = false;
resolve([]);
throw error;
});
});
}
}