Skip to content

Commit

Permalink
Merge pull request #286 from crossroads/master
Browse files Browse the repository at this point in the history
March release #1 - Performance improvements
  • Loading branch information
patrixr authored Mar 19, 2019
2 parents 562629c + d825946 commit 3b31428
Show file tree
Hide file tree
Showing 21 changed files with 3,480 additions and 588 deletions.
61 changes: 26 additions & 35 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
{
"predef": [
"document",
"window",
"moment",
"airbrakeJs",
"Twilio",
"$",
"cordova",
"-Promise"
],
"browser": true,
"boss": true,
"curly": true,
"debug": false,
"devel": true,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"eqnull": true,
"esversion": 6,
"unused": true
"predef": [ "document", "window", "moment", "airbrakeJs", "Twilio", "$", "cordova", "-Promise" ],
"browser": true,
"boss": true,
"curly": true,
"debug": false,
"devel": true,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"eqnull": true,
"esversion": 8,
"unused": true
}
96 changes: 70 additions & 26 deletions app/controllers/my_notifications.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,63 @@
import Ember from 'ember';
import offers from './offers';
import Ember from "ember";
import offers from "./offers";

export default offers.extend({
sortProperties: ["createdAt:desc"],
sortedModel: Ember.computed.sort("model", "sortProperties"),
messagesUtil: Ember.inject.service("messages"),
store: Ember.inject.service(),

allMessages: Ember.computed(function(){
allMessages: Ember.computed(function() {
return this.store.peekAll("message");
}),

model: Ember.computed("[email protected]", "session.currentUser.id", "[email protected]", function(){
var currentUserId = this.get('session.currentUser.id');
model: Ember.computed(
"[email protected]",
"session.currentUser.id",
"[email protected]",
function() {
var currentUserId = this.get("session.currentUser.id");

return this.get("allMessages").rejectBy("state", "never-subscribed").rejectBy("offer.createdBy.id", currentUserId);
}),
return this.get("allMessages")
.rejectBy("state", "never-subscribed")
.rejectBy("offer.createdBy.id", currentUserId);
}
),

hasLoadedReadMessages: false,
showUnread: Ember.computed({
get: function() {
return false;
return true;
},
set: function(key, value) {
return value;
}
}),

myNotifications: Ember.computed('showUnread', 'allNotifications', function(){
return this.get('showUnread') ? this.get('unreadNotifications') : this.get('allNotifications');
myNotifications: Ember.computed("showUnread", "allNotifications", function() {
if (this.get("showUnread")) {
return this.get("unreadNotifications");
}
return this.get("allNotifications");
}),

unreadNotifications: Ember.computed("allNotifications.[]", function() {
return this.get("allNotifications").rejectBy("unreadCount", 0);
}),

unreadNotifications: Ember.computed('allNotifications.[]', function(){
return this.get('allNotifications').rejectBy('unreadCount', 0);
readNotifications: Ember.computed("allNotifications.[]", function() {
return this.get("allNotifications").filterBy("unreadCount", 0);
}),

allNotifications: Ember.computed("[email protected]", function(){
allNotifications: Ember.computed("[email protected]", function() {
var keys = {};
var res = [];
this.get("sortedModel").forEach(function(message) {
this.get("sortedModel").forEach(message => {
var isPrivate = message.get("isPrivate");
var key = isPrivate + message.get("offer.id") + (message.get("item.id") || "");
var key =
isPrivate + message.get("offer.id") + (message.get("itemId") || "");
if (!keys[key]) {
var props = ["id", "item", "offer", "sender", "createdAt", "isPrivate"];
var notification = Ember.Object.create(message.getProperties(props));
notification.set("unreadCount", message.get("state") === "unread" ? 1 : 0);
notification.set("text", message.get("body"));
notification.set("isSingleMessage", message.get("state") === "unread");

let notification = this.buildNotification(message);
keys[key] = notification;
res.push(notification);
} else if (message.get("state") === "unread") {
Expand All @@ -58,28 +70,60 @@ export default offers.extend({
return res;
}),

buildNotification(message) {
const props = ["id", "itemId", "offer", "sender", "createdAt", "isPrivate"];

let notification = Ember.Object.create(message.getProperties(props));
notification.set("unreadCount", message.get("state") === "unread" ? 1 : 0);
notification.set("text", message.get("body"));
notification.set("isSingleMessage", message.get("state") === "unread");
if (notification.get("itemId")) {
notification.set(
"item",
this.get("store").peekRecord("item", notification.get("itemId"))
);
}
return notification;
},

actions: {
view(messageId) {
var message = this.store.peekRecord('message', messageId);
var message = this.store.peekRecord("message", messageId);
var route = this.get("messagesUtil").getRoute(message);
this.transitionToRoute.apply(this, route);
},

markThreadRead(notification) {
if(notification.unreadCount === 1) {
var message = this.store.peekRecord('message', notification.id);
if (notification.unreadCount === 1) {
var message = this.store.peekRecord("message", notification.id);
this.get("messagesUtil").markRead(message);
} else {
this.send("view", notification.id);
}
},

toggleShowUnread() {
this.set('showUnread', !this.get('showUnread'));
let showUnread = !this.get("showUnread");

if (!showUnread && !this.get("hasLoadedReadMessages")) {
// We want to show all messages, we load them if they haven't been loaded yet
let loadingView = Ember.getOwner(this)
.lookup("component:loading")
.append();
return this.get("messagesUtil")
.fetchReadMessages()
.then(() => {
this.set("hasLoadedReadMessages", true);
this.set("showUnread", showUnread);
})
.finally(() => loadingView.destroy());
}

this.set("showUnread", showUnread);
},

markAllRead() {
var allUnreadMessages = this.get('model').filterBy('state', 'unread');
var allUnreadMessages = this.get("model").filterBy("state", "unread");
allUnreadMessages.forEach(m => this.get("messagesUtil").markRead(m));
}
}
Expand Down
126 changes: 45 additions & 81 deletions app/controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,92 @@
import Ember from 'ember';
import Ember from "ember";
import { translationMacro as t } from "ember-i18n";
import backNavigator from './../mixins/back_navigator';
import backNavigator from "./../mixins/back_navigator";
import AjaxPromise from "goodcity/utils/ajax-promise";
const { getOwner } = Ember;

export default Ember.Controller.extend(backNavigator, {
filter: '',
searchText: '',
fetchMoreResult: true,
filter: "",
searchText: "",
searchPlaceholder: t("search.placeholder"),
i18n: Ember.inject.service(),

allUsers: Ember.computed(function(){
allUsers: Ember.computed(function() {
return this.store.peekAll("user");
}),

allItems: Ember.computed(function(){
allItems: Ember.computed(function() {
return this.store.peekAll("item");
}),

allGogovanOrders: Ember.computed(function(){
allGogovanOrders: Ember.computed(function() {
return this.store.peekAll("gogovan_order");
}),

allPackageTypes: Ember.computed(function(){
allPackageTypes: Ember.computed(function() {
return this.store.peekAll("package_type");
}),

allAddresses: Ember.computed(function(){
allAddresses: Ember.computed(function() {
return this.store.peekAll("address");
}),

hasSearchText: Ember.computed('searchText', function(){
return Ember.$.trim(this.get('searchText')).length;
hasSearchText: Ember.computed("searchText", function() {
return Ember.$.trim(this.get("searchText")).length;
}),

hasFilter: Ember.computed('filter', function(){
return Ember.$.trim(this.get('filter')).length;
hasFilter: Ember.computed("filter", function() {
return Ember.$.trim(this.get("filter")).length;
}),

onSearchTextChange: Ember.observer('searchText', function () {
onSearchTextChange: Ember.observer("searchText", function() {
// wait before applying the filter
Ember.run.debounce(this, this.applyFilter, 500);
}),

applyFilter: function() {
this.set('filter', this.get('searchText'));
this.set('fetchMoreResult', true);
this.set("filter", this.get("searchText"));
this.searchOnServer();
},

filteredResults: Ember.computed('filter', 'fetchMoreResult', 'allUsers.[]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', function(){
var filter = Ember.$.trim(this.get('filter').toLowerCase().replace(/\s\s+/g, ' '));
var offers = [];
var store = this.store;
var matchFilter = function(value) {
if(value) {
value = value.replace(/\n/g, " ").replace(/\s\s+/g, ' ');
return value.toLowerCase().indexOf(filter) !== -1;
} else {
return false;
}
};

if (filter.length > 0) {
this.get('allUsers').forEach(function(donor) {
if (matchFilter(donor.get('fullName')) || matchFilter(donor.get('mobile'))) {
var donations = donor.get('donations').rejectBy("state", "draft");
offers = offers.concat(donations.toArray());
}
});

this.get('allItems').rejectBy('isDraft', true).rejectBy('donorDescription', null).forEach(function(item) {
if (matchFilter(item.get('donorDescription'))) {
offers.push(item.get('offer'));
}
});

this.get('allGogovanOrders').rejectBy('driverLicense', null).forEach(function(order) {
if (matchFilter(order.get('driverLicense'))) {
offers.push(order.get('delivery.offer'));
}
});

this.get('allPackageTypes').rejectBy('packagesCount', 0).forEach(function(packageType) {
if (matchFilter(packageType.get('name'))) {
packageType.get('packages').forEach(function(pkg) {
var offer = store.peekRecord('offer', pkg.get('offerId'));
if(offer) { offers.push(offer); }
});
}
});

this.get('allAddresses').forEach(function(address) {
if (matchFilter(address.get('regionDetails'))) {
var offer = address.get('addressable.delivery.offer');
if(offer) { offers.push(offer); }
}
});
searchOnServer() {
let search = this.get("filter");
if (!search) {
this.set("filteredResults", null);
return;
}

return offers.uniq();
}),
let loadingView = getOwner(this)
.lookup("component:loading")
.append();
let url = `/offers/search?searchText=${search}`;
let store = this.get("store");

new AjaxPromise(url, "GET", this.get("session.authToken"))
.then(data => {
store.pushPayload(data);
const results = data.offers
.map(o => store.peekRecord("offer", o.id))
.filter(Boolean);
this.set("filteredResults", results);
})
.finally(() => {
loadingView.destroy();
});
},

actions: {
clearSearch(isCancelled) {
this.set('filter', '');
this.set('searchText', '');
this.set('fetchMoreResult', true);
if(!isCancelled) { Ember.$("#searchText").focus(); }
this.set("filter", "");
this.set("searchText", "");
if (!isCancelled) {
Ember.$("#searchText").focus();
}
},

cancelSearch() {
Ember.$("#searchText").blur();
this.send("clearSearch", true);
this.send("togglePath", "search");
},

searchOnServer() {
var controller = this;
var loadingView = getOwner(controller).lookup('component:loading').append();
return this.store.query('offer', { states: ["not_active"] }).finally(function(){
controller.set('fetchMoreResult', false);
loadingView.destroy();
});
}
}

});
2 changes: 1 addition & 1 deletion app/locales/en/translations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ I18nTranslationsEn =

"my_notifications":
"heading" : "{{name}}'s Offer"
"all_notifications" : "All notifications"
"all_notifications" : "Show all notifications"
"show_unread" : "Show unread only"
"mark_all_read" : "Mark all read"
"no_unread": "No unread messages!"
Expand Down
Loading

0 comments on commit 3b31428

Please sign in to comment.