@lipagas/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
73 lines (55 loc) • 1.89 kB
JavaScript
import Model, { attr, belongsTo } from '@ember-data/model';
import { computed } from '@ember/object';
import { format, formatDistanceToNow, parse, isValid } from 'date-fns';
export default class ProductHourModel extends Model {
/** @ids */
product_uuid;
/** @relationships */
product;
/** @attributes */
day_of_week;
start;
end;
/** @dates */
created_at;
updated_at;
/** @methods */
toJSON() {
return this.serialize();
}
/** @computed */
get startDateInstance() {
if (!this.start) {
return null;
}
const includesSeconds = this.start.split(':').length === 3;
const format = includesSeconds ? 'k:mm:ss' : 'k:mm';
return parse(this.start, format, new Date());
}
get endDateInstance() {
if (!this.end) {
return null;
}
const includesSeconds = this.end.split(':').length === 3;
const format = includesSeconds ? 'k:mm:ss' : 'k:mm';
return parse(this.end, format, new Date());
}
get humanReadableHours() {
if (!isValid(this.startDateInstance) || !isValid(this.endDateInstance)) {
return `${this.start} - ${this.end}`;
}
return `${format(this.startDateInstance, 'p')} - ${format(this.endDateInstance, 'p')}`;
}
get updatedAgo() {
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
return format(this.updated_at, 'PPP');
}
get createdAgo() {
return formatDistanceToNow(this.created_at);
}
get createdAt() {
return format(this.created_at, 'PPP p');
}
}