11import { Controller } from 'stimulus' ;
2+ import axios from 'axios' ;
23
34export default class extends Controller {
45 static targets = [ "moveButton" , "listItem" ] ;
56
67
78 initialize ( ) {
9+ const csrfToken = document . querySelector ( "meta[name=csrf-token]" ) . content
10+ axios . defaults . headers . common [ 'X-CSRF-Token' ] = csrfToken
811 }
912
1013 connect ( ) {
@@ -59,8 +62,6 @@ export default class extends Controller {
5962
6063 this . selectedFolderId = folderId
6164
62-
63- this . moveButtonTarget . href = this . moveButtonTarget . href . split ( '?' ) [ 0 ] . concat ( `?folder_id=${ folderId } ` )
6465 if ( this . selectedFolderId === this . originalFolderId ) {
6566 this . moveButtonTarget . disabled = true
6667 this . moveButtonTarget . classList . remove ( 'button--cta-new' )
@@ -72,6 +73,18 @@ export default class extends Controller {
7273 }
7374 }
7475
76+ file ( ) {
77+ axios . post ( this . confirmPath , { folder_id : this . selectedFolderId } , { headers : { 'accept' : 'application/json' } } )
78+ . then ( res => {
79+ this . toast . display ( res . data . message )
80+ this . modal . close ( )
81+ } )
82+ . catch ( error => {
83+ console . error ( error )
84+ this . toast . display ( 'Unable to file snippet' )
85+ } )
86+ }
87+
7588 get url ( ) {
7689 return this . data . get ( 'url' )
7790 }
@@ -84,6 +97,22 @@ export default class extends Controller {
8497 return this . data . get ( 'originalFolderId' )
8598 }
8699
100+ get modal ( ) {
101+ return document . getElementById ( 'modal' ) . modal
102+ }
103+
104+ get toast ( ) {
105+ return document . getElementById ( 'toast' ) . toast
106+ }
107+
108+ get confirmPath ( ) {
109+ return this . data . get ( 'confirmPath' )
110+ }
111+
112+ set confirmPath ( newPath ) {
113+ return this . data . set ( 'confirmPath' , newPath )
114+ }
115+
87116 set selectedFolderId ( folderId ) {
88117 return this . data . set ( 'selectedFolderId' , folderId )
89118 }
0 commit comments