@@ -2,8 +2,7 @@ import { Controller } from 'stimulus';
22import axios from 'axios' ;
33
44export default class extends Controller {
5- static targets = [ "toast" , "message" ] ;
6- static classes = [ "notice" , "alert" ] ;
5+ static targets = [ "toast" , "message" , "noticeIcon" , "alertIcon" ] ;
76
87 connect ( ) {
98 this . element [ this . identifier ] = this
@@ -34,15 +33,35 @@ export default class extends Controller {
3433
3534 display ( message , type = 'notice' ) {
3635 this . messageTarget . innerHTML = message
37- this . element . classList . remove ( this . noticeClass , this . alertClass )
38- this . element . classList . add ( this [ `${ type } Class` ] )
36+ this . displayIconForToastType ( type )
37+ this . showToast ( )
38+ }
39+
40+ showToast ( ) {
3941 this . toastTarget . classList . remove ( 'hidden' )
40- setTimeout ( ( ) => this . hideToast ( ) , 3000 )
42+
43+ this . setToastTimeout ( )
4144 }
4245
4346 hideToast ( ) {
4447 this . toastTarget . classList . add ( 'hidden' )
4548 }
49+
50+ displayIconForToastType ( type ) {
51+ this . iconTargets . forEach ( this . hideIcon )
52+
53+ this [ `${ type } IconTarget` ] . classList . remove ( 'hidden' )
54+ }
55+
56+ hideIcon ( iconTarget ) {
57+ iconTarget . classList . add ( 'hidden' )
58+ }
59+
60+ setToastTimeout ( ) {
61+ const toastTimeoutInMilliseconds = 10000
62+
63+ setTimeout ( ( ) => this . hideToast ( ) , toastTimeoutInMilliseconds )
64+ }
4665
4766 get notice ( ) {
4867 return this . data . get ( 'notice' )
@@ -51,4 +70,8 @@ export default class extends Controller {
5170 get alert ( ) {
5271 return this . data . get ( 'alert' )
5372 }
73+
74+ get iconTargets ( ) {
75+ return [ this . noticeIconTarget , this . alertIconTarget ]
76+ }
5477}
0 commit comments