11import { UmbDocumentPublicAccessRepository } from '../repository/public-access.repository.js' ;
2- import { UmbDocumentDetailRepository } from '../../../repository/index.js' ;
2+ import { UmbDocumentItemRepository } from '../../../repository/index.js' ;
33import type { UmbInputDocumentElement } from '../../../components/index.js' ;
44import type { UmbPublicAccessModalData , UmbPublicAccessModalValue } from './public-access-modal.token.js' ;
55import { css , customElement , html , nothing , state } from '@umbraco-cms/backoffice/external/lit' ;
66import { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal' ;
77import { UmbTextStyles } from '@umbraco-cms/backoffice/style' ;
8- import type { UmbInputMemberElement } from '@umbraco-cms/backoffice/member' ;
9- import type { UmbInputMemberGroupElement } from '@umbraco-cms/backoffice/member-group' ;
8+ import { UmbMemberDetailRepository , type UmbInputMemberElement } from '@umbraco-cms/backoffice/member' ;
9+ import { UmbMemberGroupItemRepository , type UmbInputMemberGroupElement } from '@umbraco-cms/backoffice/member-group' ;
1010import type { PublicAccessRequestModel } from '@umbraco-cms/backoffice/external/backend-api' ;
1111import type { UUIRadioEvent } from '@umbraco-cms/backoffice/external/uui' ;
1212
@@ -32,56 +32,52 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
3232 private _selection : Array < string > = [ ] ;
3333
3434 @state ( )
35- private _loginPageId ?: string ;
35+ private _loginDocumentId ?: string ;
3636
3737 @state ( )
38- private _errorPageId ?: string ;
38+ private _errorDocumentId ?: string ;
3939
4040 // Init
4141
4242 firstUpdated ( ) {
4343 this . #unique = this . data ?. unique ;
4444 this . #getDocumentName( ) ;
45- this . #getPublicAccessModel( ) ;
4645 }
4746
4847 async #getDocumentName( ) {
4948 if ( ! this . #unique) return ;
5049 // Should this be done here or in the action file?
51- const { data } = await new UmbDocumentDetailRepository ( this ) . requestByUnique ( this . #unique) ;
50+ const { data } = await new UmbDocumentItemRepository ( this ) . requestItems ( [ this . #unique] ) ;
5251 if ( ! data ) return ;
52+ const item = data [ 0 ] ;
5353 //TODO How do we ensure we get the correct variant?
54- this . _documentName = data . variants [ 0 ] ?. name ;
54+ this . _documentName = item . variants [ 0 ] ?. name ;
55+
56+ if ( item . isProtected ) {
57+ this . #getPublicAccessModel( ) ;
58+ }
5559 }
5660
5761 async #getPublicAccessModel( ) {
5862 if ( ! this . #unique) return ;
59- //const { data } = (await this.#publicAccessRepository.read(this.#unique));
60- // TODO Currently returning "void". Remove mock data when API is ready. Will it be Response or Request model?
61- const data : any = undefined ;
62- /*const data: PublicAccessResponseModel = {
63- members: [{ name: 'Agent', id: '007' }],
64- groups: [],
65- loginPageId: '123',
66- errorPageId: '456',
67- };*/
63+ const { data } = await this . #publicAccessRepository. read ( this . #unique) ;
6864
6965 if ( ! data ) return ;
7066 this . #isNew = false ;
7167 this . _startPage = false ;
7268
7369 // Specific or Groups
74- this . _specific = data . members . length > 0 ? true : false ;
70+ this . _specific = data . members . length > 0 ;
7571
7672 //selection
7773 if ( data . members . length > 0 ) {
78- this . _selection = data . members . map ( ( m : any ) => m . id ) ;
74+ this . _selection = data . members . map ( ( m ) => m . id ) ;
7975 } else if ( data . groups . length > 0 ) {
80- this . _selection = data . groups . map ( ( g : any ) => g . id ) ;
76+ this . _selection = data . groups . map ( ( g ) => g . id ) ;
8177 }
8278
83- this . _loginPageId = data . loginPageId ;
84- this . _errorPageId = data . errorPageId ;
79+ this . _loginDocumentId = data . loginDocument . id ;
80+ this . _errorDocumentId = data . errorDocument . id ;
8581 }
8682
8783 // Modal events
@@ -91,30 +87,54 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
9187 }
9288
9389 async #handleSave( ) {
94- if ( ! this . _loginPageId || ! this . _errorPageId || ! this . #unique) return ;
95-
96- const groups = this . _specific ? [ ] : this . _selection ;
97- const members = this . _specific ? this . _selection : [ ] ;
90+ if ( ! this . _loginDocumentId || ! this . _errorDocumentId || ! this . #unique) return ;
9891
92+ // TODO: [v15] Currently the Management API doesn't support passing the member/group ids, only the userNames/names.
93+ // This is a temporary solution where we have to look them up until the API is updated to support this.
9994 const requestBody : PublicAccessRequestModel = {
100- memberGroupNames : groups ,
101- memberUserNames : members ,
102- loginDocument : { id : this . _loginPageId } ,
103- errorDocument : { id : this . _errorPageId } ,
95+ memberGroupNames : [ ] ,
96+ memberUserNames : [ ] ,
97+ loginDocument : { id : this . _loginDocumentId } ,
98+ errorDocument : { id : this . _errorDocumentId } ,
10499 } ;
105100
101+ if ( this . _specific ) {
102+ // Members
103+ // user name is not part of the item model, so we need to look it up from the member detail repository
104+ // be aware that the detail repository requires access to the member section.
105+ const repo = new UmbMemberDetailRepository ( this ) ;
106+ const promises = this . _selection . map ( ( memberId ) => repo . requestByUnique ( memberId ) ) ;
107+ const responses = await Promise . all ( promises ) ;
108+ const memberUserNames = responses
109+ . filter ( ( response ) => response . data )
110+ . map ( ( response ) => response . data ?. username ) as string [ ] ;
111+
112+ requestBody . memberUserNames = memberUserNames ;
113+ } else {
114+ // Groups
115+ const repo = new UmbMemberGroupItemRepository ( this ) ;
116+ const { data } = await repo . requestItems ( this . _selection ) ;
117+ if ( ! data ) throw new Error ( 'No Member groups returned' ) ;
118+
119+ const groupNames = data
120+ . filter ( ( groupItem ) => this . _selection . includes ( groupItem . unique ) )
121+ . map ( ( groupItem ) => groupItem . name ) ;
122+
123+ requestBody . memberGroupNames = groupNames ;
124+ }
125+
106126 if ( this . #isNew) {
107- this . #publicAccessRepository. create ( this . #unique, requestBody ) ;
127+ await this . #publicAccessRepository. create ( this . #unique, requestBody ) ;
108128 } else {
109- this . #publicAccessRepository. update ( this . #unique, requestBody ) ;
129+ await this . #publicAccessRepository. update ( this . #unique, requestBody ) ;
110130 }
111131
112132 this . modalContext ?. submit ( ) ;
113133 }
114134
115- #handleDelete( ) {
135+ async #handleDelete( ) {
116136 if ( ! this . #unique) return ;
117- this . #publicAccessRepository. delete ( this . #unique) ;
137+ await this . #publicAccessRepository. delete ( this . #unique) ;
118138 this . modalContext ?. submit ( ) ;
119139 }
120140
@@ -125,11 +145,11 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
125145 // Change Events
126146
127147 #onChangeLoginPage( e : CustomEvent ) {
128- this . _loginPageId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
148+ this . _loginDocumentId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
129149 }
130150
131151 #onChangeErrorPage( e : CustomEvent ) {
132- this . _errorPageId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
152+ this . _errorDocumentId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
133153 }
134154
135155 #onChangeGroup( e : CustomEvent ) {
@@ -182,7 +202,10 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
182202 <small>
183203 <umb-localize key="publicAccess_paLoginPageHelp"> Choose the page that contains the login form </umb-localize>
184204 </small>
185- <umb-input-document max="1" @change=${ this . #onChangeLoginPage} ></umb-input-document>
205+ <umb-input-document
206+ .value=${ this . _loginDocumentId ? this . _loginDocumentId : '' }
207+ max="1"
208+ @change=${ this . #onChangeLoginPage} ></umb-input-document>
186209 </div>
187210 <br />
188211 <div class="select-item">
@@ -192,7 +215,10 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
192215 Used when people are logged on, but do not have access
193216 </umb-localize>
194217 </small>
195- <umb-input-document max="1" @change=${ this . #onChangeErrorPage} ></umb-input-document>
218+ <umb-input-document
219+ .value=${ this . _errorDocumentId ? this . _errorDocumentId : '' }
220+ max="1"
221+ @change=${ this . #onChangeErrorPage} ></umb-input-document>
196222 </div>` ;
197223 }
198224
@@ -220,7 +246,7 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
220246 look="primary"
221247 color="positive"
222248 label=${ this . localize . term ( 'buttons_save' ) }
223- ?disabled=${ ! this . _loginPageId || ! this . _errorPageId || this . _selection . length === 0 }
249+ ?disabled=${ ! this . _loginDocumentId || ! this . _errorDocumentId || this . _selection . length === 0 }
224250 @click="${ this . #handleSave} "></uui-button>`
225251 : html `<uui-button
226252 slot="actions"
0 commit comments