@@ -9,13 +9,15 @@ angular.module("categoryModule")
9
9
"dashboardUtilsService" ,
10
10
"_" ,
11
11
function ( $scope , $routeParams , $location , $q , categoryApiService , dashboardUtilsService , _ ) {
12
- var categoryId , rememberProducts , oldProducts , getDefaultCategory ;
12
+
13
+ var savedProducts = [ ]
14
+
13
15
// Initialize SEO
14
16
if ( typeof $scope . initSeo === "function" ) {
15
17
$scope . initSeo ( "category" ) ;
16
18
}
17
19
18
- categoryId = $routeParams . id ;
20
+ var categoryId = $routeParams . id ;
19
21
20
22
if ( ! categoryId && categoryId !== "new" ) {
21
23
$location . path ( "/categories" ) ;
@@ -25,21 +27,14 @@ function ($scope, $routeParams, $location, $q, categoryApiService, dashboardUtil
25
27
categoryId = null ;
26
28
}
27
29
28
- oldProducts = [ ] ;
29
-
30
- getDefaultCategory = function ( ) {
30
+ function getDefaultCategory ( ) {
31
31
return {
32
32
name : "" ,
33
33
"parent_id" : "" ,
34
34
"products" : [ ]
35
35
} ;
36
36
} ;
37
37
38
- /**
39
- * Current selected category
40
- *
41
- * @type {Object }
42
- */
43
38
$scope . category = { } ;
44
39
45
40
/**
@@ -55,7 +50,7 @@ function ($scope, $routeParams, $location, $q, categoryApiService, dashboardUtil
55
50
var result = response . result || { } ;
56
51
$scope . category = result ;
57
52
$scope . category . parent = $scope . category [ 'parent_id' ] ;
58
- $scope . category . products = $scope . category . product_ids ;
53
+ savedProducts = $scope . category . product_ids ;
59
54
} ) ;
60
55
}
61
56
@@ -64,34 +59,34 @@ function ($scope, $routeParams, $location, $q, categoryApiService, dashboardUtil
64
59
} ;
65
60
66
61
$scope . saveProducts = function ( ) {
67
- var promises = [ ] ;
68
-
69
- var categoryId = $scope . category . id || $scope . category . _id ;
70
- var _prev = $scope . category . product_ids ;
71
- var _new = $scope . category . products ;
72
-
73
- var products_to_remove = _ . difference ( _prev , _new ) ;
74
- var products_to_add = _ . difference ( _new , _prev ) ;
75
-
76
- if ( products_to_remove . length ) {
77
- _ . each ( products_to_remove , function ( productID ) {
78
- promises . push ( categoryApiService . removeProduct ( {
79
- categoryID : categoryId ,
80
- productID : productID
81
- } ) )
82
- } )
83
- }
62
+ var categoryID = $scope . category . id || $scope . category . _id ;
63
+ var newProductList = $scope . category . product_ids ;
84
64
85
- if ( products_to_add . length ) {
86
- _ . each ( products_to_add , function ( productID ) {
87
- promises . push ( categoryApiService . addProduct ( {
88
- categoryId : categoryId ,
89
- productId : productID
90
- } ) )
91
- } )
92
- }
65
+ var toRemove = _ . difference ( savedProducts , newProductList ) ;
66
+ var toAdd = _ . difference ( newProductList , savedProducts ) ;
67
+
68
+ var promises = _ . map ( toRemove , removePid ) ;
69
+ promises = promises . concat ( _ . map ( toAdd , addPid ) ) ;
93
70
94
71
return $q . all ( promises ) ;
72
+
73
+ /////////////////
74
+
75
+ function removePid ( productID ) {
76
+ var params = {
77
+ categoryID : categoryID ,
78
+ productID : productID ,
79
+ } ;
80
+ return categoryApiService . removeProduct ( params ) . $promise ;
81
+ }
82
+
83
+ function addPid ( productID ) {
84
+ var params = {
85
+ categoryID : categoryID ,
86
+ productID : productID ,
87
+ } ;
88
+ return categoryApiService . addProduct ( params ) . $promise ;
89
+ }
95
90
} ;
96
91
97
92
/**
@@ -101,75 +96,81 @@ function ($scope, $routeParams, $location, $q, categoryApiService, dashboardUtil
101
96
$scope . save = function ( ) {
102
97
$ ( '[ng-click="save()"]' ) . addClass ( 'disabled' ) . append ( '<i class="fa fa-spin fa-spinner"><i>' ) . siblings ( '.btn' ) . addClass ( 'disabled' ) ;
103
98
104
- var id , defer , saveSuccess , saveError , updateSuccess , updateError ;
105
- defer = $q . defer ( ) ;
99
+ var defer = $q . defer ( ) ;
100
+ var id = $scope . category . id || $scope . category . _id ;
101
+
102
+ // Props that aren't recognized by the server
103
+ delete $scope . category . parent ;
104
+ delete $scope . category . path ;
105
+ delete $scope . category . products ;
106
+
107
+ if ( ! id ) {
108
+ if ( $scope . category . name !== '' ) {
109
+
110
+ // Props that aren't recognized by the server
111
+ delete $scope . category . product_ids ;
112
+
113
+ categoryApiService . save ( $scope . category ) . $promise
114
+ . then ( saveSuccess , saveError )
115
+ . then ( function ( ) {
116
+ savedProducts = $scope . category . product_ids ;
117
+ } ) ;
118
+ }
119
+ } else {
120
+ $scope . category . id = id ;
121
+ $scope . saveProducts ( ) . then ( function ( ) {
122
+
123
+ // Props that aren't recognized by the server
124
+ delete $scope . category . product_ids ;
106
125
107
- if ( typeof $scope . category !== "undefined" ) {
108
- id = $scope . category . id || $scope . category . _id ;
126
+ categoryApiService . update ( $scope . category ) . $promise
127
+ . then ( updateSuccess , updateError )
128
+ . then ( function ( ) {
129
+ savedProducts = $scope . category . product_ids ;
130
+ } ) ;
131
+ } ) ;
109
132
}
110
133
111
- saveSuccess = function ( response ) {
134
+ return defer . promise ;
135
+
136
+ ////////////////
137
+
138
+ function saveSuccess ( response ) {
112
139
if ( response . error === null ) {
113
140
$scope . category = response . result || getDefaultCategory ( ) ;
114
141
$scope . message = dashboardUtilsService . getMessage ( null , 'success' , 'Category was created successfully' ) ;
115
142
defer . resolve ( true ) ;
116
143
}
117
- $ ( '[ng-click="save()"]' ) . removeClass ( 'disabled' ) . children ( 'i' ) . remove ( ) ;
118
- $ ( '[ng-click="save()"]' ) . siblings ( '.btn' ) . removeClass ( 'disabled' ) ;
144
+
145
+ allowSaving ( ) ;
119
146
} ;
120
147
121
- saveError = function ( ) {
148
+ function saveError ( ) {
122
149
defer . resolve ( false ) ;
123
- $ ( '[ng-click="save()"]' ) . removeClass ( 'disabled' ) . children ( 'i' ) . remove ( ) ;
124
- $ ( '[ng-click="save()"]' ) . siblings ( '.btn' ) . removeClass ( 'disabled' ) ;
150
+ allowSaving ( ) ;
125
151
} ;
126
152
127
- updateSuccess = function ( response ) {
153
+ function updateSuccess ( response ) {
128
154
if ( response . error === null ) {
129
155
$scope . category = response . result || getDefaultCategory ( ) ;
130
156
$scope . message = dashboardUtilsService . getMessage ( null , 'success' , 'Product was updated successfully' ) ;
131
157
defer . resolve ( true ) ;
132
- $ ( '[ng-click="save()"]' ) . removeClass ( 'disabled' ) . children ( 'i' ) . remove ( ) ;
133
- $ ( '[ng-click="save()"]' ) . siblings ( '.btn' ) . removeClass ( 'disabled' ) ;
158
+
159
+ allowSaving ( ) ;
134
160
}
135
161
} ;
136
162
137
- updateError = function ( ) {
163
+ function updateError ( ) {
138
164
defer . resolve ( false ) ;
139
- $ ( '[ng-click="save()"]' ) . removeClass ( 'disabled' ) . children ( 'i' ) . remove ( ) ;
140
- $ ( '[ng-click="save()"]' ) . siblings ( '.btn' ) . removeClass ( 'disabled' ) ;
165
+ allowSaving ( ) ;
141
166
} ;
142
167
143
- delete $scope . category . parent ;
144
- delete $scope . category . path ;
145
-
146
-
147
- if ( ! id ) {
148
- if ( $scope . category . name !== '' ) {
149
-
150
- delete $scope . category . products ;
151
- categoryApiService . save ( $scope . category ) . $promise
152
- . then ( saveSuccess , saveError )
153
- . then ( function ( ) {
154
- $scope . category . products = $scope . category . product_ids ;
155
- } ) ;
156
- }
157
- } else {
158
- $scope . category . id = id ;
159
- $scope . saveProducts ( ) . then ( function ( ) {
160
-
161
- // Clean the associated product list off, before posting up
162
- delete $scope . category . products ;
163
- categoryApiService . update ( $scope . category ) . $promise
164
- . then ( updateSuccess , updateError )
165
- . then ( function ( ) {
166
- $scope . category . products = $scope . category . product_ids ;
167
- } ) ;
168
- } ) ;
169
-
168
+ // Refactor
169
+ function allowSaving ( ) {
170
+ $ ( '[ng-click="save()"]' ) . removeClass ( 'disabled' ) . children ( 'i' ) . remove ( ) ;
171
+ $ ( '[ng-click="save()"]' ) . siblings ( '.btn' ) . removeClass ( 'disabled' ) ;
170
172
}
171
173
172
- return defer . promise ;
173
174
} ;
174
175
175
176
} ] ) ;
0 commit comments