Move login JS to Stimulus
This commit is contained in:
		
							parent
							
								
									47bed0bacc
								
							
						
					
					
						commit
						a67f5d3659
					
				| @ -7,41 +7,6 @@ import './bootstrap.js'; | |||||||
|  */ |  */ | ||||||
| import './styles/app.css'; | import './styles/app.css'; | ||||||
| 
 | 
 | ||||||
| function showPasswd(selector) { |  | ||||||
|     const icon = document.querySelector(selector); |  | ||||||
|     const pwInput = document.querySelector('#pass-field'); |  | ||||||
| 
 |  | ||||||
|     icon.addEventListener('click', function () { |  | ||||||
|         if (pwInput.getAttribute('type') === 'password') { |  | ||||||
|             this.firstElementChild.classList.remove('fa-eye-slash'); |  | ||||||
|             this.firstElementChild.classList.add('fa-eye'); |  | ||||||
|             pwInput.setAttribute('type', 'text'); |  | ||||||
|         } else { |  | ||||||
|             this.firstElementChild.classList.remove('fa-eye'); |  | ||||||
|             this.firstElementChild.classList.add('fa-eye-slash'); |  | ||||||
|             pwInput.setAttribute('type', 'password'); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| console.log('whatever'); |  | ||||||
| 
 |  | ||||||
| if (location.pathname.includes('login')) { |  | ||||||
|     showPasswd('#show-pw'); |  | ||||||
| 
 |  | ||||||
|     document.querySelector('#submit > button').addEventListener('click', function () { |  | ||||||
|         this.parentElement.classList.add('is-loading'); |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
|     const error = document.querySelector('.is-danger'); |  | ||||||
| 
 |  | ||||||
|     if (error) { |  | ||||||
|         error.querySelector('.delete').addEventListener('click', () => { |  | ||||||
|             error.classList.add('is-hidden'); |  | ||||||
|         }); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| if (! location.pathname.includes('login')) { | if (! location.pathname.includes('login')) { | ||||||
|     const vocabs = document.querySelector('#vocabs'); |     const vocabs = document.querySelector('#vocabs'); | ||||||
|     const records = document.querySelector('#records'); |     const records = document.querySelector('#records'); | ||||||
|  | |||||||
| @ -1,16 +0,0 @@ | |||||||
| import { Controller } from '@hotwired/stimulus'; |  | ||||||
| 
 |  | ||||||
| /* |  | ||||||
|  * This is an example Stimulus controller! |  | ||||||
|  * |  | ||||||
|  * Any element with a data-controller="hello" attribute will cause |  | ||||||
|  * this controller to be executed. The name "hello" comes from the filename: |  | ||||||
|  * hello_controller.js -> "hello" |  | ||||||
|  * |  | ||||||
|  * Delete this file or adapt it for your use! |  | ||||||
|  */ |  | ||||||
| export default class extends Controller { |  | ||||||
|     connect() { |  | ||||||
|         this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js'; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
							
								
								
									
										9
									
								
								assets/controllers/loading_controller.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								assets/controllers/loading_controller.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | import { Controller } from '@hotwired/stimulus'; | ||||||
|  | 
 | ||||||
|  | export default class extends Controller { | ||||||
|  |     static targets = ['control']; | ||||||
|  | 
 | ||||||
|  |     setLoading() { | ||||||
|  |         this.controlTarget.classList.add('is-loading'); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								assets/controllers/notification_controller.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								assets/controllers/notification_controller.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | import { Controller } from '@hotwired/stimulus'; | ||||||
|  | 
 | ||||||
|  | export default class extends Controller { | ||||||
|  |     static targets = ['notif']; | ||||||
|  | 
 | ||||||
|  |     close() { | ||||||
|  |         this.notifTarget.classList.add('is-hidden'); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										28
									
								
								assets/controllers/show_password_controller.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								assets/controllers/show_password_controller.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | |||||||
|  | import { Controller } from '@hotwired/stimulus'; | ||||||
|  | 
 | ||||||
|  | /* | ||||||
|  |  * This is an example Stimulus controller! | ||||||
|  |  * | ||||||
|  |  * Any element with a data-controller="hello" attribute will cause | ||||||
|  |  * this controller to be executed. The name "hello" comes from the filename: | ||||||
|  |  * hello_controller.js -> "hello" | ||||||
|  |  */ | ||||||
|  | export default class extends Controller { | ||||||
|  |     static targets = ['pass']; | ||||||
|  | 
 | ||||||
|  |     toggle(event) { | ||||||
|  |         const span = event.currentTarget; | ||||||
|  |         const icon = span.firstElementChild; | ||||||
|  | 
 | ||||||
|  |         if (this.passTarget.type === 'password') { | ||||||
|  |             icon.classList.remove('fa-eye'); | ||||||
|  |             icon.classList.add('fa-eye-slash'); | ||||||
|  |             this.passTarget.type = 'text'; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             icon.classList.remove('fa-eye-slash'); | ||||||
|  |             icon.classList.add('fa-eye'); | ||||||
|  |             this.passTarget.type = 'password'; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -12,9 +12,10 @@ | |||||||
| 
 | 
 | ||||||
|     <div class="card" style="max-width: 40vw; margin: 0 auto;"> |     <div class="card" style="max-width: 40vw; margin: 0 auto;"> | ||||||
|         {% if error %} |         {% if error %} | ||||||
|             <article class="message is-danger"> |             <div class="notification is-danger is-light" data-controller="notification" data-notification-target="notif"> | ||||||
|                 <div class="message-body">Wrong user name and/or password. Please retry</div> |                 <button class="delete" data-action="click->notification#close"></button> | ||||||
|             </article> |                 Wrong user name and/or password. Please retry | ||||||
|  |             </div> | ||||||
|         {% endif %} |         {% endif %} | ||||||
|         <form class="card-content pl-5 pr-5 pt-6" action="{{ path('app_login') }}" id="login" method="post"> |         <form class="card-content pl-5 pr-5 pt-6" action="{{ path('app_login') }}" id="login" method="post"> | ||||||
|             <div class="field"> |             <div class="field"> | ||||||
| @ -26,25 +27,26 @@ | |||||||
|                     <input class="input" name="_username" required type="text" placeholder="User name"> |                     <input class="input" name="_username" required type="text" placeholder="User name"> | ||||||
|                 </p> |                 </p> | ||||||
|             </div> |             </div> | ||||||
|             <div class="field mt-5"> |             <div class="field mt-5" data-controller="show-password"> | ||||||
|                 <label class="label">Password</label> |                 <label class="label">Password</label> | ||||||
|                 <p class="control has-icons-right"> |                 <p class="control has-icons-right"> | ||||||
|                 <input class="input" type="password" name="_password" required placeholder="Password" id="pass-field"> |                 <input class="input" type="password" name="_password" required placeholder="Password" id="pass-field" | ||||||
|                 <span class="icon is-small is-right is-clickable" id="show-pw"> |                     data-show-password-target="pass" /> | ||||||
|                     <i class="fa fa-eye-slash"></i> |                 <span class="icon is-small is-right is-clickable" id="show-pw" | ||||||
|  |                     data-action="click->show-password#toggle"> | ||||||
|  |                     <i class="fa fa-eye"></i> | ||||||
|                 </span> |                 </span> | ||||||
|                 </p> |                 </p> | ||||||
|             </div> |             </div> | ||||||
|             <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"> |             <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"> | ||||||
|             <div class="field mt-5"> |             <div class="field mt-5" data-controller="loading"> | ||||||
|                 <p class="control" id="submit"> |                 <p class="control" id="submit" data-loading-target="control"> | ||||||
|                     <button class="button is-link is-fullwidth" type="submit"> |                     <button class="button is-link is-fullwidth" type="submit" data-action="click->loading#setLoading"> | ||||||
|                         Sign in |                         Sign in | ||||||
|                     </button> |                     </button> | ||||||
|                 </p> |                 </p> | ||||||
|             </div> |             </div> | ||||||
|         </form> |         </form> | ||||||
|     </div> |     </div> | ||||||
| 
 |  | ||||||
| </div> | </div> | ||||||
| {% endblock %} | {% endblock %} | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user