|
33 | 33 | <input type="text" name="category" placeholder="New Category" id="{{username}}category" required>
|
34 | 34 | <input type="hidden" name="username" value="{{username}}">
|
35 | 35 | <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> |
39 | 37 | </form>
|
40 | 38 | </td>
|
41 | 39 | </tr>
|
42 | 40 | {{/approvers}}
|
43 | 41 | </tbody>
|
44 | 42 | </table>
|
45 | 43 | </div>
|
| 44 | + |
46 | 45 | <script>
|
47 |
| - window.history.replaceState?.( null, null, window.location.href ); |
| 46 | + const CATEGORIES_FORM_CONTAINER_ID = 'categories-form-container'; |
48 | 47 |
|
49 |
| - function addCategory( event ) { |
| 48 | + const handleCategorySubmit = ( event ) => { |
50 | 49 | event.preventDefault();
|
51 | 50 | const form = event.target;
|
52 | 51 |
|
53 | 52 | if( !form.checkValidity() ) {
|
54 |
| - return form.reportValidity(); |
| 53 | + form.reportValidity(); |
| 54 | + return; |
55 | 55 | }
|
56 | 56 |
|
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 ) => { |
60 | 64 | 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 ); |
63 | 67 |
|
64 |
| - container.replaceWith( newContainer ); |
| 68 | + if( container && newContainer ) { |
| 69 | + container.replaceWith( newContainer ); |
65 | 70 |
|
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 | + } |
69 | 77 | } )
|
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 ); |
71 | 96 | }
|
| 97 | +
|
| 98 | + addEventListeners(); |
72 | 99 | </script>
|
0 commit comments