Skip to content

Commit 7f0b1f8

Browse files
authored
Merge pull request #94 from BuildFire/sorting-issue
fix: Fixed sorting issues
2 parents 7ba008e + d0e6cc9 commit 7f0b1f8

6 files changed

Lines changed: 74 additions & 53 deletions

File tree

control/content/app.services.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,33 @@
247247
buildfire.analytics.unregisterEvent('coupon_item_view_' + key);
248248
}
249249
};
250-
}]).factory('StateSeeder', ['TAG_NAMES', 'DataStore', 'RankOfLastItem', '$rootScope', '$timeout' ,function(TAG_NAMES, DataStore, RankOfLastItem, $rootScope, $timeout) {
250+
}]).factory('defaultInfo', ['SORT', 'SORT_FILTER', 'LAYOUTS', function(SORT, SORT_FILTER, LAYOUTS) {
251+
return {
252+
content: {
253+
carouselImages: [],
254+
description: '',
255+
rankOfLastFilter: 0,
256+
rankOfLastItem: 0,
257+
sortItemBy: SORT.MANUALLY,
258+
sortFilterBy: SORT_FILTER.MANUALLY
259+
},
260+
design: {
261+
itemListLayout: LAYOUTS.itemListLayout[0].name
262+
},
263+
settings: {
264+
defaultView: "list",
265+
distanceIn: "mi",
266+
mapView: "show",
267+
filterPage: "show"
268+
}
269+
}
270+
}])
271+
272+
.factory('StateSeeder', ['TAG_NAMES', 'DataStore', 'RankOfLastItem', '$rootScope', '$timeout', 'defaultInfo' ,function(TAG_NAMES, DataStore, RankOfLastItem, $rootScope, $timeout, defaultInfo) {
251273
let itemsList;
252274
let stateSeederInstance;
253275
$rootScope.oldCouponsIds = [];
276+
let couponInfo = defaultInfo;
254277
let jsonTemplate = {
255278
items: [
256279
{
@@ -313,7 +336,7 @@
313336
let promises = itemsList.map((item, i) => {
314337
return new Promise((resolve, reject) => {
315338
itemsList[i] = _applyDefaults(itemsList[i]);
316-
RankOfLastItem.setRank(itemsList[i].rank);
339+
RankOfLastItem.setRank(Number(itemsList[i].rank));
317340
DataStore.insert(itemsList[i], TAG_NAMES.COUPON_ITEMS).then((res)=> {
318341
if (res) {
319342
$rootScope.oldCouponsIds.push(res.id);
@@ -339,10 +362,25 @@
339362
})
340363
Promise.allSettled(promises).then(() => {
341364
$timeout(()=> {
342-
buildfire.messaging.sendMessageToWidget({ type: "ImportCSV", importing: false });
343-
$rootScope.reloadCoupons = true;
365+
DataStore.get(TAG_NAMES.COUPON_INFO).then((result) => {
366+
if (result && result.data && Object.keys(result.data).length) {
367+
couponInfo = result.data;
368+
}
369+
const rankOfLastItem =
370+
Number(isNaN(RankOfLastItem.getRank()) ? 0 : RankOfLastItem.getRank());
371+
if (couponInfo.content) {
372+
couponInfo.content.rankOfLastItem = rankOfLastItem;
373+
} else {
374+
couponInfo.content = {
375+
rankOfLastItem: rankOfLastItem,
376+
};
377+
}
378+
DataStore.save(couponInfo, TAG_NAMES.COUPON_INFO).then(() => {
379+
buildfire.messaging.sendMessageToWidget({ type: "ImportCSV", importing: false });
380+
$rootScope.reloadCoupons = true;
381+
}).catch(err => console.warn('err while saving coupon info: ', err));
382+
}).catch(err => console.warn('err while getting coupon info: ', err));
344383
})
345-
$rootScope.reloadCoupons = true;
346384
}).catch(err => console.warn('error while saving data: ', err))
347385

348386
})
@@ -358,7 +396,7 @@
358396
summary: item.summary || "",
359397
listImage: item.listImage || "",
360398
startOn: Date.now(),
361-
expiresOn: Math.trunc(Date.now() + Math.random() * 8640000000),
399+
expiresOn: Math.trunc(Date.now() + Math.random() * 8640000000), // Picks a random day from today to One hundred days in the future (1000 mil * 60 sec * 60 min * 24 hour) * 100 days
362400
links: [],
363401
preRedemptionText: "Redeem Now",
364402
postRedemptionText: "Coupon Redeemed",
@@ -369,7 +407,7 @@
369407
"title": "image"
370408
}
371409
] : [],
372-
rank: RankOfLastItem.getRank() ? RankOfLastItem.getRank() + 10: 10,
410+
rank: RankOfLastItem.getRank() ? Number(RankOfLastItem.getRank()) + 10 : 10,
373411
addressTitle: "",
374412
location: {
375413
addressTitle: "",
@@ -380,7 +418,7 @@
380418
},
381419
Categories: [],
382420
reuseAfterInMinutes: -1,
383-
dateCreated: Date.now(),
421+
dateCreated: Math.trunc(Date.now() + (Math.random() * 1000)),
384422
deepLinkUrl: '', // must have an id from datatore
385423
deepLinkId: '', //same as item id
386424
SelectedCategories: [],
@@ -443,7 +481,7 @@
443481
jsonTemplate: jsonTemplate,
444482
sampleCSV: "Save 20% on Flights, Get 20% off on flight bookings with this exclusive coupon, https://source.unsplash.com/1080x720/?travel\n50% Off Hotel Bookings, Enjoy a 50% discount on hotel reservations using this limited-time coupon, https://source.unsplash.com/1080x720/?hotel\nCar Rental Special Offer, Rent a car for 7 days and pay for only 5 days with this coupon code, https://source.unsplash.com/1080x720/?car\nAdventure Tour Promo, Book an adventure tour and receive a free equipment rental worth $50 using this coupon, https://source.unsplash.com/1080x720/?adventure",
445483
maxRecords: 5,
446-
hintText: 'Each row should start with a Coupon title, Summary, and Image URL',
484+
hintText: 'Each row should start with a coupon title, summary, and image URL.',
447485
systemMessage: 'listImage is an image URL, summary and listImage are optional',
448486
callback: handleAIReq.bind(this, true),
449487
},

control/content/controllers/content.home.controller.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
(function (angular, buildfire) {
44
angular
55
.module('couponPluginContent')
6-
.controller('ContentHomeCtrl', ['$scope', '$timeout', 'TAG_NAMES', 'SORT', 'SORT_FILTER', 'STATUS_CODE', 'DataStore', 'LAYOUTS', 'Buildfire', 'Modals', 'RankOfLastFilter', 'RankOfLastItem', '$csv', 'Utils', '$rootScope', 'PluginEvents', 'StateSeeder', '$filter',
7-
function ($scope, $timeout, TAG_NAMES, SORT, SORT_FILTER, STATUS_CODE, DataStore, LAYOUTS, Buildfire, Modals, RankOfLastFilter, RankOfLastItem, $csv, Utils, $rootScope, PluginEvents, StateSeeder, $filter) {
6+
.controller('ContentHomeCtrl', ['$scope', '$timeout', 'TAG_NAMES', 'SORT', 'SORT_FILTER', 'STATUS_CODE', 'DataStore', 'LAYOUTS', 'Buildfire', 'Modals', 'RankOfLastFilter', 'RankOfLastItem', '$csv', 'Utils', '$rootScope', 'PluginEvents', 'StateSeeder', 'defaultInfo',
7+
function ($scope, $timeout, TAG_NAMES, SORT, SORT_FILTER, STATUS_CODE, DataStore, LAYOUTS, Buildfire, Modals, RankOfLastFilter, RankOfLastItem, $csv, Utils, $rootScope, PluginEvents, StateSeeder, defaultInfo ) {
88
var ContentHome = this;
99
let stateSeeder;
1010
$rootScope.$watch('showEmptyState', function(newValue, oldValue) {
@@ -15,27 +15,8 @@
1515
ContentHome.searchValue = "";
1616
ContentHome.filter = null;
1717
ContentHome.isBusy = true;
18-
var _data = {
19-
"content": {
20-
"carouselImages": [],
21-
"description": '',
22-
"rankOfLastFilter": '',
23-
"rankOfLastItem": '',
24-
"sortItemBy": SORT.MANUALLY,
25-
"sortFilterBy": SORT_FILTER.MANUALLY
26-
},
27-
"design": {
28-
"itemListLayout": LAYOUTS.itemListLayout[0].name
29-
},
30-
"settings": {
31-
"defaultView": "list",
32-
"distanceIn": "mi",
33-
"mapView": "show",
34-
"filterPage": "show"
35-
}
36-
};
37-
38-
18+
var _data = defaultInfo;
19+
3920
// Show the top plugin info part when on home view
4021
Buildfire.appearance.setHeaderVisibility(true);
4122

@@ -116,7 +97,8 @@
11697
ContentHome.searchOptionsForItems = {
11798
filter: { "$json.title": { "$regex": '/*' } },
11899
skip: SORT._skip,
119-
limit: SORT._limit + 1 // the plus one is to check if there are any more
100+
limit: SORT._limit + 1, // the plus one is to check if there are any more
101+
sort: { "rank": 1 }
120102
};
121103
/*
122104
* create an artificial delay so api isnt called on every character entered
@@ -204,16 +186,16 @@
204186
var isRankChanged = false;
205187
if (next) {
206188
if (prev) {
207-
draggedItem.data.rank = ((prev.data.rank || 0) + (next.data.rank || 0)) / 2;
189+
draggedItem.data.rank = Number((prev.data.rank || 0) + (next.data.rank || 0)) / 2;
208190
isRankChanged = true;
209191
} else {
210-
draggedItem.data.rank = (next.data.rank || 0) / 2;
192+
draggedItem.data.rank = Number(next.data.rank || 0) / 2;
211193
isRankChanged = true;
212194
}
213195
} else {
214196
if (prev) {
215-
draggedItem.data.rank = (((prev.data.rank || 0) * 2) + 10) / 2;
216-
maxRank = draggedItem.data.rank;
197+
draggedItem.data.rank = Number(((prev.data.rank || 0) * 2) + 10) / 2;
198+
maxRank = Number(draggedItem.data.rank);
217199
isRankChanged = true;
218200
}
219201
}
@@ -1361,21 +1343,21 @@
13611343
var success = function (result) {
13621344
console.info('Init success result:', result);
13631345
ContentHome.data = result.data;
1364-
if (!ContentHome.data.content) {
1346+
if (!Object.keys(ContentHome.data).length) {
13651347
ContentHome.data = angular.copy(_data);
13661348
} else {
13671349
if (!ContentHome.data.content)
13681350
ContentHome.data.content = {};
13691351
if (!ContentHome.data.settings)
1370-
ContentHome.data.settings = {};
1352+
ContentHome.data.settings = defaultInfo.settings;
13711353
if (!ContentHome.data.content.carouselImages)
13721354
editor.loadItems([]);
13731355
else
13741356
editor.loadItems(ContentHome.data.content.carouselImages);
13751357
if (!ContentHome.data.content.sortFilterBy)
13761358
ContentHome.data.content.sortFilterBy = ContentHome.sortFilterOptions[0];
13771359
if (!ContentHome.data.content.sortItemBy)
1378-
ContentHome.data.content.sortItemBy = ContentHome.sortItemOptions[0];
1360+
ContentHome.data.content.sortItemBy = ContentHome.sortItemOptions[4];
13791361
ContentHome.filters = [];
13801362
ContentHome.searchOptions.skip = 0;
13811363
ContentHome.busyFilter = false;

control/content/controllers/content.item.controller.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
console.log("+++++++++++++++SSSSSSS", ContentItem.data);
116116
if (!ContentItem.data.content) {
117117
ContentItem.data.content = {
118-
rankOfLastItem: "",
118+
rankOfLastItem: 0,
119119
};
120120
}
121121
if (!ContentItem.data.settings) {
@@ -253,7 +253,8 @@
253253
} else if (!isNewItemInserted) {
254254
isNewItemInserted = true;
255255
_item.data.dateCreated = +new Date();
256-
_item.data.rank = RankOfLastItem.getRank() + 10;
256+
_item.data.rank =
257+
Number(isNaN(RankOfLastItem.getRank()) ? 0 : RankOfLastItem.getRank()) + 10
257258
DataStore.insert(_item.data, TAG_NAMES.COUPON_ITEMS).then(
258259
function (data) {
259260
updating = false;
@@ -263,7 +264,7 @@
263264
createDeepLink(data, true);
264265
ContentItem.item.id = data.id;
265266
ContentItem.data.content.rankOfLastItem =
266-
RankOfLastItem.getRank() + 10;
267+
Number(isNaN(RankOfLastItem.getRank()) ? 0 : RankOfLastItem.getRank()) + 10;
267268
saveData(ContentItem.data, TAG_NAMES.COUPON_INFO);
268269
updateMasterItem(ContentItem.item);
269270
if (ContentItem.item.id)
@@ -676,7 +677,7 @@
676677
//if index is there it means filter update operation is performed
677678
ContentItem.filter = {
678679
title: response.title,
679-
rank: RankOfLastFilter.getRank() + 10,
680+
rank: Number(isNaN(RankOfLastFilter.getRank()) ? 0 : RankOfLastFilter.getRank()) + 10,
680681
noOfItems: 0,
681682
};
682683

control/content/enums.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
preRedemptionText: '',
7474
postRedemptionText: '',
7575
carouselImages: [],
76-
rank: '',
76+
rank: 0,
7777
addressTitle:'',
7878
location: {
7979
addressTitle: '',

widget/controllers/widget.home.controller.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@
127127
searchOptions.sort = {"title": -1};
128128
break;
129129
case SORT.EXPIRATION_DATE_ASC:
130-
searchOptions.sort = {"expiresOn": -1};
130+
searchOptions.sort = {"expiresOn": 1};
131131
break;
132132
case SORT.EXPIRATION_DATE_DESC:
133-
searchOptions.sort = {"expiresOn": 1};
133+
searchOptions.sort = {"expiresOn": -1};
134134
break;
135135
case SORT.NEWEST_FIRST:
136136
searchOptions.sort = {"dateCreated": -1};
@@ -285,7 +285,7 @@
285285
console.log("==================== WIDGET UPDATE CALLED ===============================")
286286
if($rootScope.importingCSV) return;
287287

288-
setTimeout(function () {
288+
$timeout(function () {
289289
if (event && event.tag === TAG_NAMES.COUPON_INFO) {
290290
WidgetHome.data = event.data;
291291
if (!WidgetHome.data.design)
@@ -294,8 +294,9 @@
294294
WidgetHome.data.content = {};
295295
if (!WidgetHome.data.settings)
296296
WidgetHome.data.settings = {};
297-
if (event.data.content.sortItemBy && currentSortOrder && (currentSortOrder != event.data.content.sortItemBy)) {
297+
if (event.data.content.sortItemBy && (currentSortOrder != event.data.content.sortItemBy)) {
298298
WidgetHome.data.content.sortItemBy = event.data.content.sortItemBy;
299+
currentSortOrder = event.data.content.sortItemBy;
299300
WidgetHome.items = [];
300301
searchOptions.skip = 0;
301302
WidgetHome.busy = false;
@@ -329,8 +330,7 @@
329330
}
330331
}
331332
currentListLayout = WidgetHome.data.design.itemListLayout;
332-
$scope.$digest();
333-
}, 0);
333+
});
334334
};
335335

336336
DataStore.onUpdate("home").then(null, null, onUpdateCallback);

widget/enums.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
})
4848
.constant('SORT', {
4949
MANUALLY: 'Manually',
50-
ITEM_TITLE_A_Z: 'Item Title A-Z',
51-
ITEM_TITLE_Z_A: 'Item Title Z-A',
50+
ITEM_TITLE_A_Z: 'Coupon Title A-Z',
51+
ITEM_TITLE_Z_A: 'Coupon Title Z-A',
5252
NEWEST_FIRST: 'Newest Entry First',
5353
OLDEST_FIRST: 'Oldest Entry First',
5454
EXPIRATION_DATE_ASC: 'Expiration Date Ascending',

0 commit comments

Comments
 (0)