Debian Codegraph @

resources/src/mediawiki.special.block/components/UserLookup.vue

raw
1 <template>
2 <cdx-field
3 :is-fieldset="true"
4 :status="status"
5 :messages="messages"
6 >
7 <cdx-lookup
8 v-model:selected="selection"
9 v-model:input-value="currentSearchTerm"
10 name="wpTarget"
11 required
12 :menu-items="menuItems"
13 :placeholder="$i18n( 'block-user-placeholder' ).text()"
14 :start-icon="cdxIconSearch"
15 @input="onInput"
16 @change="onChange"
17 @update:selected="currentSearchTerm = selection"
18 >
19 </cdx-lookup>
20 <template #label>
21 {{ $i18n( 'block-user-label' ).text() }}
22 </template>
23 <template #description>
24 {{ $i18n( 'block-user-description' ).text() }}
25 </template>
26 </cdx-field>
27 </template>
28
29 <script>
30 const { defineComponent, ref, watch } = require( 'vue' );
31 const { CdxLookup, CdxField } = require( '@wikimedia/codex' );
32 const { storeToRefs } = require( 'pinia' );
33 const { cdxIconSearch } = require( '../icons.json' );
34 const useBlockStore = require( '../stores/block.js' );
35 const api = new mw.Api();
36
37 module.exports = exports = defineComponent( {
38 name: 'UserLookup',
39 components: { CdxLookup, CdxField },
40 props: {
41 modelValue: { type: [ String, null ], required: true },
42 /**
43 * Whether the form has been submitted yet. This is used to show
44 * validation messages only after the form has been submitted.
45 */
46 formSubmitted: {
47 type: Boolean,
48 default: false
49 }
50 },
51 emits: [
52 'update:modelValue'
53 ],
54 setup( props ) {
55 const { targetUser } = storeToRefs( useBlockStore() );
56
57 // Set a flag to keep track of pending API requests, so we can abort if
58 // the target string changes
59 let pending = false;
60
61 // Codex Lookup component requires a v-modeled `selected` prop.
62 // Until a selection is made, the value may be set to null.
63 // We instead want to only update the targetUser for non-null values
64 // (made either via selection, or the 'change' event).
65 const selection = ref( props.modelValue || '' );
66 // This handles changes via selection, while onChange() handles changes via input.
67 watch( selection, ( newValue ) => {
68 if ( newValue !== null ) {
69 targetUser.value = newValue;
70 }
71 } );
72
73 const currentSearchTerm = ref( props.modelValue || '' );
74 const menuItems = ref( [] );
75 const status = ref( 'default' );
76 const messages = ref( {} );
77
78 /**
79 * Get search results.
80 *
81 * @param {string} searchTerm
82 * @return {Promise}
83 */
84 function fetchResults( searchTerm ) {
85 const params = {
86 action: 'query',
87 format: 'json',
88 formatversion: 2,
89 list: 'allusers',
90 aulimit: '10',
91 auprefix: searchTerm
92 };
93
94 return api.get( params )
95 .then( ( response ) => response.query );
96 }
97
98 /**
99 * Handle lookup input.
100 *
101 * @param {string} value
102 */
103 function onInput( value ) {
104 // Abort any existing request if one is still pending
105 if ( pending ) {
106 pending = false;
107 api.abort();
108 }
109
110 // Internally track the current search term.
111 currentSearchTerm.value = value;
112
113 // Do nothing if we have no input.
114 if ( !value ) {
115 menuItems.value = [];
116 return;
117 }
118
119 fetchResults( value )
120 .then( ( data ) => {
121 pending = false;
122
123 // Make sure this data is still relevant first.
124 if ( currentSearchTerm.value !== value ) {
125 return;
126 }
127
128 // Reset the menu items if there are no results.
129 if ( !data.allusers || data.allusers.length === 0 ) {
130 menuItems.value = [];
131 return;
132 }
133
134 // Build an array of menu items.
135 menuItems.value = data.allusers.map( ( result ) => ( {
136 label: result.name,
137 value: result.name
138 } ) );
139 } )
140 .catch( () => {
141 // On error, set results to empty.
142 menuItems.value = [];
143 } );
144 }
145
146 /**
147 * Validate the input element.
148 *
149 * @param {HTMLInputElement} el
150 */
151 function validate( el ) {
152 if ( el.checkValidity() ) {
153 status.value = 'default';
154 messages.value = {};
155 } else {
156 status.value = 'error';
157 messages.value = { error: el.validationMessage };
158 }
159 }
160
161 /**
162 * Handle lookup change.
163 *
164 * @param {Event} event
165 */
166 function onChange( event ) {
167 validate( event.target );
168 targetUser.value = event.target.value;
169 }
170
171 // Validate the input when the form is submitted.
172 // TODO: Remove once Codex supports native validations (T373872).
173 watch( () => props.formSubmitted, () => {
174 validate( document.querySelector( '[name="wpTarget"]' ) );
175 } );
176
177 return {
178 menuItems,
179 onChange,
180 onInput,
181 cdxIconSearch,
182 currentSearchTerm,
183 selection,
184 status,
185 messages
186 };
187 }
188 } );
189 </script>