refactor: standardize frontend
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+28
-48
@@ -9,15 +9,9 @@ import { EventObject } from "./event";
|
||||
import { TaskObject } from "./task";
|
||||
import { JournalObject } from "./journal";
|
||||
|
||||
type EntityPropertiesData = EventInterface | TaskInterface | JournalInterface;
|
||||
type EntityPropertiesObject = EventObject | TaskObject | JournalObject;
|
||||
type EntityInternal = Omit<EntityInterface<EntityPropertiesData>, 'properties'> & {
|
||||
properties: EntityPropertiesData | EntityPropertiesObject;
|
||||
};
|
||||
|
||||
export class EntityObject implements EntityInterface {
|
||||
|
||||
_data!: EntityInternal;
|
||||
_data!: EntityInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
@@ -28,99 +22,85 @@ export class EntityObject implements EntityInterface {
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new EventObject().toJson(),
|
||||
properties: new EventObject(),
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = data as EntityInternal;
|
||||
this._data = data
|
||||
if (data.properties) {
|
||||
this._data.properties = this.hydrateProperties(data.properties);
|
||||
} else {
|
||||
this._data.properties = new EventObject();
|
||||
const type = data.properties.type
|
||||
if (type === 'task') {
|
||||
this._data.properties = new TaskObject().fromJson(data.properties as TaskInterface);
|
||||
} else if (type === 'journal') {
|
||||
this._data.properties = new JournalObject().fromJson(data.properties as JournalInterface);
|
||||
} else {
|
||||
this._data.properties = new EventObject().fromJson(data.properties as EventInterface);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data };
|
||||
const json = { ...this._data }
|
||||
if (this._data.properties instanceof EventObject ||
|
||||
this._data.properties instanceof TaskObject ||
|
||||
this._data.properties instanceof JournalObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json as EntityInterface;
|
||||
return json as EntityInterface
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this.toJson())) as EntityInternal;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
private hydrateProperties(properties: EntityPropertiesData): EntityPropertiesObject {
|
||||
const type = properties.type;
|
||||
if (type === 'task') {
|
||||
return new TaskObject().fromJson(properties as TaskInterface);
|
||||
}
|
||||
|
||||
if (type === 'journal') {
|
||||
return new JournalObject().fromJson(properties as JournalInterface);
|
||||
}
|
||||
|
||||
return new EventObject().fromJson(properties as EventInterface);
|
||||
const cloned = new EntityObject()
|
||||
cloned._data = { ...this._data }
|
||||
return cloned
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
return this._data.provider
|
||||
}
|
||||
|
||||
get service(): string {
|
||||
return this._data.service;
|
||||
return this._data.service
|
||||
}
|
||||
|
||||
get collection(): string | number {
|
||||
return this._data.collection;
|
||||
return this._data.collection
|
||||
}
|
||||
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier;
|
||||
return this._data.identifier
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature;
|
||||
return this._data.signature
|
||||
}
|
||||
|
||||
get created(): string | null {
|
||||
return this._data.created;
|
||||
return this._data.created
|
||||
}
|
||||
|
||||
get modified(): string | null {
|
||||
return this._data.modified;
|
||||
return this._data.modified
|
||||
}
|
||||
|
||||
get properties(): EntityPropertiesObject {
|
||||
get properties(): EventObject | TaskObject | JournalObject {
|
||||
if (this._data.properties instanceof EventObject ||
|
||||
this._data.properties instanceof TaskObject ||
|
||||
this._data.properties instanceof JournalObject) {
|
||||
return this._data.properties;
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = this.hydrateProperties(this._data.properties as EntityPropertiesData);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
return this._data.properties
|
||||
}
|
||||
|
||||
const defaultProperties = new EventObject();
|
||||
this._data.properties = defaultProperties;
|
||||
return defaultProperties;
|
||||
return defaultProperties
|
||||
}
|
||||
|
||||
set properties(value: EntityPropertiesObject) {
|
||||
this._data.properties = value;
|
||||
set properties(value: EventObject | TaskObject | JournalObject) {
|
||||
this._data.properties = value
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user