Skip to content

Commit f2a7bf0

Browse files
committed
refactor isNew content-type implementations
1 parent 01363ab commit f2a7bf0

File tree

6 files changed

+46
-46
lines changed

6 files changed

+46
-46
lines changed

src/packages/documents/document-types/workspace/document-type-workspace-editor.element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class UmbDocumentTypeWorkspaceEditorElement extends UmbLitElement {
1515
@state()
1616
private _icon?: string;
1717

18+
@state()
19+
private _isNew?: string;
20+
1821
#workspaceContext?: typeof UMB_DOCUMENT_TYPE_WORKSPACE_CONTEXT.TYPE;
1922

2023
constructor() {
@@ -31,6 +34,7 @@ export class UmbDocumentTypeWorkspaceEditorElement extends UmbLitElement {
3134
this.observe(this.#workspaceContext.name, (name) => (this._name = name), '_observeName');
3235
this.observe(this.#workspaceContext.alias, (alias) => (this._alias = alias), '_observeAlias');
3336
this.observe(this.#workspaceContext.icon, (icon) => (this._icon = icon), '_observeIcon');
37+
this.observe(this.#workspaceContext.isNew, (isNew) => (this._isNew = isNew), '_observeIsNew');
3438
}
3539

3640
private async _handleIconClick() {
@@ -70,6 +74,7 @@ export class UmbDocumentTypeWorkspaceEditorElement extends UmbLitElement {
7074
label="name"
7175
value=${this._name}
7276
alias=${this._alias}
77+
?auto-generate-alias=${this._isNew}
7378
@change="${this.#onNameAndAliasChange}"
7479
${umbFocus()}></umb-input-with-alias>
7580
</div>

src/packages/documents/document-types/workspace/document-type-workspace.context.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,18 @@ export class UmbDocumentTypeWorkspaceContext
287287
this.setDefaultTemplate(templateEntity);
288288
}
289289

290-
if ((await this.structure.create(parent.unique)) === true) {
291-
// TODO: this might not be the right place to alert the tree, but it works for now
292-
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
293-
const event = new UmbRequestReloadTreeItemChildrenEvent({
294-
entityType: parent.entityType,
295-
unique: parent.unique,
296-
});
297-
eventContext.dispatchEvent(event);
298-
299-
this.setIsNew(false);
300-
this.createTemplateMode = false;
301-
}
290+
await this.structure.create(parent.unique);
291+
292+
// TODO: this might not be the right place to alert the tree, but it works for now
293+
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
294+
const event = new UmbRequestReloadTreeItemChildrenEvent({
295+
entityType: parent.entityType,
296+
unique: parent.unique,
297+
});
298+
eventContext.dispatchEvent(event);
299+
300+
this.setIsNew(false);
301+
this.createTemplateMode = false;
302302
} else {
303303
await this.structure.save();
304304

src/packages/media/media-types/workspace/media-type-workspace-editor.element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export class UmbMediaTypeWorkspaceEditorElement extends UmbLitElement {
1818
@state()
1919
private _icon?: string;
2020

21+
@state()
22+
private _isNew?: string;
23+
2124
#workspaceContext?: UmbMediaTypeWorkspaceContext;
2225

2326
constructor() {
@@ -34,6 +37,7 @@ export class UmbMediaTypeWorkspaceEditorElement extends UmbLitElement {
3437
this.observe(this.#workspaceContext.name, (name) => (this._name = name), '_observeName');
3538
this.observe(this.#workspaceContext.alias, (alias) => (this._alias = alias), '_observeAlias');
3639
this.observe(this.#workspaceContext.icon, (icon) => (this._icon = icon), '_observeIcon');
40+
this.observe(this.#workspaceContext.isNew, (isNew) => (this._isNew = isNew), '_observeIsNew');
3741
}
3842

3943
private async _handleIconClick() {
@@ -73,6 +77,7 @@ export class UmbMediaTypeWorkspaceEditorElement extends UmbLitElement {
7377
label="name"
7478
value=${this._name}
7579
alias=${this._alias}
80+
?auto-generate-alias=${this._isNew}
7681
@change="${this.#onNameAndAliasChange}"
7782
${umbFocus()}></umb-input-with-alias>
7883
</div>

src/packages/media/media-types/workspace/media-type-workspace.context.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,15 @@ export class UmbMediaTypeWorkspaceContext
208208
const parent = this.#parent.getValue();
209209
if (!parent) throw new Error('Parent is not set');
210210

211-
if ((await this.structure.create(parent.unique)) === true) {
212-
if (!parent) throw new Error('Parent is not set');
213-
214-
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
215-
const event = new UmbRequestReloadTreeItemChildrenEvent({
216-
entityType: parent.entityType,
217-
unique: parent.unique,
218-
});
219-
eventContext.dispatchEvent(event);
220-
this.setIsNew(false);
221-
}
211+
await this.structure.create(parent.unique);
212+
213+
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
214+
const event = new UmbRequestReloadTreeItemChildrenEvent({
215+
entityType: parent.entityType,
216+
unique: parent.unique,
217+
});
218+
eventContext.dispatchEvent(event);
219+
this.setIsNew(false);
222220
} else {
223221
await this.structure.save();
224222

src/packages/members/member-type/workspace/member-type-workspace-editor.element.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class UmbMemberTypeWorkspaceEditorElement extends UmbLitElement {
1414
@state()
1515
private _icon?: string;
1616

17+
@state()
18+
private _isNew?: string;
19+
1720
@state()
1821
private _iconColorAlias?: string;
1922
// TODO: Color should be using an alias, and look up in some dictionary/key/value) of project-colors.
@@ -34,18 +37,7 @@ export class UmbMemberTypeWorkspaceEditorElement extends UmbLitElement {
3437
this.observe(this.#workspaceContext.name, (name) => (this._name = name), '_observeName');
3538
this.observe(this.#workspaceContext.alias, (alias) => (this._alias = alias), '_observeAlias');
3639
this.observe(this.#workspaceContext.icon, (icon) => (this._icon = icon), '_observeIcon');
37-
38-
this.observe(
39-
this.#workspaceContext.isNew,
40-
(isNew) => {
41-
if (isNew) {
42-
// TODO: Would be good with a more general way to bring focus to the name input.
43-
(this.shadowRoot?.querySelector('#name') as HTMLElement)?.focus();
44-
}
45-
this.removeUmbControllerByAlias('_observeIsNew');
46-
},
47-
'_observeIsNew',
48-
);
40+
this.observe(this.#workspaceContext.isNew, (isNew) => (this._isNew = isNew), '_observeIsNew');
4941
}
5042

5143
private async _handleIconClick() {
@@ -85,6 +77,7 @@ export class UmbMemberTypeWorkspaceEditorElement extends UmbLitElement {
8577
label="name"
8678
value=${this._name}
8779
alias=${this._alias}
80+
?auto-generate-alias=${this._isNew}
8881
@change="${this.#onNameAndAliasChange}"
8982
${umbFocus()}></umb-input-with-alias>
9083
</div>

src/packages/members/member-type/workspace/member-type-workspace.context.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,31 +173,30 @@ export class UmbMemberTypeWorkspaceContext
173173
}
174174
}
175175

176+
/**
177+
* Save or creates the member type, based on wether its a new one or existing.
178+
*/
176179
async submit() {
177180
const data = this.getData();
178-
if (data === undefined) throw new Error('Cannot save, no data');
181+
if (!data) {
182+
throw new Error('Something went wrong, there is no data for media type you want to save...');
183+
}
179184

180185
if (this.getIsNew()) {
181186
const parent = this.#parent.getValue();
182187
if (!parent) throw new Error('Parent is not set');
183-
const { error } = await this.repository.create(data, parent.unique);
184-
if (error) {
185-
throw new Error(error.message);
186-
}
187-
this.setIsNew(false);
188188

189-
// TODO: this might not be the right place to alert the tree, but it works for now
189+
await this.structure.create(parent.unique);
190+
190191
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
191192
const event = new UmbRequestReloadTreeItemChildrenEvent({
192193
entityType: parent.entityType,
193194
unique: parent.unique,
194195
});
195196
eventContext.dispatchEvent(event);
197+
this.setIsNew(false);
196198
} else {
197-
const { error } = await this.structure.save();
198-
if (error) {
199-
throw new Error(error.message);
200-
}
199+
await this.structure.save();
201200

202201
const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
203202
const event = new UmbRequestReloadStructureForEntityEvent({

0 commit comments

Comments
 (0)