@@ -17,8 +17,6 @@ import Sortable from 'sortablejs';
17
17
import { addElement } from '../../../../assets/src/utils' ;
18
18
19
19
export const handleCategories = ( ) => {
20
- console . log ( 'handleCategories' ) ;
21
-
22
20
const listGroupItems = document . querySelectorAll ( '.list-group-item' ) ;
23
21
const sortableCategories = document . querySelector ( '.list-group.list-group-root' ) ;
24
22
@@ -32,61 +30,63 @@ export const handleCategories = () => {
32
30
} ) ;
33
31
} ) ;
34
32
35
- const sortable = Sortable . create ( sortableCategories , {
36
- animation : 150 ,
37
- dataIdAttr : 'data-id' ,
38
- filter : '.pmf-category-not-sortable' ,
39
- group : 'pmf-category-order' ,
40
- store : {
41
- /**
42
- * Get the order of elements. Called once during initialization.
43
- * @param {Sortable } sortable
44
- * @returns {Array }
45
- */
46
- get : ( sortable ) => {
47
- const order = localStorage . getItem ( sortable . options . group . name ) ;
48
- return order ? order . split ( '|' ) : [ ] ;
49
- } ,
33
+ if ( sortableCategories ) {
34
+ const sortable = Sortable . create ( sortableCategories , {
35
+ animation : 150 ,
36
+ dataIdAttr : 'data-id' ,
37
+ filter : '.pmf-category-not-sortable' ,
38
+ group : 'pmf-category-order' ,
39
+ store : {
40
+ /**
41
+ * Get the order of elements. Called once during initialization.
42
+ * @param {Sortable } sortable
43
+ * @returns {Array }
44
+ */
45
+ get : ( sortable ) => {
46
+ const order = localStorage . getItem ( sortable . options . group . name ) ;
47
+ return order ? order . split ( '|' ) : [ ] ;
48
+ } ,
50
49
51
- /**
52
- * Save the order of elements. Called onEnd (when the item is dropped).
53
- * @param {Sortable } sortable
54
- */
55
- set : ( sortable ) => {
56
- const order = sortable . toArray ( ) ;
57
- const csrf = document . querySelector ( 'input[name=csrf]' ) . value ;
58
- localStorage . setItem ( sortable . options . group . name , order . join ( '|' ) ) ;
50
+ /**
51
+ * Save the order of elements. Called onEnd (when the item is dropped).
52
+ * @param {Sortable } sortable
53
+ */
54
+ set : ( sortable ) => {
55
+ const order = sortable . toArray ( ) ;
56
+ const csrf = document . querySelector ( 'input[name=csrf]' ) . value ;
57
+ localStorage . setItem ( sortable . options . group . name , order . join ( '|' ) ) ;
59
58
60
- fetch ( 'index.php?action=ajax&ajax=categories&ajaxaction=update-order' , {
61
- method : 'POST' ,
62
- headers : {
63
- Accept : 'application/json, text/plain, */*' ,
64
- 'Content-Type' : 'application/json' ,
65
- } ,
66
- body : JSON . stringify ( {
67
- csrf : csrf ,
68
- order : order ,
69
- } ) ,
70
- } )
71
- . then ( async ( response ) => {
72
- if ( response . status === 200 ) {
73
- return response . json ( ) ;
74
- }
75
- throw new Error ( 'Network response was not ok.' ) ;
76
- } )
77
- . then ( ( response ) => {
78
- sortableCategories . insertAdjacentElement (
79
- 'beforebegin' ,
80
- addElement ( 'div' , { classList : 'alert alert-success' , innerText : response . success } )
81
- ) ;
59
+ fetch ( 'index.php?action=ajax&ajax=categories&ajaxaction=update-order' , {
60
+ method : 'POST' ,
61
+ headers : {
62
+ Accept : 'application/json, text/plain, */*' ,
63
+ 'Content-Type' : 'application/json' ,
64
+ } ,
65
+ body : JSON . stringify ( {
66
+ csrf : csrf ,
67
+ order : order ,
68
+ } ) ,
82
69
} )
83
- . catch ( ( error ) => {
84
- sortableCategories . insertAdjacentElement (
85
- 'beforebegin' ,
86
- addElement ( 'div' , { classList : 'alert alert-danger' , innerText : error } )
87
- ) ;
88
- } ) ;
70
+ . then ( async ( response ) => {
71
+ if ( response . status === 200 ) {
72
+ return response . json ( ) ;
73
+ }
74
+ throw new Error ( 'Network response was not ok.' ) ;
75
+ } )
76
+ . then ( ( response ) => {
77
+ sortableCategories . insertAdjacentElement (
78
+ 'beforebegin' ,
79
+ addElement ( 'div' , { classList : 'alert alert-success' , innerText : response . success } )
80
+ ) ;
81
+ } )
82
+ . catch ( ( error ) => {
83
+ sortableCategories . insertAdjacentElement (
84
+ 'beforebegin' ,
85
+ addElement ( 'div' , { classList : 'alert alert-danger' , innerText : error } )
86
+ ) ;
87
+ } ) ;
88
+ } ,
89
89
} ,
90
- } ,
91
- } ) ;
90
+ } ) ;
91
+ }
92
92
} ;
0 commit comments