Skip to content

Commit

Permalink
added search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Jan 31, 2018
1 parent 33dae3b commit e778bd7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
4 changes: 2 additions & 2 deletions web/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ <h1><a href="/">Stan's Robot Shop</a></h1>
</span>
<span id="search">
<form>
<input type="text" size="20"/>
<button>Search</button>
<input type="text" size="20" ng-model="data.searchText"/>
<button ng-click="search();">Search</button>
</form>
</span>

Expand Down
53 changes: 49 additions & 4 deletions web/static/js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
$routeProvider.when('/', {
templateUrl: 'splash.html',
controller: 'shopform'
}).when('/search/:text', {
templateUrl: 'search.html',
controller: 'searchform'
}).when('/product/:sku', {
templateUrl: 'product.html',
controller: 'productform'
Expand All @@ -41,20 +44,22 @@
$locationProvider.html5Mode(true);
}]);

// clear template fragment cache
// clear template fragment cache, development
// TODO - disable this later
robotshop.run(function($rootScope, $templateCache) {
$rootScope.$on('$viewContentLoaded', function() {
console.log('>>> clearing cache');
$templateCache.removeAll();
});
});

robotshop.controller('shopform', function($scope, $http, currentUser) {
robotshop.controller('shopform', function($scope, $http, $location, currentUser) {
$scope.data = {};

$scope.data.uniqueid = 'foo';
$scope.data.categories = [];
$scope.data.products = {};
$scope.data.searchText = '';
// empty cart
$scope.data.cart = {
total: 0
Expand All @@ -75,6 +80,13 @@
}
};

$scope.search = function() {
if($scope.data.searchText) {
$location.url('/search/' + $scope.data.searchText);
$scope.data.searchText = '';
}
};

function getCategories() {
$http({
url: '/api/catalogue/categories',
Expand Down Expand Up @@ -130,6 +142,29 @@
});
});

robotshop.controller('searchform', function($scope, $http, $routeParams) {
$scope.data = {};
$scope.data.searchResults = [];

function search(text) {
if(text) {
$http({
url: '/api/catalogue/search/' + text,
method: 'GET'
}).then((res) => {
console.log('search results', res.data);
$scope.data.searchResults = res.data;
}).catch((e) => {
console.log('ERROR', e);
});
}
}

var text = $routeParams.text;
console.log('search init with', text);
search(text);
});

robotshop.controller('productform', function($scope, $http, $routeParams, $timeout, currentUser) {
$scope.data = {};
$scope.data.message = ' ';
Expand Down Expand Up @@ -387,15 +422,25 @@
method: 'POST',
data: {
name: $scope.data.name,
password: $scope.data.password,
email: $scope.data.email
password: $scope.data.password
}
}).then((res) => {
var oldId = currentUser.uniqueid;
$scope.data.user = res.data;
$scope.data.user.password = '';
$scope.data.password = $scope.data.password2 = '';
currentUser.user = $scope.data.user;
currentUser.uniqueid = $scope.data.user.name;
// login OK move cart across
$http({
url: '/api/cart/rename/' + oldId + '/' + $scope.data.user.name,
method: 'GET'
}).then((res) => {
console.log('cart moved OK');
}).catch((e) => {
// 404 is OK as cart might not exist yet
console.log('ERROR', e);
});
}).catch((e) => {
console.log('ERROR', e);
$scope.data.message = 'ERROR ' + e.data;
Expand Down
16 changes: 16 additions & 0 deletions web/static/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- search template -->
<div>
<div ng-if="data.searchResults.length == 0">
Sorry nothing matches your search
</div>
<div ng-if="data.searchResults.length != 0">
<h3>These ecellent candidates match your search</h3>
<table>
<tr ng-repeat="item in data.searchResults">
<td><a class="product" href="/product/{{ item.sku }}">{{ item.name }}</a></td>
<td>{{ item.description }}</td>
<td>&euro;{{ item.price }}</td>
</tr>
</table>
</div>
</div>

0 comments on commit e778bd7

Please sign in to comment.