Skip to content

Commit 25d2900

Browse files
committed
split create and update
1 parent c924270 commit 25d2900

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

src/packages/core/workspace/entity-detail/entity-detail-workspace-base.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -130,39 +130,9 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
130130
}
131131

132132
if (this.getIsNew()) {
133-
const parent = this.#parent.getValue();
134-
if (!parent) throw new Error('Parent is not set');
135-
const { error, data } = await this._detailRepository!.create(currentData, parent.unique);
136-
if (error || !data) {
137-
throw error?.message ?? 'Repository did not return data after create.';
138-
}
139-
140-
this._data.setPersisted(data);
141-
this._data.setCurrent(data);
142-
143-
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
144-
const event = new UmbRequestReloadChildrenOfEntityEvent({
145-
entityType: parent.entityType,
146-
unique: parent.unique,
147-
});
148-
eventContext.dispatchEvent(event);
149-
this.setIsNew(false);
133+
this.#create(currentData);
150134
} else {
151-
const { error, data } = await this._detailRepository!.save(currentData);
152-
if (error || !data) {
153-
throw error?.message ?? 'Repository did not return data after create.';
154-
}
155-
156-
this._data.setPersisted(data);
157-
this._data.setCurrent(data);
158-
159-
const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
160-
const event = new UmbRequestReloadStructureForEntityEvent({
161-
unique: this.getUnique()!,
162-
entityType: this.getEntityType(),
163-
});
164-
165-
actionEventContext.dispatchEvent(event);
135+
this.#update(currentData);
166136
}
167137
}
168138

@@ -184,6 +154,44 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
184154
return !newUrl.includes(currentWorkspacePathIdentifier);
185155
}
186156

157+
async #create(currentData: DetailModelType) {
158+
const parent = this.#parent.getValue();
159+
if (!parent) throw new Error('Parent is not set');
160+
const { error, data } = await this._detailRepository!.create(currentData, parent.unique);
161+
if (error || !data) {
162+
throw error?.message ?? 'Repository did not return data after create.';
163+
}
164+
165+
this._data.setPersisted(data);
166+
this._data.setCurrent(data);
167+
168+
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
169+
const event = new UmbRequestReloadChildrenOfEntityEvent({
170+
entityType: parent.entityType,
171+
unique: parent.unique,
172+
});
173+
eventContext.dispatchEvent(event);
174+
this.setIsNew(false);
175+
}
176+
177+
async #update(currentData: DetailModelType) {
178+
const { error, data } = await this._detailRepository!.save(currentData);
179+
if (error || !data) {
180+
throw error?.message ?? 'Repository did not return data after create.';
181+
}
182+
183+
this._data.setPersisted(data);
184+
this._data.setCurrent(data);
185+
186+
const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
187+
const event = new UmbRequestReloadStructureForEntityEvent({
188+
unique: this.getUnique()!,
189+
entityType: this.getEntityType(),
190+
});
191+
192+
actionEventContext.dispatchEvent(event);
193+
}
194+
187195
#onWillNavigate = async (e: CustomEvent) => {
188196
const newUrl = e.detail.url;
189197

0 commit comments

Comments
 (0)