Skip to content

Commit 47778c7

Browse files
committed
require all or no mandatory for unpublishing
1 parent 7ae4ae7 commit 47778c7

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/packages/documents/documents/modals/unpublish-modal/document-unpublish-modal.element.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,72 @@ import { UmbSelectionManager } from '@umbraco-cms/backoffice/utils';
1212

1313
import '../shared/document-variant-language-picker.element.js';
1414

15+
/**
16+
* @function isPublished
17+
* @param {UmbDocumentVariantOptionModel} option - the option to check.
18+
* @returns {boolean} boolean
19+
*/
20+
export function isPublished(option: UmbDocumentVariantOptionModel): boolean {
21+
return (
22+
option.variant?.state === UmbDocumentVariantState.PUBLISHED ||
23+
option.variant?.state === UmbDocumentVariantState.PUBLISHED_PENDING_CHANGES
24+
);
25+
}
26+
1527
@customElement('umb-document-unpublish-modal')
1628
export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
1729
UmbDocumentUnpublishModalData,
1830
UmbDocumentUnpublishModalValue
1931
> {
20-
#selectionManager = new UmbSelectionManager<string>(this);
32+
protected readonly _selectionManager = new UmbSelectionManager<string>(this);
2133
#referencesRepository = new UmbDocumentReferenceRepository(this);
2234

2335
@state()
2436
_options: Array<UmbDocumentVariantOptionModel> = [];
2537

38+
@state()
39+
_selection: Array<string> = [];
40+
2641
@state()
2742
_hasReferences = false;
2843

2944
@state()
3045
_hasUnpublishPermission = true;
3146

47+
@state()
48+
_hasInvalidSelection = true;
49+
3250
override firstUpdated() {
3351
this.#configureSelectionManager();
3452
this.#getReferences();
3553
}
3654

3755
async #configureSelectionManager() {
38-
this.#selectionManager.setMultiple(true);
39-
this.#selectionManager.setSelectable(true);
56+
this._selectionManager.setMultiple(true);
57+
this._selectionManager.setSelectable(true);
4058

4159
// Only display variants that are relevant to pick from, i.e. variants that are draft or published with pending changes:
42-
this._options =
43-
this.data?.options.filter(
44-
(option) =>
45-
option.variant &&
46-
(!option.variant.state ||
47-
option.variant.state === UmbDocumentVariantState.PUBLISHED ||
48-
option.variant.state === UmbDocumentVariantState.PUBLISHED_PENDING_CHANGES),
49-
) ?? [];
60+
this._options = this.data?.options.filter((option) => isPublished(option)) ?? [];
5061

5162
let selected = this.value?.selection ?? [];
5263

5364
// Filter selection based on options:
5465
selected = selected.filter((s) => this._options.some((o) => o.unique === s));
5566

56-
this.#selectionManager.setSelection(selected);
67+
this._selectionManager.setSelection(selected);
68+
69+
this.observe(
70+
this._selectionManager.selection,
71+
(selection) => {
72+
this._selection = selection;
73+
const selectionHasMandatory = this._options.some((o) => o.language.isMandatory && selection.includes(o.unique));
74+
const selectionDoesNotHaveAllMandatory = this._options.some(
75+
(o) => o.language.isMandatory && !selection.includes(o.unique),
76+
);
77+
this._hasInvalidSelection = selectionHasMandatory && selectionDoesNotHaveAllMandatory;
78+
},
79+
'observeSelection',
80+
);
5781
}
5882

5983
async #getReferences() {
@@ -80,7 +104,7 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
80104

81105
#submit() {
82106
if (this._hasUnpublishPermission) {
83-
this.value = { selection: this.#selectionManager.getSelection() };
107+
this.value = { selection: this._selection };
84108
this.modalContext?.submit();
85109
return;
86110
}
@@ -91,6 +115,10 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
91115
this.modalContext?.reject();
92116
}
93117

118+
private _requiredFilter = (variantOption: UmbDocumentVariantOptionModel): boolean => {
119+
return variantOption.language.isMandatory && !this._selection.includes(variantOption.unique);
120+
};
121+
94122
override render() {
95123
return html`<umb-body-layout headline=${this.localize.term('content_unpublish')}>
96124
<p id="subtitle">
@@ -100,8 +128,9 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
100128
</p>
101129
102130
<umb-document-variant-language-picker
103-
.selectionManager=${this.#selectionManager}
131+
.selectionManager=${this._selectionManager}
104132
.variantLanguageOptions=${this._options}
133+
.requiredFilter=${this._hasInvalidSelection ? this._requiredFilter : undefined}
105134
.pickableFilter=${this.data?.pickableFilter}></umb-document-variant-language-picker>
106135
107136
<p>
@@ -130,7 +159,7 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
130159
<uui-button label=${this.localize.term('general_close')} @click=${this.#close}></uui-button>
131160
<uui-button
132161
label="${this.localize.term('actions_unpublish')}"
133-
?disabled=${!this._hasUnpublishPermission || !this.#selectionManager.getSelection().length}
162+
?disabled=${this._hasInvalidSelection || !this._hasUnpublishPermission || this._selection.length === 0}
134163
look="primary"
135164
color="warning"
136165
@click=${this.#submit}></uui-button>

0 commit comments

Comments
 (0)