ability to set a preset

This commit is contained in:
Niels Lyngsø
2023-06-02 22:08:56 +02:00
parent 60226c7b38
commit 72bc321d92

View File

@@ -12,6 +12,8 @@ export class UmbDataTypeWorkspaceContext
#data = new UmbObjectState<DataTypeResponseModel | undefined>(undefined);
data = this.#data.asObservable();
#preset?: Partial<DataTypeResponseModel>;
name = this.#data.getObservablePart((data) => data?.name);
id = this.#data.getObservablePart((data) => data?.id);
@@ -19,6 +21,10 @@ export class UmbDataTypeWorkspaceContext
super(host, new UmbDataTypeRepository(host));
}
public setPreset(preset: Partial<DataTypeResponseModel>) {
this.#preset = preset;
}
async load(id: string) {
const { data } = await this.repository.requestById(id);
if (data) {
@@ -28,7 +34,10 @@ export class UmbDataTypeWorkspaceContext
}
async createScaffold(parentId: string | null) {
const { data } = await this.repository.createScaffold(parentId);
let { data } = await this.repository.createScaffold(parentId);
if (this.#preset) {
data = { ...data, ...this.#preset };
}
this.setIsNew(true);
// TODO: This is a hack to get around the fact that the data is not typed correctly.
// Create and response models are different. We need to look into this.
@@ -80,8 +89,8 @@ export class UmbDataTypeWorkspaceContext
} else {
await this.repository.save(this.#data.value.id, this.#data.value);
}
// If it went well, then its not new anymore?.
this.setIsNew(false);
this.saveComplete(this.#data.value);
}
async delete(id: string) {