Skip to content

Commit feb1baf

Browse files
waximabbaxJeroenDeDauw
authored andcommitted
Supporting both Enter press and clicking
1 parent 1c873df commit feb1baf

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

templates/ManageApprovers.mustache

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,67 @@
3333
<input type="text" name="category" placeholder="New Category" id="{{username}}category" required>
3434
<input type="hidden" name="username" value="{{username}}">
3535
<input type="hidden" name="action" value="add">
36-
<button class="add-button" value="add" onclick="addCategory( event => event)">
37-
Add
38-
</button>
36+
<button class="add-button" type="submit" value="add">Add</button>
3937
</form>
4038
</td>
4139
</tr>
4240
{{/approvers}}
4341
</tbody>
4442
</table>
4543
</div>
44+
4645
<script>
47-
window.history.replaceState?.( null, null, window.location.href );
46+
const CATEGORIES_FORM_CONTAINER_ID = 'categories-form-container';
4847
49-
function addCategory( event ) {
48+
const handleCategorySubmit = ( event ) => {
5049
event.preventDefault();
5150
const form = event.target;
5251
5352
if( !form.checkValidity() ) {
54-
return form.reportValidity();
53+
form.reportValidity();
54+
return;
5555
}
5656
57-
fetch( window.location.href, { method: 'POST', body: new FormData( form ) } )
58-
.then( response => response.text() )
59-
.then( html => {
57+
const { username } = form.elements;
58+
59+
fetch( window.location.href, {
60+
method: 'POST',
61+
body: new FormData( form )
62+
} ).then( ( response ) => response.text() )
63+
.then( ( html ) => {
6064
const newDoc = new DOMParser().parseFromString( html, 'text/html' );
61-
const container = document.querySelector( "#categories-form-container" );
62-
const newContainer = newDoc.querySelector( "#categories-form-container" );
65+
const container = document.getElementById( CATEGORIES_FORM_CONTAINER_ID );
66+
const newContainer = newDoc.getElementById( CATEGORIES_FORM_CONTAINER_ID );
6367
64-
container.replaceWith( newContainer );
68+
if( container && newContainer ) {
69+
container.replaceWith( newContainer );
6570
66-
const username = form.elements.username.value;
67-
const inputField = document.getElementById( `${ username }category` );
68-
inputField?.focus();
71+
const inputField = document.getElementById( `${ username.value }category` );
72+
if( inputField ) {
73+
inputField.focus();
74+
}
75+
addEventListeners();
76+
}
6977
} )
70-
.catch( error => console.error( 'Error:', error ) );
78+
.catch( ( error ) => {
79+
console.error( 'Error:', error );
80+
} );
81+
};
82+
83+
const addEventListeners = () => {
84+
const container = document.getElementById( CATEGORIES_FORM_CONTAINER_ID );
85+
if( container ) {
86+
container.addEventListener( 'submit', ( event ) => {
87+
if( event.target.classList.contains( 'add-category-form' ) ) {
88+
handleCategorySubmit( event );
89+
}
90+
} );
91+
}
92+
};
93+
94+
if( window.history.replaceState ) {
95+
window.history.replaceState( null, null, window.location.href );
7196
}
97+
98+
addEventListeners();
7299
</script>

0 commit comments

Comments
 (0)